JavaScript自動集成化測試

觀察程序員把時間耗在哪里,其實編碼只占據(jù)很小的一部分,最多的時間則花在了調(diào)試上,有時甚至為了找出一個小問題不惜花費好幾個小時來調(diào)試定位。如果在編碼開始就為每一個功能編寫相應(yīng)測試,雖然每次需要花費額外的時間精力來編寫測試,但一旦測試代碼運行正常,工作就可以結(jié)束了,不需要再額外花費時間到調(diào)試代碼上。因此,引入單元測試是有必要的?(實際開發(fā)中我其實沒見過誰寫過測試:joy:)

單元測試是什么?

在計算機編程中,單元測試(英語:Unit Testing)又稱為模塊測試, 是針對程序模塊(軟件設(shè)計的最小單位)來進行正確性檢驗的測試工作。 程序單元是應(yīng)用的最小可測試部件。

Node.js 單元測試框架

比較受歡迎的單元測試框架有Jasmine,Mocha,Jest,AVA,Tape。Jasmine開箱即用,無需斷言庫;Mocha應(yīng)該是使用人數(shù)最多的框架,需要導(dǎo)入其他庫來實現(xiàn)斷言功能;Jest廣泛應(yīng)用于測試React應(yīng)用;AVA利用JS異步特性和并發(fā)運行測試,性能有所提高;Tape比較小,搭建運行比較簡潔,無需過多的配置,支持TypeScript/CoffeeScript/ES6。本文主要介紹下Mocha。

斷言庫

由于Mocha本身沒有斷言功能,需要選擇一款斷言庫搭配使用。與Mocha搭配的斷言庫主要有5種(should.js/chai/expect.js/better-assert/unexpected),本文使用的是TJ大神的should.js,該斷言庫更加類似于自然語言的寫法。

Mocha測試腳本基本結(jié)構(gòu)

  • describe 測試套件(test suite)一組相關(guān)的測試
  • it 測試用例(test case)測試的最小單元
    測試腳本應(yīng)包括一個或多個describe,每個describe塊應(yīng)包括一個或多個it塊

Mocha用法

命令行執(zhí)行測試腳本:

$ mocha 'filename.{test|spec}.js'
指定測試腳本時,可以使用通配符,同時指定多個文件(支持node與shell通配符)

瀏覽器執(zhí)行測試腳本:

執(zhí)行$ mocha init生成index.html及配套css、js,添加mocha.setup配置并引入should.js及測試腳本,末尾添加mocha.run(),瀏覽器打開index.html就可以看到測試腳本的執(zhí)行結(jié)果

Mocha命令行參數(shù)

Usage: mocha [debug] [options] [files]

Options:

-V, --version                           output the version number
-A, --async-only                        force all tests to take a callback (async) or return a promise
-c, --colors                            force enabling of colors
-C, --no-colors                         force disabling of colors
-G, --growl                             enable growl notification support
-O, --reporter-options <k=v,k2=v2,...>  reporter-specific options
-R, --reporter <name>                   specify the reporter to use (default: spec)
-S, --sort                              sort test files
-b, --bail                              bail after first test failure
-d, --debug                             enable node's debugger, synonym for node --debug
-g, --grep <pattern>                    only run tests matching <pattern>
-f, --fgrep <string>                    only run tests containing <string>
-gc, --expose-gc                        expose gc extension
-i, --invert                            inverts --grep and --fgrep matches
-r, --require <name>                    require the given module
-s, --slow <ms>                         "slow" test threshold in milliseconds [75]
-t, --timeout <ms>                      set test-case timeout in milliseconds [2000]
-u, --ui <name>                         specify user-interface (bdd|tdd|qunit|exports) (default: bdd)
-w, --watch                             watch files for changes
--check-leaks                           check for global variable leaks
--full-trace                            display the full stack trace
--compilers <ext>:<module>,...          use the given module(s) to compile files (default: )
--debug-brk                             enable node's debugger breaking on the first line
--globals <names>                       allow the given comma-delimited global [names] (default: )
--es_staging                            enable all staged features
--harmony<_classes,_generators,...>     all node --harmony* flags are available
--preserve-symlinks                     Instructs the module loader to preserve symbolic links when resolving and caching modules
--icu-data-dir                          include ICU data
--inline-diffs                          display actual/expected differences inline within each string
--no-diff                               do not show a diff on failure
--inspect                               activate devtools in chrome
--inspect-brk                           activate devtools in chrome and break on the first line
--interfaces                            display available interfaces
--no-deprecation                        silence deprecation warnings
--exit                                  force shutdown of the event loop after test run: mocha will call process.exit
--no-timeouts                           disables timeouts, given implicitly with --debug
--no-warnings                           silence all node process warnings
--opts <path>                           specify opts path (default: test/mocha.opts)
--perf-basic-prof                       enable perf linux profiler (basic support)
--napi-modules                          enable experimental NAPI modules
--prof                                  log statistical profiling information
--log-timer-events                      Time events including external callbacks
--recursive                             include sub directories
--reporters                             display available reporters
--retries <times>                       set numbers of time to retry a failed test case
--throw-deprecation                     throw an exception anytime a deprecated function is used
--trace                                 trace function calls
--trace-deprecation                     show stack traces on deprecations
--trace-warnings                        show stack traces on node process warnings
--use_strict                            enforce strict mode
--watch-extensions <ext>,...            specify extensions to monitor with --watch (default: js)
--delay                                 wait for async suite definition
--allow-uncaught                        enable uncaught errors to propagate
--forbid-only                           causes test marked with only to fail the suite
--forbid-pending                        causes pending tests and test marked with skip to fail the suite
--file <file>                           include a file to be ran during the suite (default: )
--exclude <file>                        a file or glob pattern to ignore (default: )
-h, --help                              output usage information

Commands:

init <path>  initialize a client-side mocha setup at <path>

Mocha異步測試

callback方式

it塊執(zhí)行時需要傳入done參數(shù),當(dāng)測試結(jié)束時,必須顯式調(diào)用這個done函數(shù),告訴Mocha測試結(jié)束了,否則,Mocha會一直等到超時報錯。

Promise

Mocha內(nèi)置對Promise的支持,允許直接返回Promise,等到它的狀態(tài)改變,再執(zhí)行斷言,而不用顯式調(diào)用done函數(shù)

Mocha提供的鉤子及測試腳本管理

鉤子:

  • before 在該test suite內(nèi)所有test case之前運行
  • after 在該test suite內(nèi)所有test case之后運行
  • beforeEach 在該test suite內(nèi)每個test case之后運行
  • afterEach 在該test suite內(nèi)每個test case之后運行

腳本管理:

  • describe.only/it.only 只運行帶only的test suite/test case
  • describe.skip/it.skip 跳過帶skip的test suite/test case

參考:
Mocha 文檔
阮一峰Mocha教程

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容