リトルJsユニット


まずはテストするjsコードです.
function add(num11, num22){
	return num11 + num22;
}
function sbutract(num11, num22){
	return num11 - num22;
}
function multiply(num11, num22){
	return num11 * num22;
}

function addNumbers(){
	var v1 = document.getElementById('value11').value;
	var v2 = document.getElementById('value22').value;
	v1 = parseInt(v1);
	v2 = parseInt(v2);
	v3 = v1 + v2;
	var v3 = v1 + v2;
	document.getElementById('value33').value = v3;
}
そしてJs Unitを使ってユニットテストを行います.js Unit_.testJS.
<script type="text/javascript" src="../app/jsUnitCore.js"></script>
<script type="text/javascript" src="mycode.js"></script>

<script type="text/javascript">
	//JavaScript    : JavaScript                     
	var count = 0;
	
	//JUnit   000,jsUnit    012
	//  jsUnit  , setUp tearDown   JUnit         
	//JUnit  setUp tearDown        ,                   
	//jsUnit                    ,  setUp tearDown            
	function setUp(){
		alert(count++);
		alert('setUp invoked');
		//      :      addNumbers()     
		document.getElementById('value11').value = 6;
		document.getElementById('value22').value = 9;
	}
	function tearDown(){
		alert('tearDown invoked');
		//      
		document.getElementById('value11').value = '';
		document.getElementById('value22').value = '';
		document.getElementById('value33').value = '';
	}
	
	//        ,           。   JUnit4.x  @BeforeClass
	// jsUnit      @AfterClass     
	function setUpPage(){
		alert('setUpPage invoked');
		//       setUpPage()      ,    jsUnit:        
		setUpPageStatus = 'complete';
	}

	//       (Test Function)
	//jsUnit           , JUnit3.8  
	//        test   
	function testAdd(){
		var result = add(1, 2);
		assertEquals(3, result);
	}
	function testSbutract(){
		var result = sbutract(1, 2);
		assertEquals(-1, result);
	}
	function testMultiply(){
		var result = multiply(1, 2);
		assertEquals(2, result);
	}
	
	//jsUnit    
	//		1) jsUnitCore.js        
	//		2)        js     
	//		3)  jsUnit      (%JSUNIT_HOME%/testRunner.html)
	//		4) file:///           
	//                    :F:\Note\jsunit\tests\jsUnit_testSuite.html
	//               ,          ,        JsUnit    tests   ,   ,  
	//		5)  Run    
	function testAddNumbers(){
		addNumbers();
		assertEquals('15', document.getElementById('value33').value);
	}
</script>

<input type="text" id="value11"><br/>
<input type="text" id="value22"><br/>
<input type="text" id="value33"><br/>
<input type="button" value="Add" onclick="addNumbers();">
最後はJs Unitのテストキットを使ったjs Unit_です.test Suite.
<script type="text/javascript" src="../app/jsUnitCore.js"></script>

<script type="text/javascript">
	//        
	function mySuite(){
		var testSuite = new top.jsUnitTestSuite();
		//                    ,      
		testSuite.addTestPage('tests/jsUnit_testJS.html');
		testSuite.addTestPage('tests/jsUnit_testJS.html');
		return testSuite;
	}
	
	//jsUnit         suite()
	function suite(){
		var testSuite = new top.jsUnitTestSuite();
		testSuite.addTestPage('tests/jsUnit_testJS.html');
		testSuite.addTestSuite(mySuite()); //            
		return testSuite;
	}
</script>