JsUnit Tool Application

Keywords: Javascript Web Server

Why use JsUnit, because the project is not a WEB project, JS code can not use web pages test JsUnit is also used to test the JS code written.

Preparations: Download JsUnit related resources: www.jsunit.net, decompressed as follows:


The core files of JsUnit (jsUnitCore.js, jsUnitTestManager.js, etc.) are in the app folder. TesRunner. html is used to test the graphical page of JS. You don't need to access it through the web server, you just need to load it through the file system to browse, as follows:

Load your test page through the file input box, click Run to run the test page, and the following Trace level drop-down box can select the log level (warn, info, debug). Note that some browsers may not see the file input box, please select the appropriate browser test.

Let's write a test case test01.html:

  1. <pre name="code" class="html"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">   
  2. <HTML>   
  3. <HEAD>   
  4. <TITLE> my_jsUnit_test01</TITLE>   
  5. <script language="JavaScript" src="../app/jsUnitCore.js"></script>  
  6. <script language="JavaScript">  
  7. var result = null;  
  8.   
  9. function add(num1,num2){  
  10.     return num1+num2;  
  11. }  
  12. function multiply(num1,num2){  
  13.     return num1*num2;  
  14. }  
  15. //The test method should start with test without parameters. jsUnit automatically identifies the test method that starts with test and runs it.
  16. function testAdd() {  
  17.     result = add(1,1);  
  18.     debug("add(1,1)="+result);  
  19.     assertEquals("1+1=2", 2, add(1,1));  
  20. }  
  21. //Invalid test methods
  22. function multiplyTest(){  
  23.     result = multiply(2,3);  
  24.     debug("multiply(2,3)="+result);  
  25.         assertEquals("2*3=6", 5, multiply(2,3));  
  26. }  
  27. //If you cannot find a test method or if the method to be tested does not start with a standard test, use the following method to specify manually
  28. function exposeTestFunctionNames() {   
  29.     var tests = new Array(2);   
  30.     tests[0] = "multiplyTest";   
  31.     tests[1] = "testAdd";   
  32.     return tests;   
  33. }  
  34. //setUp() is called before each test.
  35. function setUp(){  
  36.     info("–>result="+result);  
  37.     result = 0;  
  38.     info("Test fixture, initialization ready state–>result="+result);  
  39. }  
  40.   
  41. //tearDown() is called after each test.
  42. function tearDown(){  
  43.     result = null;  
  44.     warn("Clear or reset test fixture–>result="+result);  
  45. }  
  46.   
  47. //A one-time start method is called before all test functions (including setUp()) are called. It is advisable that there should be no log operation in this method.
  48. function setUpPage(){  
  49.     result = 100;  
  50.     //Set this when you're done, or you'll be blocked.
  51.         setUpPageStatus = "complete";   
  52. }  
  53. </script>   
  54. </HEAD>   
  55.   
  56. <BODY>   
  57. test page for testValidArgs();   
  58. </BODY>   
  59. </HTML> </pre><br>  
  60. <pre></pre>  
  61. <p></p>  
  62. <pre></pre>  
  63. <p></p>  
  64. <p><span style="font-size:13px; color:#009933">open testRunner.html,stay file Fill in your test01.html Path, Choice debug Log level, Click Run,When the test results appear, Click error Information will pop up specific information:</span></p>  
  65. <p><span style="font-size:13px; color:#009933"><img src="http://hi.csdn.net/attachment/201111/17/0_1321517493y0PN.gif" alt=""></span></p>  
  66. <p><span style="font-size:13px; color:#009933">click Run The post-pop-up log information is as follows:</span></p>  
  67. <p><span style="font-size:13px; color:#009933"><img src="http://hi.csdn.net/attachment/201111/17/0_132151771397rY.gif" alt=""></span></p>  
  68. <p><span style="font-size:13px; color:#009933">Batch testing:</span></p>  
  69. <p><span style="font-size:13px; color:#009933"></span></p><pre name="code" class="html"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  70. <html>  
  71.   <head>  
  72.     <title>testSuite.html</title>  
  73.     <script language="JavaScript" src="../app/jsUnitCore.js"></script>  
  74.     <script type="text/javascript">  
  75.         function sampleSuite(){  
  76.             var sampleSuite = new JsUnitTestSuite();  
  77.             //Add test pages to the test suite
  78.             sampleSuite.addTestPage("tests/taobao_test.html");  
  79.             return sampleSuite;  
  80.         }  
  81.         //jsUnit must provide a method named suite() in order to identify the test set and test automatically.
  82.         function suite(){  
  83.             var testSuite = new JsUnitTestSuite();  
  84.             //Adding other test sets to the test set
  85.             testSuite.addTestSuite(sampleSuite());  
  86.             testSuite.addTestPage("tests/ShopexCore_test.html");  
  87.             return testSuite;  
  88.         }  
  89.     </script>  
  90.   </head>  
  91.     
  92.   <body>  
  93.     This is a simple test suite.   
  94.   </body>  
  95. </html>  
  96. </pre><br>  
  97. <br>  
  98. <p></p>  
  99. <p><span style="font-size:13px; color:#009933"></span></p>  
  100. <pre></pre>  
  101.      
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">   
  2. <HTML>   
  3. <HEAD>   
  4. <TITLE> my_jsUnit_test01</TITLE>   
  5. <script language="JavaScript" src="../app/jsUnitCore.js"></script>  
  6. <script language="JavaScript">  
  7. var result = null;  
  8.   
  9. function add(num1,num2){  
  10.     return num1+num2;  
  11. }  
  12. function multiply(num1,num2){  
  13.     return num1*num2;  
  14. }  
  15. //The test method should start with test without parameters. jsUnit automatically identifies the test method that starts with test and runs it.
  16. function testAdd() {  
  17.     result = add(1,1);  
  18.     debug("add(1,1)="+result);  
  19.     assertEquals("1+1=2", 2, add(1,1));  
  20. }  
  21. //Invalid test methods
  22. function multiplyTest(){  
  23.     result = multiply(2,3);  
  24.     debug("multiply(2,3)="+result);  
  25.         assertEquals("2*3=6", 5, multiply(2,3));  
  26. }  
  27. //If you cannot find a test method or if the method to be tested does not start with a standard test, use the following method to specify manually
  28. function exposeTestFunctionNames() {   
  29.     var tests = new Array(2);   
  30.     tests[0] = "multiplyTest";   
  31.     tests[1] = "testAdd";   
  32.     return tests;   
  33. }  
  34. //setUp() is called before each test.
  35. function setUp(){  
  36.     info("-->result="+result);  
  37.     result = 0;  
  38.     info("Test fixture, initialization ready state-->result="+result);  
  39. }  
  40.   
  41. //tearDown() is called after each test.
  42. function tearDown(){  
  43.     result = null;  
  44.     warn("Clear or reset test fixture-->result="+result);  
  45. }  
  46.   
  47. //A one-time start method is called before all test functions (including setUp()) are called. It is advisable that there should be no log operation in this method.
  48. function setUpPage(){  
  49.     result = 100;  
  50.     //Set this when you're done, or you'll be blocked.
  51.         setUpPageStatus = "complete";   
  52. }  
  53. </script>   
  54. </HEAD>   
  55.   
  56. <BODY>   
  57. test page for testValidArgs();   
  58. </BODY>   
  59. </HTML>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> my_jsUnit_test01</TITLE> 
