netbeans和karma进行单元测试

Cocolada 2014-05-09

1、安装karma

安装时,需要提前安装node.js,才能执行下列步骤

打开命令行,把karma安装到系统里,加上-g参数,使全局可用

npm install -g karma karma-cli

npm install -g karma-jasmine karma-chrome-launcher karma-firefox-launcher karma-ie-launcher karma-junit-reporter karma-commonjs karma-coverage karma-script-launcher

2、创建配置文件

打开netbeans,在项目配置文件夹下创建karma.conf.js文件

在IDE里,选中项目,右键--》新建--》karma配置文件

修改文件内容:

module.exports = function(config) {
     config.set({
        basePath: '../',
        files: [
            'public_html/js/*.js',
            'test/js/*.js'
        ],
        exclude: [
        ],
        autoWatch: true,
        frameworks: ['jasmine'
        ],
        browsers: ['Chrome'
        ],
         reporters: ['progress', 'junit'],

        // the default configuration
        junitReporter: {
          outputFile: 'test-results.xml',
          suite: ''
        },
        
        plugins: [
              'karma-junit-reporter',
              'karma-chrome-launcher',
              'karma-firefox-launcher',
              'karma-script-launcher',
              'karma-jasmine'
        ]
    });
};

创建测试文件

//路径public_html/js/plus.js

// Some code under test
function plus(a, b) {
  return a + b;
}
//路径test/js/test.js

describe('plus', function() {
  it('should pass', function() {
    expect(true).toBe(true);
  });

  it('should work', function() {
    expect(plus(1, 2)).toBe(3);
  });
  
  it('should work', function() {
	    expect(plus(5, 4)).toBe(2);
	  });
});

3、配置项目

选中项目--》右键属性--》JavaScript测试--》测试提供器karma

配置文件选中刚刚创建的karma.conf.js,确定关闭属性对话框。

4、运行测试项目

项目多了一个karma的图标,选中--》右键--》启动,即可进行项目测试。

5、异常,百撕不得骑姐~

ERROR [karma]: [TypeError: Cannot call method 'push' of undefined]
TypeError: Cannot call method 'push' of undefined
    at onBrowserStart (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\lib\reporters\base.js:14:20)
    at null.<anonymous> (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\lib\events.js:15:22)
    at EventEmitter.emit (events.js:117:20)
    at onStart (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\lib\browser.js:128:13)
    at Socket.<anonymous> (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\lib\events.js:15:22)
    at Socket.EventEmitter.emit [as $emit] (events.js:117:20)
    at SocketNamespace.handlePacket (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\node_modules\socket.io\lib\namespace.js:335:22)
    at Manager.onClientMessage (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\node_modules\socket.io\lib\manager.js:488:38)
    at WebSocket.Transport.onMessage (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\node_modules\socket.io\lib\transport.js:387:20)
    at Parser.<anonymous> (C:\Users\xxx\AppData\Roaming\npm\node_modules\karma\node_modules\socket.io\lib\transports\websocket\hybi-16.js:39:10)
完成。

按照官方的说明,配置N次,都不能成功执行。

恭喜你,和我的版本一样,是netbeans的bug

Product Version: NetBeans IDE 8.0 (Build 201403101706)
更新: NetBeans IDE 已更新到版本 NetBeans 8.0 Patch 1.1
Java: 1.7.0_45; Java HotSpot(TM) 64-Bit Server VM 24.45-b08
Runtime: Java(TM) SE Runtime Environment 1.7.0_45-b18
System: Windows 7 version 6.1 running on amd64; GBK; zh_CN (nb)

C:\Users>node --version
v0.10.22


C:\Users>karma --version
Karma version: 0.12.9

问题原因:

netbeans未能够识别出来karma.conf.js文件,给

C:\Users\consumer\AppData\Roaming\npm\karma.cmd脚本传了个错误的启动参数

start "D:\Program Files\NetBeans 8.0\webcommon\karma\karma-netbeans.conf.js" --port 9880

根本没有加载我们的配置文件。

解决办法:

项目--》配置文件夹--》右键--》新建--》空文件--》karma.cmd

输入

karma start config\karma.conf.js

项目--》右键--》属性--》JavaScript单元测试--》karma--》浏览--》选中karma.cmd

--》确定

rockandroll~

start config\karma.conf.js
INFO [karma]: Karma v0.12.9 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 34.0.1847 (Windows 7)]: Connected on socket esBL7IuEI9lscGYZ_WXz with id 57142013
Chrome 34.0.1847 (Windows 7): Executed 0 of 3 SUCCESS (0 secs / 0 secs)
Chrome 34.0.1847 (Windows 7): Executed 1 of 3 SUCCESS (0 secs / 0.01 secs)
Chrome 34.0.1847 (Windows 7): Executed 2 of 3 SUCCESS (0 secs / 0.011 secs)
Chrome 34.0.1847 (Windows 7) plus should work FAILED
	Expected 9 to be 2.
	Error: Expected 9 to be 2.
	    at null.<anonymous> (E:/html-app/test/js/test.js:11:25)
Chrome 34.0.1847 (Windows 7): Executed 3 of 3 (1 FAILED) (0 secs / 0.012 secs)
Chrome 34.0.1847 (Windows 7): Executed 3 of 3 (1 FAILED) (0.018 secs / 0.012 secs)

若控制台为乱码,修改netbeans的配置文件:

在NetBeans8.0\etc\netbeans.conf文件里追加一行

netbeans_default_options="-J-Dfile.encoding=UTF-8"

单元测试代码的编写方法

相关推荐