API自動(dòng)化測試與持續(xù)集成

目的

  • 如何使用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

安裝

  • 命令
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

Travis與Gulp集成

  • 使用Git項(xiàng)目SuperTestWithGulp
  • 在項(xiàng)目根目錄中添加.travis.yml文件,language表示使用的語言為node_js0.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
GulpInTravis.png
  • Travis會(huì)在Github代碼有變更時(shí),自動(dòng)拉取項(xiàng)目的代碼并進(jìn)行在線集成
gulpWithTravis.png

Jenkins

Jenkins與Grunt集成

  • 安裝NodeJS、Git插件
  • 配制Jobbuild stepexecute 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測試集成后,更高效的提高測試效率。

參考

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

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

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