@king-
2016-11-24T10:32:05.000000Z
字数 3527
阅读 1378
前端测试
//安装npm install -g karma//测试安装是否成功karma start
需包含的modules
module 之间是存在依赖关系的,当安装了某个模块的同时,如果存在依赖关系则会同时自动下载相关的依赖包
- jasmine-core
- karma-jasmine
通过方向键选择相关内容
//执行karma初始化> karma initWhich testing framework do you want to use ?Press tab to list possible options. Enter to move to the next question.> jasmineDo 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.> noDo you want to capture a browser automatically ?Press tab to list possible options. Enter empty string to move to the next question.> Chrome>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.> yes
// Karma configuration// Generated on Tue Sep 22 2015 14:13:42 GMT+0800 (中国标准时间)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-adapterframeworks: ['jasmine'],// list of files / patterns to load in the browserfiles: [],// list of files to excludeexclude: [],// preprocess matching files before serving them to the browser// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessorpreprocessors: {},// test results reporter to use// possible values: 'dots', 'progress'// available reporters: https://npmjs.org/browse/keyword/karma-reporterreporters: ['progress'],// web server portport: 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_DEBUGlogLevel: config.LOG_INFO,// enable / disable watching file and executing tests whenever any file changesautoWatch: true,// start these browsers// available browser launchers: https://npmjs.org/browse/keyword/karma-launcherbrowsers: ['Chrome'],// Continuous Integration mode// if true, Karma captures browsers, runs the tests and exitssingleRun: false})}
npm install karma-jasmine
function reverse(name){return name.split("").reverse().join("");}
describe("A suite of basic functions", function() {it("reverse word",function(){expect("DCBA").toEqual(reverse("ABCD"));});});
配置files和exclude,指定配置文件和测试文件
module.exports = function (config) {config.set({basePath: '',frameworks: ['jasmine'],files: ['*.js'],exclude: ['karma.conf.js'],reporters: ['progress'],port: 9876,colors: true,logLevel: config.LOG_INFO,autoWatch: true,browsers: ['Chrome'],captureTimeout: 60000,singleRun: false});};
//如果不指定测试配置文件,则只能启动karma,并不知道将执行何种测试用例karma start karma.conf.js
在测试的同时,我们也需要了解,我们测试用例的代码覆盖率情况
需要的相关包
- jasmine-core
- karma
- karma-chrome-launcher
- karma-coverage
- karma-jasmine
npm install karma-coverage
npm install karma
npm install karma-chrome-launcher
添加 coverage 相关配置
module.exports = function(config) {config.set({basePath: '',frameworks: ['jasmine'],files: ['*.js'],exclude: ['karma.conf.js'],reporters: ['progress','coverage'],// coverage新增配置内容preprocessors : {'src.js': 'coverage'},// coverage新增配置内容// coverage新增配置内容coverageReporter: {type : 'html',dir : 'coverage/'},port: 9876,colors: true,logLevel: config.LOG_INFO,autoWatch: true,browsers: ['Chrome'],captureTimeout: 60000,singleRun: false});}
karma start karma.conf.js
在这里,覆盖报告是在正常的jasmine测试结果后分析得到的,所以和之前的执行是一样的。
当jasmine测试结束后,将在配置文件中设置的dir : coverage/doverage文件生成报告