1:安装karma
y@y:~$ npm install --global karma
2:安装karma插件
y@y:~$ npm install --global karma-jasmine karma-chrome-launcher
运行Karma:
y@y:~$ .npm-packages/lib/node_modules/karma/bin/karma start
INFO [karma]: Karma v0.12.32 server started at http://localhost:9876/
3:安装karma,命令行插件
y@y:~$ npm install -g karma-cli
测试:
y@y:~$ karma --version
Karma version: 0.12.
y@y:~$ karma start
INFO [karma]: Karma v0.12.32 server started at http://localhost:9876/
4:配置Karma
y@y:angular-test$ karma init my-karma.conf.js 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.
> 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.
> *.js
WARN [init]: There is no file matching this pattern. > test/*Spec.js
> 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 Config file generated at "/home/y/my_note/nodejs/angular-test/my-karma.conf.js". y@y:angular-test$ ll
总用量
drwxrwxr-x y y 5月 : ./
drwxrwxr-x y y 5月 : ../
drwxrwxr-x y y 5月 : app/
drwxrwxr-x y y 5月 : bower_components/
drwxrwxr-x y y 5月 : .idea/
-rw-rw-r-- y y 5月 : my-karma.conf.js
drwxrwxr-x y y 5月 : test/
y@y:angular-test$
5:修改项目下面的my-karma.conf.jsp配置文件,将需要的js文件引入:
// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.min.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/js/*.js',
'test/*Spec.js'
],
6:进行运行测试:
@y:angular-test$ karma start my-karma.conf.js
INFO [karma]: Karma v0.12.32 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 39.0. (Linux)]: Connected on socket cz6gdNTkD5A0FyfLAAAA with id
Chrome 39.0. (Linux): Executed of SUCCESS (0.021 secs / 0.016 secs)