單元測試: 以軟件的單元爲單位,對軟件進行測試javascript
步驟html
Selenium
工具自動打開瀏覽器測試端對端測試: 全鏈路測試,從開始端到結束端的測試java
冒煙測試: 正式的全面測試開始以前,對主要功能進行的預測試node
TDD(Test-Driven Development): 測試驅動開發git
步驟github
提供4個方法shell
suite()
test()
setup()
teardown()
BDD(Behavior-Driven Development): 行爲驅動開發數據庫
提供6個方法npm
describle()
it()
before()
after()
beforeEach()
afterEach()
mocha
test
目錄下第一層的腳本--recursive
會運行當前目錄以及子目錄的腳本shell通配符api
mocha spec/{one,two}.js mocha test/unit/*.js mocha test/{,**/}*.{js,jsx}
node通配符
mocha 'test/**/*.@(js|jsx)'
--help
, -h
: 查看全部命令參數--reporter
, -R
: 指定測試報告格式,官方詳細介紹
spec
: 默認格式tap
: ``mochawesome
: 以HTML格式報告生成不一樣格式的報告
生成markdown格式的報告
mocha --recursive -R markdown > spec.md
生成html格式的報告
mocha -recursive -R doc > spec.html
--growl
, -G
: 測試結果在桌面顯示--watch
, -w
: 監視指定測試腳本,有變化會自動運行mocha
--bail
, -b
: 指定只要有一個測試沒有經過,就中止執行後面的測試用例--grep
, -g
: 用於搜索測試用例的名稱--invert
, -i
: 只運行不符合條件的測試腳本, 必須與--grep
配合使用
mocha --grep "1 加 1" --invert
命令行
mocha --recursive --reporter tap --growl
配置項mocha.opts
文件
--reporter tap --recursive --growl
指定測試目錄mocha.opts
文件
server-tests --recursive
ES6
依賴包安裝
npm i babel-polyfill --save npm i @babel/core @babel/preset-env -D
.babelrc
配置
{ "presets": [ ["@babel/env", { "modules": false, "useBuiltIns": "usage", "corejs": 2, "shippedProposals": true }] ] }
指定轉換器
ES6
npx mocha --compilers js:@babel/core/register
CoffeScript
npx mocha --compilers coffee:coffee-script/register
延時執行done
it('延時1000毫秒', function(done) { setTimeout(() => { done() }, 1e3) }) it('請求以後執行', function() { request .get('https://api.github.com') .end(function(err, res) { expect(res).to.be.an('object') done() }) })
返回一個promise
it('請求以後執行', function() { return request .get('https://api.github.com') .end(function(err, res) { expect(res).to.be.an('object') }) })
經過命令行
mocha -t 1000
: 設置超時時間未1000毫秒mocha -s 1000
: 設置測試延時1000毫秒執行before
after
beforeEach
afterEach
only
只運行某個用例mocha init test
新建add.js
function add(x, y) { return x + y; }
把add.js
和chai.js
加入index.html
<script> mocha.setup('bdd'); </script> <script src="add.js"></script> <script src="http://chaijs.com/chai.js"></script> <script src="tests.js"></script> <script> mocha.run(); </script>
新建tests.js
var expect = chai.expect; describe('加法函數的測試', function() { it('1 加 1 應該等於 2', function() { expect(add(1, 1)).to.be.equal(2); }); it('任何數加0等於自身', function() { expect(add(1, 0)).to.be.equal(1); expect(add(0, 0)).to.be.equal(0); }); });