<script language="JavaScript" src="../app/jsUnitCore.js"></script>
<script language="JavaScript">
var result = null;

function add(num1,num2){
    return num1+num2;
}
function multiply(num1,num2){
    return num1*num2;
}
// The test method should start with test without parameters, and jsUnit will automatically identify the test method that starts with test and run it.
function testAdd() {
    result = add(1,1);
    debug("add(1,1)="+result);
    assertEquals("1+1=2", 2, add(1,1));
}
// Invalid test methods
function multiplyTest(){
    result = multiply(2,3);
    debug("multiply(2,3)="+result);
        assertEquals("2*3=6", 5, multiply(2,3));
}
// If you cannot find a test method or if the method to be tested does not start with a standard test, use the following method to specify manually
function exposeTestFunctionNames() { 
    var tests = new Array(2); 
    tests[0] = "multiplyTest"; 
    tests[1] = "testAdd"; 
    return tests; 
}
// setUp() is called before each test
function setUp(){
    info("-->result="+result);
    result = 0;
    info("test fixture, initialization ready state - > result=" +result);
}

// tearDown() is called after each test
function tearDown(){
    result = null;
    warn("Clear or reset test fixture - > result="+result);
}

// A one-time start method called before all test functions (including setUp()) are invoked, and the idea is that there can be no log operation in this method
function setUpPage(){
    result = 100;
    // This must be set after completion, otherwise it will be blocked.  
        setUpPageStatus = "complete"; 
}
</script> 
</HEAD> 

<BODY> 
test page for testValidArgs(); 
</BODY> 
</HTML> 



Open testRunner.html, fill in your test01.html path in the file, select debug log level, click Run, the test results appear, click error information will pop up specific information:

The log information that pops up after clicking Run is as follows:

Batch testing:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>testSuite.html</title>  
  5.     <script language="JavaScript" src="../app/jsUnitCore.js"></script>  
  6.     <script type="text/javascript">  
  7.         function sampleSuite(){  
  8.             var sampleSuite = new JsUnitTestSuite();  
  9.             //Add test pages to the test suite
  10.             sampleSuite.addTestPage("tests/taobao_test.html");  
  11.             return sampleSuite;  
  12.         }  
  13.         //jsUnit must provide a method named suite() in order to identify the test set and test automatically.
  14.         function suite(){  
  15.             var testSuite = new JsUnitTestSuite();  
  16.             //Adding other test sets to the test set
  17.             testSuite.addTestSuite(sampleSuite());  
  18.             testSuite.addTestPage("tests/ShopexCore_test.html");  
  19.             return testSuite;  
  20.         }  
  21.     </script>  
  22.   </head>  
  23.     
  24.   <body>  
  25.     This is a simple test suite.   
  26.   </body>  
  27. </html>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>testSuite.html</title>
    <script language="JavaScript" src="../app/jsUnitCore.js"></script>
    <script type="text/javascript">
        function sampleSuite(){
            var sampleSuite = new JsUnitTestSuite();
            // Adding test pages to the test suite
            sampleSuite.addTestPage("tests/taobao_test.html");
            return sampleSuite;
        }
        // jsUnit must provide a method called suite() to identify the test set and test automatically
        function suite(){
            var testSuite = new JsUnitTestSuite();
            // Adding other test sets to the test set
            testSuite.addTestSuite(sampleSuite());
            testSuite.addTestPage("tests/ShopexCore_test.html");
            return testSuite;
        }
    </script>
  </head>

  <body>
    This is a simple test suite. 
  </body>
</html>




Posted by sane993 on Sun, 30 Jun 2019 12:20:49 -0700