<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        詳細介紹NodeJs測試框架Mocha的安裝與使用

        來源:懂視網 責編:小采 時間:2020-11-27 20:23:22
        文檔

        詳細介紹NodeJs測試框架Mocha的安裝與使用

        詳細介紹NodeJs測試框架Mocha的安裝與使用:本文全面介紹如何使用Mocha,讓你輕松上手。如果你以前對測試一無所知,本文也可以當作http://www.gxlcms.com/wiki/48.html target=_blank
        推薦度:
        導讀詳細介紹NodeJs測試框架Mocha的安裝與使用:本文全面介紹如何使用Mocha,讓你輕松上手。如果你以前對測試一無所知,本文也可以當作http://www.gxlcms.com/wiki/48.html target=_blank

        本文全面介紹如何使用Mocha,讓你輕松上手。如果你以前對測試一無所知,本文也可以當作http://www.gxlcms.com/wiki/48.html" target="_blank">JavaScript單元測試入門。

        Mocha是運行在nodejs和瀏覽器下的Javascript的單元測試框架,相當的容易上手和好用,單元測試框架其實都差不多,基本都包含下面內容:

        用于寫測試用例的宏,屬性或者函數
        斷定庫, 用于測試是否可以通過
        輔助庫,如hook庫(測試前后調用某些函數或者方法),異常檢查(某些函數在某些參數的情況下拋出異常), 輸入組合(支持多排列的參數輸入組合)等。
        支持IDE的集成
        下面就按照官方文檔的順序來簡明扼要的

        安裝與初步的使用

        在控制臺窗口中執行下列命令:

        $ npm install -g mocha
        $ mk
        dir
         test
        $ $EDITOR test/test.js

        可以寫如下代碼:

        var assert = require('assert');
        describe('Array', function() {
         describe('#indexOf()', function () {
         it('should return -1 when the value is not present', function () {
         assert.equal(-1, [1,2,3].indexOf(5));
         assert.equal(-1, [1,2,3].indexOf(0));
         });
         });
        });

        回到控制臺:

        $ mocha
         .
         ? 1 test complete (1ms)

        這里mocha會查找當前文件目錄下test文件夾下的內容,自動執行。

        斷定庫

        這個是判定測試用例是否通過,默認下可以用nodejs的assert庫,與此同時,Mocha支持我們使用不同的斷定庫,現在可以支持下面的斷定庫,每個斷定庫的用法有一些差異,自己可以參考相應的文檔。

        1 should.js BDD style shown throughout these docs (BDD模式,本文檔用的都是這個斷定庫)
        2 better-assert) c-style self-documenting assert()(C-模型下的斷定庫)
        3 expect.js expect() style assertions (expect模式的斷定庫)
        4 unexpected the extensible BDD assertion toolkit
        5 chai expect(), assert() and should style assertions

        同步代碼

        同步代碼表示測試的是同步函數,上面的Array相關的例子代碼就是。這個比較好理解。

        異步代碼

        只所以有異步代碼測試,原因是在nodejs上許多異步函數,如下面的代碼中,只有done()函數執行完畢后,該測試用例才算完成

        describe('User', function() {
         describe('#save()', function() {
         it('should save without error', function(done) {
         var user = 
        new
         User('Luna');
         user.saveAsync(function(err) {
         
        if
         (err) throw err;
         done(); // 只有執行完此函數后,該測試用例算是完成。
         });
         });
         });
        });

        詳解describe和it

        上面的實例代碼比較簡單,那么什么是describe和it呢? 大致上,我們可以看出describe應該是聲明了一個TestSuit(測試集合) ,而且測試集合可以嵌套管理,而it聲明定義了一個具體的測試用例。 以bdd interface為例,具體的源代碼如下:

         /**
         * Describe a "suite" with the given `title`
         * and callback `fn` containing nested suites
         * and/or tests.
         */
         context.describe = context.context = function(title, fn) {
         var suite = Suite.create(suites[0], title);
         suite.file = file;
         suites.unshift(suite);
         fn.call(suite);
         suites.shift();
         return suite;
         };
         /**
         * Describe a specification or test-case
         * with the given `title` and callback `fn`
         * acting as a thunk.
         */
         context.it = context.specify = function(title, fn) {
         var suite = suites[0];
         if (suite.pending) {
         fn = null;
         }
         var test = new Test(title, fn);
         test.file = file;
         suite.addTest(test);
         return test;
         };

        Hooks(鉤子)

        實際上這個在寫unit test是很常見的功能,就是在執行測試用例,測試用例集合前或者后需要某個回調函數(鉤子)。Mocha提供了before(),after(), beforeEach() 和aftetEach(),示例代碼如下:

        describe('hooks', function() {
         before(function() {
         // runs before all tests in this block
         // 在執行所有的測試用例前 函數會被調用一次
         });
         after(function() {
         // runs after all tests in this block
         // 在執行完所有的測試用例后 函數會被調用一次
         });
         beforeEach(function() {
         // runs before each test in this block
         // 在執行每個測試用例前 函數會被調用一次
         });
         afterEach(function() {
         // runs after each test in this block
         // 在執行每個測試用例后 函數會被調用一次
         });
         // test cases
        });

        hooks還有下列其他用法:

        Describing Hooks - 可以對鉤子函數添加描述,能更好的查看問題
        Asynchronous Hooks (異步鉤子): 鉤子函數可以是同步,也可以是異步的,和測試用例一下,下面是異步鉤子的示例代碼:

         beforeEach(function(done) {
         // 異步函數
         db.
        clear
        (function(err) {
         if (err) return done(err);
         db.save([tobi, loki, jane], done);
         });
         });

        Root-Level Hooks (全局鉤子) - 就是在describe外(測試用例集合外)執行,這個一般是在所有的測試用例前或者后執行。
        Pending Tests (掛起測試)

        就是有一些測試,現在還沒有完成,有點類似TODO, 如下面的代碼:

        describe('Array', function() {
         describe('#indexOf()', function() {
         // pending test below 暫時不寫回調函數
         it('should return -1 when the value is not present');
         });
        });

        Exclusive Tests (排它測試)

        排它測試就是允許一個測試集合或者測試用例,只有一個被執行,其他都被跳過。如下面測試用例集合:

        describe('Array', function() {
         describe.only('#indexOf()', function() {
         // ...
         });
         // 測試集合不會被執行
         describe('#ingored()', function() {
         // ...
         });
        });

        下面是對于測試用例:

        describe('Array', function() {
         describe('#indexOf()', function() {
         it.only('should return -1 unless present', function() {
         // ...
         });
         // 測試用例不會執行
         it('should return the index when present', function() {
         // ...
         });
         });
        });

        需要說明的是,對于Hooks(回調函數)會被執行。

        Inclusive Tests(包含測試)

        與only函數相反,skip函數,將會讓mocha系統無視當前的測試用例集合或者測試用例,所有被skip的測試用例將被報告為Pending。
        下面是對與測試用例集合的示例代碼:

        describe('Array', function() {
         //該測試用例會被ingore掉 
         describe.skip('#indexOf()', function() {
         // ...
         });
         // 該測試會被執行
         describe('#indexOf()', function() {
         // ...
         });
        });

        下面例子是對具體的測試用例:

        describe('Array', function() {
         describe('#indexOf()', function() {
         // 測試用例會被ingore掉
         it.skip('should return -1 unless present', function() {
         // ...
         });
         // 測試用例會被執行
         it('should return the index when present', function() {
         // ...
         });
         });
        });

        Dynamically Generating Tests(動態生成測試用例)

        其實這個在很多其他的測試工具,如NUnit也會有,就是將測試用例的參數用一個集合代替,從而生成不同的測試用例。下面是具體的例子:

        var assert = require('assert');
        function add() {
         return Array.prototype.slice.call(arguments).reduce(function(prev, curr) {
         return prev + curr;
         }, 0);
        }
        describe('add()', function() {
         var tests = [
         {args: [1, 2], expected: 3},
         {args: [1, 2, 3], expected: 6},
         {args: [1, 2, 3, 4], expected: 10}
         ];
         // 下面就會生成三個不同的測試用例,相當于寫了三個it函數的測試用例。
         tests.forEach(function(test) {
         it('correctly adds ' + test.args.length + ' args', function() {
         var res = add.apply(null, test.args);
         assert.equal(res, test.expected);
         });
         });
        });

        Interfaces(接口)

        Mocha的接口系統允許用戶用不同風格的函數或者樣式寫他們的測試用例集合和具體的測試用例,mocha有BDD,TDD,Exports,QUnit和Require 風格的接口。

        BDD - 這個是mocha的默認樣式,我們在本文中的示例代碼就是這樣的格式。
        其提供了describe(), context(), it(), before(), after(), beforeEach(), and afterEach()的函數,示例代碼如下:

        describe('Array', function() {
         before(function() {
         // ...
         });
         describe('#indexOf()', function() {
         context('when not present', function() {
         it('should not throw an error', function() {
         (function() {
         [1,2,3].indexOf(4);
         }).should.not.throw();
         });
         it('should return -1', function() {
         [1,2,3].indexOf(4).should.equal(-1);
         });
         });
         context('when present', function() {
         it('should return the index where the element first appears in the array', function() {
         [1,2,3].indexOf(3).should.equal(2);
         });
         });
         });
         });

        TDD - 提供了 suite(), test(), suiteSetup(), suiteTeardown(), setup(), 和 teardown()的函數,其實和BDD風格的接口類似(suite相當于describe,test相當于it),示例代碼如下:

        suite('Array', function() {
         setup(function() {
         // ...
         });
         suite('#indexOf()', function() {
         test('should return -1 when not present', function() { 
         assert.equal(-1, [1,2,3].indexOf(4));
         });
         });
        });

        Exports - 對象的值都是測試用例集合,函數值都是測試用例。 關鍵字before, after, beforeEach, and afterEach 需要特別定義。
        具體的示例代碼如下:

        module.exports = {
         before: function() {
         // ...
         },
         'Array': {
         '#indexOf()': {
         'should return -1 when not present': function() {
         [1,2,3].indexOf(4).should.equal(-1);
         }
         }
         }
        };

        QUnit - 有點像TDD,用suit和test函數,也包含before(), after(), beforeEach()和afterEach(),但是用法稍微有點不一樣, 可以參考下面的代碼:

        function ok(expr, msg) {
         if (!expr) throw new Error(msg);
        }
        suite('Array');
        test('#length', function() {
         var arr = [1,2,3];
         ok(arr.length == 3);
        });
        test('#indexOf()', function() {
         var arr = [1,2,3];
         ok(arr.indexOf(1) == 0);
         ok(arr.indexOf(2) == 1);
         ok(arr.indexOf(3) == 2);
        });
        suite('String');
        test('#length', function() {
         ok('foo'.length == 3);
        });

        Require - 該接口允許我們利用require關鍵字去重新封裝定義 describe ,it等關鍵字,這樣可以避免全局變量。
        如下列代碼:

        var testCase = require('mocha').describe;
        var pre = require('mocha').before;
        var assertions = require('mocha').it;
        var assert = require('assert');
        testCase('Array', function() {
         pre(function() {
         // ...
         });
         testCase('#indexOf()', function() {
         assertions('should return -1 when not present', function() {
         assert.equal([1,2,3].indexOf(4), -1);
         });
         });
        });
        上述默認的接口是BDD, 如果想

        上述默認的接口是BDD, 如果想使用其他的接口,可以使用下面的命令行:

        mocha -ui 接口(TDD|Exports|QUnit...)

        Reporters (測試報告/結果樣式)

        Mocha 支持不同格式的測試結果暫時,其支持 Spec, Dot Matrix,Nyan,TAP…等等,默認的樣式為Spec,如果需要其他的樣式,可以用下列命令行實現:

        mocha --reporter 具體的樣式(Dot Matrix|TAP|Nyan...)

        Editor Plugins

        mocha 能很好的集成到TextMate,Wallaby.js,JetBrains(IntelliJ IDEA, WebStorm) 中,這里就用WebStorm作為例子。 JetBrains提供了NodeJS的plugin讓我們很好的使用mocha和nodeJs。 添加mocha 的相關的菜單,

        這里就可以直接在WebStorm中運行,調試mocha的測試用例了。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        詳細介紹NodeJs測試框架Mocha的安裝與使用

        詳細介紹NodeJs測試框架Mocha的安裝與使用:本文全面介紹如何使用Mocha,讓你輕松上手。如果你以前對測試一無所知,本文也可以當作http://www.gxlcms.com/wiki/48.html target=_blank
        推薦度:
        標簽: 介紹 js 的使用
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 在线免费观看一级片| 久久精品一本到99热免费| 最近中文字幕mv免费高清电影 | 四虎免费影院4hu永久免费| 国产亚洲精品VA片在线播放| 国产精品视频免费| 久久精品国产亚洲av麻豆蜜芽 | 深夜国产福利99亚洲视频| 黄网站色成年片大免费高清| 日产国产精品亚洲系列| aa级女人大片喷水视频免费| 久久精品国产精品亚洲精品| 精品熟女少妇av免费久久| 亚洲人成毛片线播放| 在线观看免费成人| 爱爱帝国亚洲一区二区三区| 亚洲乱码中文字幕综合234| a毛片免费观看完整| 亚洲视频欧洲视频| 四虎免费在线观看| 黄色视屏在线免费播放| 亚洲国产精品一区二区久久| 免免费国产AAAAA片| 亚洲AV永久无码天堂影院| 亚洲国产精品人人做人人爱| 99在线免费视频| 亚洲三级在线视频| 又粗又硬免费毛片| 日本免费电影一区二区| 国产精品高清视亚洲一区二区| 免费a级毛片无码av| 国产免费一区二区三区在线观看| 亚洲午夜久久久久久尤物| 四虎1515hm免费国产| 午夜爽爽爽男女免费观看影院| 亚洲日本久久一区二区va| 亚洲高清偷拍一区二区三区| 日本免费大黄在线观看| 亚洲成AV人片高潮喷水| 亚洲不卡中文字幕无码| 女人18毛片特级一级免费视频|