我如何只在业力中运行某些测试?

时间:2014-10-28 08:03:03

标签: karma-runner karma-mocha

我正确设置了karma配置,配置文件,在后台运行,非常棒。一旦我更改并保存文件,它就会重新运行测试....所有750个单元测试。我希望能够运行一些。如果没有手动攻击配置文件或在多个文件中注释掉数百个测试,有没有简单的方法呢?

E.g。当使用say mocha运行命令行服务器测试时,我只使用regexp:mocha -g 'only tests that I want'。使调试和快速检查变得更加容易。

6 个答案:

答案 0 :(得分:6)

所以现在我觉得很愚蠢。 mocha支持非常窄的正则表达式匹配版本。

这会运行所有测试

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

我应该注意到这一点。叹息。

答案 1 :(得分:4)

不幸的是,你可以在karma启动时这样做,而不是在运行时。 如果你想动态地改变它,你需要付出更多的努力。

假设您希望从一开始就专注于一组特定的测试/测试套件,在karma-mocha plugin page上有这段代码可以做您想做的事情:

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>参数,您必须将配置文件包装在配置器中,该配置文件将监听CLI并为您自定义业力配置。

了解this SO answer了解如何设置非常简单的配置器

答案 2 :(得分:0)

我有同样的问题,这是我对karma.conf.js的一点改动。 实际上,从命令行中取一个参数并修改“files”中的模式。 我使用minimist来解析参数列表。

在配置文件中:

/* 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#

答案 3 :(得分:0)

一个很好的扩展,可以帮助这里是karma-jasmine-html-reporter-livereload https://www.npmjs.com/package/karma-jasmine-html-reporter-livereload

或karma-jasmine-html-reporter https://www.npmjs.com/package/karma-jasmine-html-reporter?__hstc=72727564.86845f057bb4d741f59d578059e30644.1443860954685.1453095135802.1453138187458.37&__hssc=72727564.1.1453138187458&__hsfp=2285154675

它会创建一个调试页面,您可以在其中单独运行每个测试。对大型项目非常有用!

答案 4 :(得分:0)

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

答案 5 :(得分:0)

有不同的方法。

  1. 使用--grep选项。这样做的缺点是,在运行特定的测试套件之前,所有测试都已进行了预处理。

  2. 使用.only方法。缺点与否相同。 1.使用1和2方法,我的节点进程经常崩溃,通常说内存不足。

  3. 限制文件选项进行处理。这是超级快。

将预处理限制为某些文件夹,例如UnitIntegration文件夹。 为此,我使用了自定义cli选项--only,并在业力配置中使用了

const modules = config.only;

并在文件模式中

files: typeof modules === 'string ? '[`tests/**/${module}/**/*.(test|spec).js`]: 'tests/**/*.(test|spec).js'

优势:开发人员只能通过限制预处理阶段的速度来快速进行一些小的更改,因此只能运行某些测试。

您也可以将3号与1号或2号组合使用。