使用Grunt运行时指定QUnit模块

时间:2013-04-26 16:28:09

标签: testing qunit gruntjs

我正在使用Grunt,PhantomJS和“watch”插件在我开发时运行我的QUnit测试(与CI分开)。我希望能够专注于特定的QUnit模块,同时我正在处理该模块测试所关注的代码。在浏览器中运行QUnit时,我可以指定要运行的模块(与所有测试相比)。

所以问题是,我可以告诉Grunt qunit任务只运行某个模块吗?我正在考虑一个命令行参数,以便我不必更改我的Gruntfile,如:

~$ grunt qunit --module="test this stuff, test that stuff"

更新

要清楚,我想要运行的是使用QUnit的module()方法在测试套件中创建的模块:

module( "group a" );
test( "a basic test example", function() {
    ok( true, "this test is fine" );
});
test( "a basic test example 2", function() {
    ok( true, "this test is fine" );
});

module( "group b" );
test( "a basic test example 3", function() {
    ok( true, "this test is fine" );
});
test( "a basic test example 4", function() {
    ok( true, "this test is fine" );
});

在上面的示例中,此代码全部在一个测试套件中,但在生成的html测试文件中,我得到一个下拉列表来运行模块“group a”或模块“group b”(通过QUnit的UI)。我想要的是能够以编程方式指定我想通过grunt qunit任务运行特定模块。

2 个答案:

答案 0 :(得分:0)

如果您为grunt-qunit设置配置,请执行以下操作:

grunt.initConfig({
  qunit: {
    module1: {
      options: {
        urls: [
         'http://localhost:8000/test/module1/foo.html',
         'http://localhost:8000/test/module1/bar.html',
        ]
      }
    },
    module2: {
      options: {
        urls: [
         'http://localhost:8000/test/module2/foo.html',
         'http://localhost:8000/test/module2/bar.html',
        ]
      }
    }
  }
  ...

您可以运行grunt qunit:module1

这样的单个模块

答案 1 :(得分:0)

我认为可行的解决方法可能是定义模块过滤器选项,如果存在,则将其附加到URL。 Gruntfile.js中有类似的东西

var moduleFilter =  '';
if (grunt.option('module')) {
  moduleFilter = '?module=' + grunt.option('module')
}

然后使用它:

grunt.initConfig({
  qunit: {
    options: {
        ...
        urls: [
          'http://localhost:3000/qunit' + moduleFilter
        ]
    }
  }
});

请注意,这仅适用于一个模块。 也许(但未经测试)您可以使用filter查询参数而不是模块, 并命名您的模块以匹配过滤器,以便能够对它们进行分组。

注意:运行多个模块不是QUnit支持的。

<强>参考文献:

相关问题