目的
- 如何使用
SuperTest測試框架,進(jìn)行API測試 - 如何將API測試與構(gòu)建工具結(jié)合
- 如何將API測試、構(gòu)建工具與持續(xù)集成結(jié)合
SuperTest
什么是SuperTest
-
To provide a high-level abstraction for testing HTTP,提供一個(gè)高級別的HTTP測試 - Github地址:SuperTest
如何安裝
- 命令
npm install supertest --save-dev
- 樣例
describe('Test Demo.', function() {
it('Visit URL', function(done) {
request.get('')
.expect(200)
.end(done);
});
});
- ?樣例原理:通過獲取請求的
結(jié)果,對請求結(jié)果進(jìn)行驗(yàn)證。樣例中的驗(yàn)證條件為返回的狀態(tài)碼為200。
自動(dòng)化API測試:Grunt & Gulp
Grunt篇
什么是Grunt
-
The JavaScript Task Runner,JavaScript的構(gòu)建工具 - 官網(wǎng):Grunt
安裝
- 命令
npm install -g grunt-cli
功能分析
測試目的:?請求https://github.com/aimer1124/SuperTestWithGrunt是否能返回狀態(tài)碼200
使用Github來Clonehttps://github.com/aimer1124/SuperTestWithGrunt.git

fileTree.png
-
/test/module/demo.js:測試腳本
var config = require('../config/endpoints'),
request = require('supertest')(config.host[config.env]);
describe('Test Demo.', function() {
this.timeout(10000);
it('Visit ' + config.env, function(done) {
request.get('')
.expect(200)
.end(done);
});
});
-
/test/config/endpoints.js:環(huán)境配制
module.exports = {
host : {
master: 'https://github.com/aimer1124/SuperTestWithGrunt',
branch: 'https://github.com/aimer1124/SuperTestWithGrunt/tree/differentENV'
},
env: process.env.NODE_ENV || 'master'
};
-
Gruntfile.js:Grunt運(yùn)行時(shí)的命令配制 -
package.json:npm 安裝時(shí)所需要的包 -
results.txt:執(zhí)行結(jié)果存放文件
執(zhí)行
- 命令:
grunt - 運(yùn)行結(jié)果
? SuperTestWithGrunt git:(master) ? grunt
Running "mochaTest:test" (mochaTest) task
Test Demo.
? Visit master (1640ms)
1 passing (2s)
Done, without errors.
- 結(jié)果分析:
? Visit master (1640ms)表示測試正常通過;1 passing (2s)表示整個(gè)測試所執(zhí)行的時(shí)間和測試所執(zhí)行的數(shù)量
Gulp篇
什么是Gulp
-
Automate and enhance your workflow,自動(dòng)化并且增強(qiáng)你的工作流 - 官網(wǎng):http://gulpjs.com/
- 中文官網(wǎng):http://www.gulpjs.com.cn/
安裝
- 命令
npm install --global gulp-cli
功能分析
測試目的:?請求http://aimer1124.github.io/是否能返回狀態(tài)碼200
使用Github來Clonehttps://github.com/aimer1124/SuperTestWithGulp

SuperTestWithGulp.png
-
/test/config/endpoints.js:環(huán)境配制
var host = {
master: require('./master.js'),
branch: require('./branch.js')
};
var ENV;
module.exports = function(env) {
if (env) {
ENV = host[env];
return;
}
return ENV;
};
-
/test/config/master的具體配制
module.exports = {
url: 'http://aimer1124.github.io/',
name: 'master'
};
-
/test/module/test-demo.js:測試腳本
var data = require('../config/endpoints'),
request = require('supertest')(data().url);
describe('Test Demo.', function() {
this.timeout(10000);
it('Visit ' + data().url, function(done) {
request.get('')
.expect(200)
.end(done);
});
console.log('You are in ' + data().name);
});
-
gulpfile.js:Grunt運(yùn)行時(shí)的命令配制 -
package.json:npm 安裝時(shí)所需要的包 -
results.txt:執(zhí)行結(jié)果存放文件
執(zhí)行
- 命令
gulp master
- 結(jié)果
? SuperTestWithGulp git:(master) gulp master
[17:34:44] Using gulpfile ~/Downloads/SuperTestWithGulp/gulpfile.js
[17:34:44] Starting 'master'...
[17:34:44] Finished 'master' after 37 ms
You are in master
Test Demo.
? Visit http://aimer1124.github.io/ (502ms)
1 passing (506ms)
- 結(jié)果分析:
? Visit http://aimer1124.github.io/ (502ms)表示測試正常通過;1 passing (506ms)表示整個(gè)測試所執(zhí)行的時(shí)間和測試所執(zhí)行的數(shù)量
自動(dòng)化測試的持續(xù)集成
持續(xù)集成是什么
Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.
Travis CI
- 在線CI工具
- 官網(wǎng):https://travis-ci.org/
Travis與Gulp集成
- 使用Git項(xiàng)目SuperTestWithGulp
- 在項(xiàng)目根目錄中添加
.travis.yml文件,language表示使用的語言為node_js,0.12表示使用node_js的版本,before_script表示運(yùn)行腳本前執(zhí)行的腳本命令,script表示啟動(dòng)時(shí)的執(zhí)行腳本
language: node_js
node_js:
- "0.12"
before_script:
- npm install -g gulp
script: gulp master
- 在Travis中關(guān)聯(lián)此Github項(xiàng)目SuperTestWithGulp

GulpInTravis.png
- Travis會(huì)在Github代碼有
變更時(shí),自動(dòng)拉取項(xiàng)目的代碼并進(jìn)行在線集成

gulpWithTravis.png
Jenkins
Build great things at any scale- 官網(wǎng):https://jenkins.io/
Jenkins與Grunt集成
- 安裝
NodeJS、Git插件 - 配制
Job的build step中execute shell

ConfigScriptInJenkins.png
- 運(yùn)行
Job即可執(zhí)行API測試

LogInJenkins.png
總結(jié)
- API自動(dòng)化測試已經(jīng)說完了,完全沒有太復(fù)雜的代碼和編寫難度。
- 使用SuperTest可實(shí)現(xiàn)多場景、多環(huán)境的API場景測試,且執(zhí)行速度較UI自動(dòng)化測試快很多。
- SuperTest與Grunt/Gulp的集成很方便,即使在本地進(jìn)行調(diào)試也很快捷。
- 持續(xù)集成工具Travis/Jenkins,與API測試集成后,更高效的提高測試效率。
參考
- SuperTest
https://github.com/visionmedia/supertest - Grunt官網(wǎng)
http://gruntjs.com/ - NPM官網(wǎng)
https://www.npmjs.com/ - Gulp官網(wǎng)
http://gulpjs.com/ - Continuous Integration
https://www.thoughtworks.com/continuous-integration - Travis CI
https://travis-ci.org/ - Jenkins
https://jenkins.io/ - NodeJS
https://nodejs.org/en/ - UI自動(dòng)化測試
http://m.itdecent.cn/p/cb24e7fa8f56 - Jenkins with Grunt
http://aimer1124.github.io/2016/03/03/Tool-Jenkins-with-SuperTest-and-Grunt/