To be or not to be.That is a question!

---源于莎士比亚的《哈姆雷特》

导航

javascript QUnit 单元测试

<!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" );
});

 

posted on 2013-06-29 17:23  Ijavascript  阅读(285)  评论(0编辑  收藏  举报