javascript QUnit 单元测试
2013-06-29 17:23
141 查看
<!doctype html> <html> <head lang="zh-CN" dir="ltr"> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <!--qunit start--> <h1 id="qunit-header">QUnit Test Suite</h1> <h2 id="qunit-banner"></h2> <div id="qunit-testrunner-toolbar"></div> <h2 id="qunit-userAgent"></h2> <ol id="qunit-tests"></ol> <!--qunit end--> <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css"> <script type="text/javascript" src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script> <script type="text/javascript" src="demo.js"></script> </body> </html>
test( "ok test", function() { ok( true, "true succeeds" ); ok( "non-empty", "non-empty string succeeds" ); ok( false, "false fails" ); ok( 0, "0 fails" ); ok( NaN, "NaN fails" ); ok( "", "empty string fails" ); ok( null, "null fails" ); ok( undefined, "undefined fails" ); }); test( "strictEqual test", function() { strictEqual( 1, 1, "1 and 1 are the same value and type" ); }); test( "a test", function() { notStrictEqual( 1, "1", "String '1' and number 1 don't have the same value" ); }); test( "throws", function() { function CustomError( message ) { this.message = message; } CustomError.prototype.toString = function() { return this.message; }; throws( function() { throw "error" }, "throws with just a message, no expected" ); throws( function() { throw new CustomError(); }, CustomError, "raised error is an instance of CustomError" ); throws( function() { throw new CustomError("some error description"); }, /description/, "raised error message contains 'description'" ); }); asyncTest( "asynchronous test: one second later!", function() { expect( 1 ); setTimeout(function() { ok( true, "Passed and ready to resume!" ); start(); }, 1000); }); test( "a test", function() { stop(); asyncOp(); setTimeout(function() { equals( asyncOp.result, "someExpectedValue" ); start(); }, 150 ); }); test( "a test", function() { stop(); asyncOp(); setTimeout(function() { equals( asyncOp.result, "someExpectedValue" ); start(); }, 150 ); }); QUnit.done(function( details ) { console.log( "Total: ", details.total, " Failed: ", details.failed, " Passed: ", details.passed, " Runtime: ", details.runtime ); }); asyncTest( "asynchronous test: one second later!", function() { expect( 1 ); setTimeout(function() { ok( true, "Passed and ready to resume!" ); start(); }, 1000); }); asyncTest( "asynchronous test: video ready to play", 1, function() { var $video = $( "video" ); $video.on( "canplaythrough", function() { ok( true, "video has loaded and is ready to play" ); start(); }); }); test( "a test", function() { expect( 2 ); function calc( x, operation ) { return operation( x ); } var result = calc( 2, function( x ) { ok( true, "calc() calls operation function" ); return x * x; }); equal( result, 4, "2 square equals 4" ); }); test( "a test", 2, function() { function calc( x, operation ) { return operation( x ); } var result = calc( 2, function( x ) { ok( true, "calc() calls operation function" ); return x * x; }); equal( result, 4, "2 square equals 4" ); }); module( "group a" ); test( "a basic test example", function() { ok( true, "this test is fine" ); }); test( "a basic test example 2", function() { ok( true, "this test is fine" ); }); module( "group b" ); test( "a basic test example 3", function() { ok( true, "this test is fine" ); }); test( "a basic test example 4", function() { ok( true, "this test is fine" ); });
相关文章推荐
- FireUnit:基于Firebug的JavaScript单元测试扩展
- javaScript的单元测试框架Unit.js介绍
- javascript 单元测试 (Jsunit应用) 转
- javaScript的单元测试框架Unit.js介绍
- 引用单元测试模块unittest框架(三)
- play+angularjs+karma unit test(单元测试)
- 《Pragmatic Unit Testing In Java with JUnit》—单元测试之道读后感
- Python单元测试框架unittest简介
- Javascript单元测试框架QUnitjs详细介绍
- javascript单元测试及框架介绍
- Python unittest单元测试框架
- javascript单元测试
- javascript之QUnit单元测试
- iOS进行单元测试OCUnit+xctool
- Python之自动单元测试之一(unittest使用实例)
- JavaScript单元测试
- JS单元测试简单的项目实例(mocha+unit.js+istanbul)
- 【python单元测试unittest】加载测试套的代码修正
- 使用Android Studio进行本地单元测试(Local Unit Tests)