Vue項目自動化測試

在開發(fā)的過程中颓屑,除了代碼本身斤寂,測試也是重要的一環(huán)。大體來說揪惦,測試分為以下幾種類型:

單元測試
功能測試
性能測試
安全測試

單元測試工具 Karma

要使用 Karma 對代碼進行單元測試遍搞,首先需要安裝一系列的相關(guān)插件。我們來新建一個名為 myKarmDemo 的目錄器腋,并安裝相關(guān)的插件:

npm install karma-cli -g
npm install karma jasmine-core karma-jasmine karma-phantomjs-launcher -D

接下來對我們的工程進行初始化:

karma init

之后會彈出一些選項溪猿,其中包含了一些初始化的配置工作,使用上下方向鍵可以在配置項之間進行切換纫塌。我這里選擇使用 Jasmine 測試框架诊县,使用 PhantomJS 無界面瀏覽器,整體的配置選項如下:

myKarmDemo 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

Config file generated at "/home/charley/Desktop/myKarmDemo/karma.conf.js".

初始化完成之后措左,會在我們的項目中生成一個 karma.conf.js 文件依痊,這個文件就是 Karma 的配置文件。
配置文件比較簡單怎披,能夠比較輕松的看懂胸嘁,這里我對原始的配置文件進行簡單的修改,結(jié)果如下:

// Karma configuration
// Generated on Sun Oct 29 2017 21:45:27 GMT+0800 (CST)

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 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
  })
}

然后創(chuàng)建一個 src 目錄和一個 test 目錄凉逛,在其中分別創(chuàng)建 index.js 和 index.spec.js 文件缴渊。
我要做的測試內(nèi)容比較簡單,對 index.js 中的兩個函數(shù)(一個加法函數(shù)鱼炒,一個乘法函數(shù))進行測試衔沼。
index.js 文件如下:

// 加法函數(shù)
function add(x){
    return function(y){
        return x + y;
    }
}
// 乘法函數(shù)
function multi(x){
    return function(y){
        return x * y + 1;
    }
}

index.spec.js 文件如下:

describe("運算功能單元測試",function(){
    it("加法函數(shù)測試",function(){
        var add5 = add(5)
        expect(add5(5)).toBe(10)
    });

    it("乘法函數(shù)測試",function(){
        var multi5 = multi(5)
        expect(multi5(5)).toBe(25)
    })
})

單測的代碼寫好后蝌借,就可以使用 karma start 來運行單元測試。由于我們的乘法代碼中有錯誤指蚁,因此測試結(jié)果是這樣的:

myKarmDemo karma start
29 10 2017 22:21:56.283:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
29 10 2017 22:21:56.287:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
29 10 2017 22:21:56.294:INFO [launcher]: Starting browser PhantomJS
29 10 2017 22:21:56.549:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket 0h6boaepSUMwG7l2AAAA with id 44948955
PhantomJS 2.1.1 (Linux 0.0.0) 運算功能單元測試 乘法函數(shù)測試 FAILED
        Expected 26 to be 25.
        test/index.spec.js:9:31
PhantomJS 2.1.1 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.048 secs / 0.002 secs)

將乘法函數(shù)的代碼改為正常菩佑,再次啟用 karma start 進行測試:

29 10 2017 22:23:08.670:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
29 10 2017 22:23:08.673:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
29 10 2017 22:23:08.685:INFO [launcher]: Starting browser PhantomJS
29 10 2017 22:23:08.940:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket pl_pZDcAK4rBTpr0AAAA with id 40770640
PhantomJS 2.1.1 (Linux 0.0.0): Executed 2 of 2 SUCCESS (0.045 secs / 0.001 secs)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市凝化,隨后出現(xiàn)的幾起案子稍坯,更是在濱河造成了極大的恐慌,老刑警劉巖搓劫,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瞧哟,死亡現(xiàn)場離奇詭異,居然都是意外死亡枪向,警方通過查閱死者的電腦和手機勤揩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秘蛔,“玉大人陨亡,你說我怎么就攤上這事∩钤保” “怎么了负蠕?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長倦畅。 經(jīng)常有香客問我遮糖,道長,這世上最難降的妖魔是什么叠赐? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任止吁,我火速辦了婚禮,結(jié)果婚禮上燎悍,老公的妹妹穿的比我還像新娘敬惦。我一直安慰自己,他們只是感情好谈山,可當我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布俄删。 她就那樣靜靜地躺著,像睡著了一般奏路。 火紅的嫁衣襯著肌膚如雪畴椰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天鸽粉,我揣著相機與錄音斜脂,去河邊找鬼。 笑死触机,一個胖子當著我的面吹牛帚戳,可吹牛的內(nèi)容都是我干的玷或。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼片任,長吁一口氣:“原來是場噩夢啊……” “哼偏友!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起对供,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤位他,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后产场,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鹅髓,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年京景,在試婚紗的時候發(fā)現(xiàn)自己被綠了窿冯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡醋粟,死狀恐怖靡菇,靈堂內(nèi)的尸體忽然破棺而出重归,到底是詐尸還是另有隱情米愿,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布鼻吮,位于F島的核電站育苟,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏椎木。R本人自食惡果不足惜违柏,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望香椎。 院中可真熱鬧漱竖,春花似錦、人聲如沸畜伐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽玛界。三九已至万矾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間慎框,已是汗流浹背良狈。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留笨枯,地道東北人薪丁。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓遇西,卻偏偏與公主長得像,于是被迫代替她去往敵國和親窥突。 傳聞我的和親對象是個殘疾皇子努溃,可洞房花燭夜當晚...
    茶點故事閱讀 45,507評論 2 359