[关闭]
@king- 2016-11-24T18:32:05.000000Z 字数 3527 阅读 1300

karma + jasmine 自动化测试

前端测试


1. Karma 安装

  1. //安装
  2. npm install -g karma
  3. //测试安装是否成功
  4. karma start

2. Karma + Jasmine 配置

需包含的modules
module 之间是存在依赖关系的,当安装了某个模块的同时,如果存在依赖关系则会同时自动下载相关的依赖包

  • jasmine-core
  • karma-jasmine
  1. //执行karma初始化
  2. > karma init
  3. Which testing framework do you want to use ?
  4. Press tab to list possible options. Enter to move to the next question.
  5. > jasmine
  6. Do you want to use Require.js ?
  7. This will add Require.js plugin.
  8. Press tab to list possible options. Enter to move to the next question.
  9. > no
  10. Do you want to capture a browser automatically ?
  11. Press tab to list possible options. Enter empty string to move to the next question.
  12. > Chrome
  13. >
  14. What is the location of your source and test files ?
  15. You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
  16. Enter empty string to move to the next question.
  17. >
  18. Should any of the files included by the previous patterns be excluded ?
  19. You can use glob patterns, eg. "**/*.swp".
  20. Enter empty string to move to the next question.
  21. >
  22. Do you want Karma to watch all the files and run the tests on change ?
  23. Press tab to list possible options.
  24. > yes
  1. // Karma configuration
  2. // Generated on Tue Sep 22 2015 14:13:42 GMT+0800 (中国标准时间)
  3. module.exports = function(config) {
  4. config.set({
  5. // base path that will be used to resolve all patterns (eg. files, exclude)
  6. basePath: '',
  7. // frameworks to use
  8. // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
  9. frameworks: ['jasmine'],
  10. // list of files / patterns to load in the browser
  11. files: [
  12. ],
  13. // list of files to exclude
  14. exclude: [
  15. ],
  16. // preprocess matching files before serving them to the browser
  17. // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
  18. preprocessors: {
  19. },
  20. // test results reporter to use
  21. // possible values: 'dots', 'progress'
  22. // available reporters: https://npmjs.org/browse/keyword/karma-reporter
  23. reporters: ['progress'],
  24. // web server port
  25. port: 9876,
  26. // enable / disable colors in the output (reporters and logs)
  27. colors: true,
  28. // level of logging
  29. // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
  30. logLevel: config.LOG_INFO,
  31. // enable / disable watching file and executing tests whenever any file changes
  32. autoWatch: true,
  33. // start these browsers
  34. // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
  35. browsers: ['Chrome'],
  36. // Continuous Integration mode
  37. // if true, Karma captures browsers, runs the tests and exits
  38. singleRun: false
  39. })
  40. }
  1. npm install karma-jasmine
  1. function reverse(name){
  2. return name.split("").reverse().join("");
  3. }
  1. describe("A suite of basic functions", function() {
  2. it("reverse word",function(){
  3. expect("DCBA").toEqual(reverse("ABCD"));
  4. });
  5. });
  1. module.exports = function (config) {
  2. config.set({
  3. basePath: '',
  4. frameworks: ['jasmine'],
  5. files: ['*.js'],
  6. exclude: ['karma.conf.js'],
  7. reporters: ['progress'],
  8. port: 9876,
  9. colors: true,
  10. logLevel: config.LOG_INFO,
  11. autoWatch: true,
  12. browsers: ['Chrome'],
  13. captureTimeout: 60000,
  14. singleRun: false
  15. });
  16. };
  1. //如果不指定测试配置文件,则只能启动karma,并不知道将执行何种测试用例
  2. karma start karma.conf.js

3. karma和istanbul代码覆盖率

在测试的同时,我们也需要了解,我们测试用例的代码覆盖率情况

需要的相关包

  • jasmine-core
  • karma
  • karma-chrome-launcher
  • karma-coverage
  • karma-jasmine
  1. npm install karma-coverage
  1. npm install karma
  1. npm install karma-chrome-launcher
  1. module.exports = function(config) {
  2. config.set({
  3. basePath: '',
  4. frameworks: ['jasmine'],
  5. files: ['*.js'],
  6. exclude: ['karma.conf.js'],
  7. reporters: ['progress','coverage'],// coverage新增配置内容
  8. preprocessors : {'src.js': 'coverage'},// coverage新增配置内容
  9. // coverage新增配置内容
  10. coverageReporter: {
  11. type : 'html',
  12. dir : 'coverage/'
  13. },
  14. port: 9876,
  15. colors: true,
  16. logLevel: config.LOG_INFO,
  17. autoWatch: true,
  18. browsers: ['Chrome'],
  19. captureTimeout: 60000,
  20. singleRun: false
  21. });
  22. }
  1. karma start karma.conf.js

在这里,覆盖报告是在正常的jasmine测试结果后分析得到的,所以和之前的执行是一样的。
jasmine 测试结束后,将在配置文件中设置的 dir : coverage/ doverage文件生成报告

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注