我已经正确设置了karma配置,配置文件在后台运行,非常棒。一旦我更改并保存了一个文件,它就会重新运行测试。。。。所有750个单元测试。我只想跑几次。除了手动破解配置文件或注释掉许多文件中的数百个测试之外,有什么简单的方法可以做到这一点吗?
例如,当使用mocha运行命令行服务器测试时,我只使用regexp:mocha -g 'only tests that I want'
。使调试和快速检查更加容易。
所以现在我觉得自己很愚蠢。mocha支持非常窄版本的regexp匹配。
这将运行所有测试
describe('all tests',function(){
describe('first tests',function(){
});
describe('second tests',function(){
});
});
这只是运行"第一次测试"
describe('all tests',function(){
describe.only('first tests',function(){
});
describe('second tests',function(){
});
});
你也可以做it.only()
我应该注意到的。叹气
不幸的是,您可以在因果报应启动时这样做,而不是在运行时。如果你想动态地改变它,你必须付出更多的努力。
假设你想从一开始就专注于一组特定的测试,在karma-mocha
插件页面上有一段代码可以做你想做的事情:
module.exports = function(config) {
config.set({
// karma configuration here
...
// this is a mocha configuration object
client: {
// The pattern string will be passed to mocha
args: ['--grep', '<pattern>'],
...
}
});
};
为了使<pattern>
参数化,您必须将配置文件封装在Configurator中,该Configurator将侦听CLI并为您自定义因果报应配置。
看看这个SO答案,了解如何设置一个非常简单的配置程序。
我也有同样的问题,这是我在karma.conf.js上做了一点更改后的工作。事实上,从命令行中获取一个参数并修改"文件"中的模式。我使用minimit来解析参数列表。
在配置文件中:
/* Begin */
var minimist = require('minimist');
var argv = minimist(process.argv);
var testBase="test/unit";
var testExt=".spec.js";
var unitTestPattern = testBase+'/**/*'+testExt;
if ("test" in argv){
unitTestPattern = testBase+"/"+argv["test"]+testExt;
}
/* End */
module.exports = function(config){
config.set({
//....
files : [
//....
unitTestPattern, //place here
// 'test/unit/**/*.spec.js', //replace this
//....
],
//....
});
};
磨合命令提示符:
karma start test/karma.conf.js --single-run --test #TEST_CASE_FILE#
一个很好的扩展可以帮助这里是karma jasmine html记者livereloadhttps://www.npmjs.com/package/karma-jasmine-html-reporter-livereload
或karma jasmine html记者https://www.npmjs.com/package/karma-jasmine-html-reporter?__hstc=72727564.86845f057bb4d741f59d578059e30644.1443860954685.1453095135802.1453138187458.37&__hssc=72727564.1.14453138187458&__hsfp=2285154675
它创建了一个调试页面,您可以在其中单独运行每个测试。对大型项目非常有用!
1)在您的karma.conf.js中,从终端获取参数:
var files = (process.env.npm_config_single_file) ? process.env.npm_config_single_file : 'test/test_index.js';
2) 为了运行单个测试,您需要使用所有配置设置一个选项对象(没有文件和预处理器):
var option = {
webpack: {
// webpack configuration
},
// more configuration......
};
3) 设置文件路径和预处理器:
option.files = [
{pattern: files, watch: false}
];
option.preprocessors = {};
option.preprocessors[files] = [ 'webpack', 'sourcemap' ];
// call config.set function
config.set(option);
4) 在终端中运行:
npm test --single_file=**/my-specific-file-spec.js
有关更多信息,请查看此PR:https://github.com/webpack/karma-webpack/pull/178
有不同的方法。
-
使用
--grep
选项。这样做的缺点是,在运行特定的测试套件之前,所有的测试都是经过预处理的。 -
使用
.only
方法。缺点与1号相同。同时使用1和2方法,我的节点进程经常崩溃,说内存不足。 -
限制要处理的文件选项。这太快了。
将预处理限制为某些文件夹,如Unit
或Integration
文件夹。为此,我使用了自定义cli选项--only
和karma配置
const modules = config.only;
并且在文件模式中
files: typeof modules === 'string ? '[`tests/**/${module}/**/*.(test|spec).js`]: 'tests/**/*.(test|spec).js'
优点:当开发人员通过在预处理阶段进行限制来加快一个小的更改时,他们只能运行某些测试。
你也可以使用3号和1号或2号的组合。