詳解karma & jasmine自動化測試,
前端包管理工具
代碼重用和複用是快捷開發的一種重要方式,但是原始的代碼模組散佈於各個平台上,不好尋找,程式員對其進行有效管理也成為了一大難題。此時,依賴(包、外掛程式、工具都可以稱呼,本質是他人寫好封裝後的代碼模組)管理工具應需而生。依賴管理工具使用簡單的命令即可提供 依賴的尋找、安裝、卸載等操作,深受廣大程式員喜愛。
前端 Node.js 最為常用的依賴管理工具 是npm,npm 之於Node.js,就如?pip 之於 Python,gem 之於 Ruby,pear 之於 PHP , maven 之於Java 。
?Karma 環境的搭建
安裝 karma (karma用於run自動化測試指令碼)
npm install karma --save-dev
安裝karma-jasmine (jasmine用於編寫單元測試用例)
npm install karma-jasmine --save-devnpm install jasmine-core --save-dev
安裝karma-chrome-launcher(用於啟動chrome瀏覽器;如果是firefox可以使用karma-firefox-launcher;同理可得其他)
npm install karma-chrome-launcher --save-devnpm install karma-firefox-launcher --save-dev
安裝coverage(測試程式碼涵蓋範圍)
npm install karma-coverage --save-dev
?Jasmine
jasmine有四種類型的函數:
1. 分組 describe????
// 聲明一類測試案例describe('add algorithm',function(){ // 在裡面可以定義一些變數,如 var a=1,b=2;});2. 用例 it
// 聲明一類測試案例describe('add algorithm',function(){ // 在裡面可以定義一些變數,如 var a=1,b=2; // 聲明一種測試案例 it('test add one',function(){ }); it('test add two',function(){ });});3. 期望 expect4. 匹配to****
// 聲明一類測試案例describe('add algorithm',function(){ // 可以定義一些變數,如 var a=1,b=2; // 聲明一種測試案例 it('test add one',function(){ // 期望 自訂的函數 addOne(1) 結果為 2, 反向讀代碼 expect(2).toEqual(addOne(a)); expect(3).toEqual(addOne(b)); }); it('test add two',function(){ expect(3).toEqual(addTwo(a)); expect(5).toEqual(addTwo(b)); });});
你可以在 github 或者 入門指導網站 瞭解到 jasmine 的詳細資料?
github地址:?https://github.com/jasmine/jasmine
guide地址: ?https://jasmine.github.io/2.0/introduction.html
?Karma 設定檔
讀到這裡,可能會有疑問:被測試函數 和 測試指令碼應該放在哪裡?
下面來看?karma 設定檔
在 karma.exe 所在目錄下 或者 已將 karma 安裝至 global
命令列輸入(當然你也可以 命名為 **.conf.js)
karma init karma.conf.js
然後根據提示設定檔
配置 被測試代碼路徑 和 測試指令碼路徑 ( ** /?* 通配 檔案路徑/名稱)
省略省略省略…………
出現以下提示表示配置完成
如果想做一些個人化的處理,可以開啟檔案並 添加/修改 配置項
/** * Created by lonelydawn on 2017-03-04. */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: [ 'public/bower_components/angular/angular.js', 'app/javascripts/**/*.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 //程式碼涵蓋範圍測試 ,使用 karma-coverage preprocessors: { 'app/javascripts/**/*.js': 'coverage' }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['progress','coverage'], coverageReporter: { type: 'html', dir: 'coverage/' }, // 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: false })};
之後 命令列執行 ,即可開始測試 ( 在配置和啟動的時候一定要注意路徑問題 )
karma start karma.conf.js
?
Gulp下 karma 的使用
gulp 是一款非常簡單好用的自動化構建工具,中文文檔很詳細。
gulp 中文文檔地址 : http://www.gulpjs.com.cn/?
?
在 gulp 中使用karma 不再需要安裝 gulp-karma組件
github原文:
Karma?integration into?Gulp.js-based build.?
?
將 Karma 配置到項目 node_modules中並將設定檔建好之後
在 gulpfile.js 中寫入
var gulp=require('gulp');var Karma=require('karma').Server;// 前端自動化測試gulp.task('test', function (done) { new Karma({ // 設定檔所在路徑 configFile: __dirname + '/karma.conf.js', // 執行測試結束後退出 singleRun:true }, done).start();});gulp.task('tdd', function (done) { new Karma({ configFile: __dirname + '/karma.conf.js' }, done).start();});
之後在命令列鍵入
gulp test
或
gulp tdd
執行測試即可.