Javascript unit test (jsunit application)

Source: Internet
Author: User

Jsunit is the Javascript version of JUnit. Download and decompress jsunit zip. Most of the core files are in the jsunit/APP directory. Two basic files are available:
. Jsunitcore. js-each test file must reference it.
. Testrunner.html -- used to run a specific jsunit Test

(1) Example:
Create two files under the same level directory after jsunit is decompressed. The directory structure is as follows:
| -- Jsunit
| -- Simple. js
| -- Simple_test.html

Run jsunit/testrunner.html, select simple_test.html on the page, and click Run to view the test result.

// Simple. js Javascript file tested
Function addtwonumbers (value1, value2 ){
Return parseint (value1) + parseint (value2 );
}

// Simple_test.html Test File
<! Doctype HTML public
"-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Script language = "JavaScript"
Src = "jsunit/APP/jsunitcore. js"> </SCRIPT>
<Script language = "JavaScript"
Src = "Simple. js"> </SCRIPT>
<Script language = "JavaScript">
Function testvalidargs ()
{
Assertequals ("2 + 2 is 4 ",
4, addtwonumbers (2, 2 ));
}
Function testwithnegativenumbers ()
{
Assertequals ("-2 +-2 is-4 ",
-4, addtwonumbers (-2,-2 ));
}
Function exposetestfunctionnames ()
{
VaR tests = new array (2 );
Tests [0] = "testvalidargs ";
Tests [1] = "testwithnegativenumbers ";
Return tests;
}
</SCRIPT>
</Head>

<Body>
Test page for addtwonumbers (value1, value2 );
</Body>
</Html>

Generally, test functions starting with "test" will be automatically discovered. However, if not, we need to use the exposetestfunctionnames function to display the specified test function.

(2) setuppage ()

Jsunit contains a one-time start method. setuppage () is called only once for each test page, that is, before all test functions are called. It is suitable for preprocessing () in the last line, set the setuppagestatus variable to "complete", for example:

VaR arg1;
VaR arg2;

Function setuppage (){
Arg1 = 2;
Arg2 = 2;
Setuppagestatus = "complete ";
}

(3) Test Set

The test set groups different test pages, including test pages or other test sets. They are executed in order.
Note:
. The test set cannot contain any test function.
. Must contain a suite function that returns the jsunittestsuite object
. There are two ways to add the test page: addtestpage (testpage) addtestsuite (testsuite). When testing the page, you must renew the page, which is relative to testrunner.html.

Example:

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Script language = "JavaScript"
Src = "jsunit/APP/jsunitcore. js"> </SCRIPT>
<Script language = "JavaScript"
Src = "Simple. js"> </SCRIPT>
<Script language = "JavaScript">
Function samplesuite ()
{
VaR samplesuite = new top. jsunittestsuite ();
Samplesuite. addtestpage ("../simple_test.html ");
Return samplesuite;
}
Function Suite ()
{
VaR testsuite = new top. jsunittestsuite ();
Testsuite. addtestsuite (samplesuite ());
Testsuite. addtestpage ("../simple_test.html ");
Return testsuite;
}
</SCRIPT>
</Head>

<Body>
</Body>
</Html>

(4) logs and tracking

When running testrunner.html, you will find that testrunner.html has a trace level item, which is used for the log information in the test method. It contains three functions:
Warn (message, [value])
Inform (message, [value])
Debug (message, [value])

# JavaScript/ajax Columns

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.