JsUnit ( Jorg Schaible )
http://jsunit.berlios.de/
- jsunit_1-2.tar.gz
AllTests.html
AllTests.js
AllTests.wsh
D:\jsunit_1-2\tests>cscript AllTests.js
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
JavaScript compatibility:
has exceptions: true
exceptions working: true
JavaScript engine detection:
isBrowser: false
isJScript: true
isWSH: true
isIIS: false
isNSServer: false
isRhino: false
isMozillaShell: false
isKJS: false
isShell: true
isObtree: false
hasCallStackSupport: true
JsUnit Test Suite:
........................................
........................................
........................................
........
Time: 0.47
OK (128 tests)
D:\jsunit_1-2\samples>cscript AllTests.js
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
..........................F..
Time: 0.06
There was 1 failure:
1) Test SimpleTest.testAdd failed: AssertionFailedError: Expected:<6>, but was:<5>
FAILURES!!!
Tests run: 28, Failures: 1, Errors: 0
/* JsUnit - a JUnit port for JavaScript Copyright (C) 1999,2000,2001,2002,2003 Joerg Schaible This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation in version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Test suites built with JsUnit are derivative works derived from tested classes and functions in their production; they are not affected by this license. */ if( !this.JsUtil ) { var pathJsUtil = "lib/JsUtil.js"; if( this.WScript ) { var fso = new ActiveXObject( "Scripting.FileSystemObject" ); var file = fso.OpenTextFile( pathJsUtil, 1 ); var all = file.ReadAll(); file.Close(); eval( all ); } else load( pathJsUtil ); eval( JsUtil.prototype.include( "lib/JsUnit.js" )); } /** * Some simple tests. */ function SimpleTest(name) { TestCase.call( this, name ); } SimpleTest.prototype = new TestCase(); SimpleTest.prototype.setUp = function() { this.fValue1= 2; this.fValue2= 3; }; SimpleTest.prototype.testAdd = function() { var result = this.fValue1 + this.fValue2; // forced failure result == 5 this.assertEquals( 6, result ); }; SimpleTest.prototype.testDivideByZero = function() { var zero = 0; this.assertEquals( "Infinity", 8/zero ); }; SimpleTest.prototype.testAsserts = function() { this.assertTrue( true ); this.assertFalse( false ); this.assertEquals( 1, this.fValue2 - this.fValue1 ); this.assertNull( null ); this.assertNotNull( this.fValue1 ); this.assertUndefined(); this.assertNotUndefined( true ); this.assertSame( this, this ); this.assertNotSame( new Number( this.fValue1 ), new Number( this.fValue1 )); }; function SimpleTestSuite() { TestSuite.call( this, "SimpleTestSuite" ); this.addTestSuite( SimpleTest ); } SimpleTestSuite.prototype = new TestSuite(); SimpleTestSuite.prototype.suite = function() { return new SimpleTestSuite(); }; function AllTests() { TestSuite.call( this, "AllTests" ); } AllTests.prototype = new TestSuite(); AllTests.prototype.suite = function() { var suite = new AllTests(); suite.addTest( SimpleTestSuite.prototype.suite()); return suite; }; if( JsUtil.prototype.isShell ) { var args; if( this.WScript ) { args = new Array(); for( var i = 0; i < WScript.Arguments.Count(); ++i ) args[i] = WScript.Arguments( i ); } else if( this.arguments ) args = arguments; else args = new Array(); var result = TextTestRunner.prototype.main( args ); JsUtil.prototype.quit( result ); }
if( !this.JsUtil ) { var pathJsUtil = "lib/JsUtil.js"; if( this.WScript ) { var fso = new ActiveXObject( "Scripting.FileSystemObject" ); var file = fso.OpenTextFile( pathJsUtil, 1 ); var all = file.ReadAll(); file.Close(); eval( all ); } else load( pathJsUtil ); eval( JsUtil.prototype.include( "lib/JsUnit.js" )); }
function SimpleTest(name) { TestCase.call( this, name ); } SimpleTest.prototype = new TestCase(); SimpleTest.prototype.setUp = // snip. SimpleTest.prototype.testAdd =
function SimpleTestSuite() { TestSuite.call( this, "SimpleTestSuite" ); this.addTestSuite( SimpleTest ); } SimpleTestSuite.prototype = new TestSuite(); SimpleTestSuite.prototype.suite = function () { return new SimpleTestSuite(); };
function AllTests() { TestSuite.call( this, "AllTests" ); } AllTests.prototype = new TestSuite(); AllTests.prototype.suite = function() { var suite = new AllTests(); suite.addTest( SimpleTestSuite.prototype.suite()); return suite; };
if( JsUtil.prototype.isShell ) { var args; if( this.WScript ) { args = new Array(); for( var i = 0; i < WScript.Arguments.Count(); ++i ) args[i] = WScript.Arguments( i ); } else if( this.arguments ) args = arguments; else args = new Array(); var result = TextTestRunner.prototype.main( args ); JsUtil.prototype.quit( result ); }
assertTrue( String msg, String cond ) | Asserts that a condition is true. |
assertFalse( String msg, String cond ) | Asserts that a condition is false. |
assertEquals( String msg, Object expected, Object actual ) | Asserts that two values are equal. |
assertFloatEquals( String msg, Object expected, Object actual, Object tolerance ) | Asserts that two floating point values are equal to within a given tolerence. |
assertSame( String msg, Object expected, Object actual ) | Asserts that two values are the same. |
assertNull( String msg, Object object ) | Asserts that an object is null. |
assertUndefined( String msg, Object object ) | Asserts that an object is undefined. |
assertNotSame( String msg, Object expected, Object actual ) | Asserts that two values are not the same. |
assertNotNull( String msg, Object object ) | Asserts that an object is not null. |
assertNotUndefined( String msg, Object object ) | Asserts that an object is not undefined. |
fail( String msg, CallStack stack, String usermsg ) | Fails a test with a give message. |
setUp() | Set up the environment of the fixture. |
tearDown | Clear up the environment of the fixture. |
this.assertEquals( 1, 1.0 );
this.assertEquals( "1", 1 );
this.assertEquals( true, 1 );
this.assertEquals( false, 0 );
this.assertEquals( true, "1" );
this.assertEquals( false, "0" );
this.assertEquals( ["a"], "a" );
this.assertEquals( "a,b", ["a", "b"] );
this.assertEquals( "tostring", {toString:function(){return "tostring";}} );
this.assertEquals( [1], 1 );
this.assertEquals( function(){}, "function(){}" );
this.assertEquals( undefined, null );
this.assertEquals( /abc/, "abc" );
junction %JSUNIT_HOME%\lib lib
junction -d lib
<?XML version="1.0" standalone="yes" ?> <job id="JsUnitTest"> <script language="JScript" src="../lib/JsUtil.js" /> <script language="JScript" src="../lib/JsUnit.js" /> <script language="JScript"><![CDATA[ // JScript code ]]></script> </job>
vardump( iObject ) | iObject を文字列に変換して返す。型などの情報を追加している。 |
assertNotEquals( msg, expected, actual ) | assertNotEquals の否定版。 expected と actual が同じであるときログに記録する。 |
assertDeepEquals( msg, expected, actual ) | assertEquals より深く比較する。 expected に equals メソッドが定義されている場合はこれを利用する。 |
assertDeepNotEquals( msg, expected, actual ) | assertDeepEquals の否定版。 expected と actual が同じであるときログに記録する。 |
Assert_assertNaN( msg, object ) | object が NaN でないときログに記録する。 |
assertInstanceof( msg, klass, object ) | object が klass 一族ではないときログに記録する。 |
assertIn( msg, method, object ) | object に method が定義されていない場合にログに記録する。 |
assertMatch( msg, pattern, string ) | string が正規表現 pattern にマッチしないときログに記録する。 |
assertNoMatch( msg, pattern, string ) | assertMatch の否定版。 string が正規表現 pattern にマッチするときログに記録する。 |
assertThrows( msg, expectedSymbol, block ) | block を呼び出し、例外 expectedSymbol が送出されなかった場合にログに記録する。 |
assertNothingThrown( msg, block ) | block を呼び出し、例外が送出された場合にログに記録する。 |
assertBlock( msg, block ) | block が偽を返した場合にログに記録する。 |
TestSuiteCollection.declareTestCase( iName, iTestCase ) | iName の名前でテストケースを作成します。 iTestCase はプロパティで渡します。テストケースの作成と同時にテストスイートも作成します。テストスイートの名前は iName の最後に Suite を付加した名前になります。 |
<?XML version="1.0" standalone="yes" ?> <job id="JsUnitTest"> <script language="JScript" src="lib/JsUtil.js" /> <script language="JScript" src="lib/JsUnit.js" /> <script language="JScript" src="lib/JsUnitExtend.js" /> <script language="JScript" src="lib/TestSuiteCollection.js" /> <script language="JScript"><![CDATA[ TestSuiteCollection.declareTestCase( "SimpleTest", { test : function() { this.assertEquals( 1, 1 ); } } ); if( JsUtil.prototype.isShell ) { var args; if( this.WScript ) { args = new Array(); for( var i = 0; i < WScript.Arguments.Count(); ++i ) args[i] = WScript.Arguments( i ); } else if( this.arguments ) args = arguments; else args = new Array(); var result = TextTestRunner.prototype.main( args ); JsUtil.prototype.quit( result ); } ]]></script> </job>