JS测试框架Karma安装使用
1.安装NodeJS
可以在http://nodejs.org/download/ 下载,我下载的是windows版本。
2.安装karma
新建一个目录如:E:\JavaScript\karma
E:\JavaScript\karma>npm install karma --save-dev
E:\JavaScript\karma>npm install karma-jasmine karma-chrome-launcher --save-dev //安装chrome插件
其它相关插件需要的话也可以安装,如:
npm karma-firefox-launcher #firefox浏览器插件
npm karma-junit-reporter #junit 测试报告插件
npm karma-coverage #单元测试覆盖率插件
验证是否安装成功:
E:\JavaScript\karma>karma start
INFO [karma]: Karma v0.12.17 server started at http://localhost:9876/
INFO [Chrome 36.0.1985 (Windows 7)]: Connected on socket McpCZbkxpU1Qg-mtrrdj wi
th id manual-1692
可以在本机访问URL: http://localhost:9876,页面显示如下:
3.配置Karma
可以用karma int命令创建karma.config.js配置文件
- E:\JavaScript\karma>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 quest
- ion.
- > 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.
- >
- 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 "E:\JavaScript\karma\karma.conf.js".
4.安装karma-jasmine
E:\JavaScript\karma>npm install karma-jasmine
5.JS单元测试举例
参考文档:
1.http://karma-runner.github.io/0.12/index.html
2.http://blog.csdn.net/puncha/article/details/18384289