qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Javascript單元測試Unit Testing之QUnit

          QUnit是一個基于JQuery的單元測試Unit Testing 框架。雖然是基于JQuery但用來測試純Javascript代碼。
            用來運行Javascript單元測試用例的html頁面是這樣的:
          <!DOCTYPE html>
          <html>
          <head>
          <meta charset="utf-8">
          <title>QUnit test runner</title>
          <link rel="stylesheet" href="lib/qunit-1.10.0.css">
          </head>
          <body>
          <div id="qunit"></div>
          <div id="qunit-fixture"></div>
          <script src="lib/qunit-1.10.0.js"></script>
          <!--test code goes here-->
          </body>
          </html>
            假設我們有如下簡單的javascript代碼simpleMath.js,實現基本的數學操作,階乘,平均數。
          SimpleMath = function() {
          };
          SimpleMath.prototype.getFactorial = function (number) {
          if (number < 0) {
          throw new Error("There is no factorial for negative numbers");
          }
          else if (number == 1 || number == 0) {
          // If number <= 1 then number! = 1.
          return 1;
          } else {
          // If number > 1 then number! = number * (number-1)!
          return number * this.getFactorial(number-1);
          }
          }
          SimpleMath.prototype.signum = function (number) {
          if (number > 0)  {
          return 1;
          } else if (number == 0) {
          return 0;
          } else {
          return -1;
          }
          }
          SimpleMath.prototype.average = function (number1, number2) {
          return (number1 + number2) / 2;
          }
            如對getFactorial函數,我們可以編寫以下3種測試用例:
            正數,零,負數
          // Factorial testing module
          module("Factorial", {
          setup: function() {
          this.simpleMath = new SimpleMath();
          }, teardown: function() {
          delete this.simpleMath;
          }
          });
          test("calculating factorial for a positive number", function() {
          equal(this.simpleMath.getFactorial(3), 6, "Factorial of three must equal six");
          });
          test("calculating factorial for zero", function() {
          equal(this.simpleMath.getFactorial(0), 1, "Factorial of zero must equal one");
          });
          test("throwing an error when calculating the factorial for a negative number", function() {
          raises(function() {
          this.simpleMath.getFactorial(-10)
          }, "There is no factorial for negative numbers ...");
          });
            上面的代碼中,module中有setup, teardown可以讓我們事前事后做一些操作,使用斷言equal期望3的階乘結果是6,如果您有接觸過Java或C#平臺的單元測試,應該不能理解。然后我們把2個js腳本文件引入到上面html中
            <script src="src/simpleMath.js"></script>
            <script src="tests/simpleMathTest.js"></script>
            最終我們在瀏覽器中打開這個html,結果也就是顯示出來了。
            如上圖,我們看到7個測試用例,全部有通過了,點擊前2個用例,顯示出斷言的結果。常見的斷言還有ok( truthy [, message ] ), deepEqual( actual, expected [, message ] ),如下是ok:
            ok(true, "true passes");
            ok(4==4, "4 must equal 4");
            ok("some string", "Non-empty string passes");
            QUnit也有支持異步ajax的測試方法asyncTest,由于篇幅有限,在這兒不多介紹了。寫這篇文章的時候,QUnit是v1.14.0版本了。未來可能還有新版本。更多內容,請可以參考API文檔。
            希望對您軟件開發有幫助。

          posted on 2014-06-30 18:40 順其自然EVO 閱讀(215) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2014年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 河间市| 炎陵县| 项城市| 万载县| 启东市| 台南县| 札达县| 凤庆县| 繁峙县| 天等县| 桂东县| 赤壁市| 塘沽区| 阿城市| 汕头市| 固原市| 西丰县| 延津县| 黔西县| 玛纳斯县| 临江市| 海安县| 河间市| 布拖县| 廉江市| 株洲县| 敖汉旗| 贡山| 黄冈市| 大埔县| 定襄县| 那曲县| 汶川县| 苏尼特左旗| 宜黄县| 崇信县| 兴国县| 桃源县| 府谷县| 遵化市| 古浪县|