以上为我的对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任务会一直执行。
以上所述为第一次探索自动化测试的过程,确实学习到了不少东西,可是不少高级的用法并不太了解,也有不足的地方没有接入到真实项目中配合使用。