以上爲我的對Assert類的api理解,第三方斷言庫的語法基本和此接近,用法上可能更方便,語義化更好。javascript
mocha單元測試框架css
shouldBDD風格第三方斷言庫,mocha同時也支持其餘第三方斷言庫,選擇哪看我的喜愛html
根據mocha的Github文檔說明安裝mocha,java
npm install -g mocha
複製代碼
增長一個名爲mocha.opts的配置文件,文件內容爲node
--require should
複製代碼
可直接默認添加依賴,無需手動requiredjquery
提示:使用最新版本的mocha在package.json文件所在目錄下執行mocha命令不會加載opts文件,下降版本可解決這個問題git
如下代碼爲should文檔使用的例子github
引入should依賴後自動綁定should的api到Object.prototype原型上,若是是Object.create(null)建立的則使用should(params)來使用should的api,具體api參考should文檔web
var should = require('should');
var user = {
name: 'tj'
, pets: ['tobi', 'loki', 'jane', 'bandit']
};
user.should.have.property('name', 'tj');
user.should.have.property('pets').with.lengthOf(4);
// If the object was created with Object.create(null)
// then it doesn't inherit `Object.prototype`, so it will not have `.should` getter // so you can do: should(user).have.property('name', 'tj'); // also you can test in that way for null's
should(null).not.be.ok();
someAsyncTask(foo, function(err, result){
should.not.exist(err);
should.exist(result);
result.bar.should.equal(foo);
});
複製代碼
下面代碼爲一個測試用例,測試一個大數相加的函數,輸入與輸出是否一致:chrome
let add = require('../lib/add')
describe('大數相加add方法', function () {
it('字符串"42329"加上字符串"21532"等於"63861"', function () {
add('42329', '21532')
.should.equal('63861')
})
it('"843529812342341234"加上"236124361425345435"等於"1079654173767686669"', function () {
add('843529812342341234', '236124361425345435')
.should.equal('1079654173767686669')
})
})
複製代碼
describe()定義了一組測試用例,describe()內可重複嵌套describe(),第一個參數爲測試用例命名,第二個參數爲執行的函數
在項目目錄下運行mocha命令執行測試
Krama 一個基於Node.js的JavaScript測試執行過程管理工具
travis-CI 持續集成構建項目
首先根據krama Github文檔安裝
npm install -g krama
複製代碼
安裝完成後在咱們的項目中執行
karma init
複製代碼
執行命令後按照官網文檔說明初始化部分配置
$ karma init
// 選擇使用的測試框架
Which testing framework do you want to use?
Press tab to list possible options. Enter to move to the next question.
> jasmine
// 是否使用require.js
Do you want to use Require.js?
This will add Require.js plugin.
Press tab to list possible options. Enter to move to the next question.
> no
// 是否自動綁定瀏覽器
Do you want to capture a browser automatically?
Press tab to list possible options. Enter empty string to move to the next question.
> Chrome
> Firefox
>
// 輸入你的項目依賴源和測試的文件,一個一個輸入,最後生成一個數組
What is the location of your source and test files?
You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
Press Enter to move to the next question.
> *.js
> test/**/*.js
>
// 排除部分文件
Should any of the files included by the previous patterns be excluded?
You can use glob patterns, eg. "**/*.swp".
Press Enter to move to the next question.
>
// 監聽文件而且當文件改變時候執行測試
Do you want Karma to watch all the files and run the tests on change?
Press tab to list possible options.
> yes
複製代碼
最後本人項目生成的karma配置文件以下
// Karma configuration
// Generated on Tue Mar 12 2019 21:15:08 GMT+0800 (China Standard Time)
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha'],
// list of files / patterns to load in the browser
files: [
'https://cdn.bootcss.com/jquery/2.2.4/jquery.js',
'node_modules/should/should.js',
'test/**.js'
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
複製代碼
把咱們的項目接入travis-CI持續集成,首先登錄travis-CI網站www.travis-ci.org/
提示:須要有本身的github帳號,而且以github帳號受權登錄travis-ci網站
登錄後能夠看到本身github上全部項目倉庫,選擇你須要持續集成的項目倉庫
下面是簡單的配置文件說明
language: node_js
node_js:
- "lts/*"
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
複製代碼
language: 定義咱們測試的語言
node_js: 定義node的版本,能夠指定某個特定的版本
before_script:在script配置命令前執行
根據karma官網文檔說明接入travis-CI的配置裏說明karma-runner.github.io/1.0/plus/tr…
新增.travis.yml文件後並上傳到github項目倉庫中,在travis-ci網站中打開開關,travis-ci檢測到配置文件會自動執行測試命令
在job log標籤頁中能夠看到執行任務的日誌
根據任務日誌能夠看到咱們的測試任務執行狀況,4個測試用例均經過測試。
在集成travis-ci到項目中遇到了很多坑,翻了文檔看了不少配置,一開始覺得是chrome-launch的問題,在before_script中手動安裝也是報錯;另一開始是看到文檔裏是對firefox配置,覺得這是專門針對firefox的配置,後來照着karma接入travis-ci的文檔配置嘗試一下,竟然成功了,這結果來得太意外。
另外在karma.conf的文件中,singleRun的配置也須要配置成true,否則travis-ci任務會一直執行。
以上所述爲第一次探索自動化測試的過程,確實學習到了不少東西,可是不少高級的用法並不太瞭解,也有不足的地方沒有接入到真實項目中配合使用。