BDD:Behaviour Driven Development 行为驱动开发
TDD:Test Driven Development 测试驱动开发
1、单元测试
2、e2e测试
3、视觉回归测试
4、性能测试
在此之前,自己没有在任何项目中使用过自动化测试,主要是工作内容迭代快周期短,完全没有时间去写测试用例,开发完成后就直接移交给测试同学进行测试,他们也是人工完成测试。但自动化的好处就在于,只要用例覆盖的全面,就能大大提高代码质量,减少一些容易忽略的bug,做完迭代修改,跑一遍测试用例,轻松做完回归,也能让自己放心不是。而且对于项目以后的维护和交接都提供了保障作用。
这里简单介绍下karma工具的入门使用,首先保证有node环境,然后就是一顿插件要装
npm install karma-cli -g // 安装全局karma命令
// 安装jasmine断言库,无界面浏览器phantomJs,开发环境即可
npm install karma jasmine-core karma-jasmine karma-phantomjs-launcher -D
初始化karma
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
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 any browsers automatically ?
Press tab to list possible options. Enter empty string to move to the next question.
> PhantomJS
>
What is the location of your source and test files ?
You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
Enter empty string to move to the next question.
>
Should any of the files included by the previous patterns be excluded ?
You can use glob patterns, eg. "**/*.swp".
Enter empty string 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.
> no
初始化完成后,会生成一个karma.conf.js的文件,这就是karma的配置文件,下面需要对配置文件做一个简单的修改
// Karma configuration
// Generated on Tue May 28 2019 00:10:18 GMT+0800 (GMT+08:00)
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: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'./src/*.js',
'./test/*.spec.js'
],
// list of files / patterns 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: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// 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
})
}
在karma.conf.js同一目录下创建src和test文件夹,这里的路径与配置文件中file属性配置的路径一致就可以,然后在src中创建index.js,在test中创建index.spec.js
index.js代码如下:
function test (flag) {
if (flag === 1) {
return 'hello world';
} else {
return 'hello kitty';
}
}
function sum (x, y) {
return x + y;
}
index.spec.js的代码如下:
describe('index中包含的方法测试', function(){
it('test方法测试', function(){
expect(test(1)).toBe('hello world');
expect(test(2)).toBe('hello kitty');
});
it('sum方法测试', function() {
expect(sum(1, 2)).toBe(3);
});
})
然后运行karma start或者把命令集成到package.json中
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"unit": "karma start"
},
就可以运行npm run unit启动测试,可以得到下面的结果
karma start
28 05 2019 00:29:14.510:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
28 05 2019 00:29:14.518:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited
28 05 2019 00:29:14.527:INFO [launcher]: Starting browser PhantomJS
28 05 2019 00:29:20.933:INFO [PhantomJS 2.1.1 (Windows 8.0.0)]: Connected on socket 19EZePoOha6FPj8uAAAA with id 92401631
PhantomJS 2.1.1 (Windows 8.0.0): Executed 2 of 2 SUCCESS (0.005 secs / 0.003 secs)
TOTAL: 2 SUCCESS
可以看见,两个测试用例都验证成功了
这里只是一个简单的入门介绍,想要尝试在自己的项目中使用,可以去github上看看,推荐看看这个(https://github.com/karma-runner/karma)
另关于jasmine断言的语法,大家可以去官网看看,我找了另一个博主的总结,觉得挺全面的,果断引用jasmine语法