【问题标题】:Specify QUnit module when run with Grunt使用 Grunt 运行时指定 QUnit 模块
【发布时间】:2013-04-20 21:39:22
【问题描述】:

我在开发时使用 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 测试文件中,我得到一个下拉菜单来运行模块“组 a”或模块“组 b”(通过 QUnit 的 UI)。我想要的是能够以编程方式指定我想通过grunt qunit 任务运行特定模块。

【问题讨论】:

    标签: testing qunit gruntjs


    【解决方案1】:

    如果您像这样为 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

    【讨论】:

    • 虽然如此,但我希望找到更通用的东西。基本上,当我将一个模块添加到我的测试套件时,我不想为它创建一个新的模块目标。不过感谢您的发帖。
    • 好吧,您可以创建一个在./test 中查找的任务,并为每个子目录配置一个模块,然后将它们排队等待执行,并采用模块参数进行过滤。
    • 是的,但我更多的是指能够运行 QUnit 的modules。您在这里拥有的是创建充当该角色的不同 html 测试套件。我想要的是能够运行在测试套件中声明的模块,而不是新创建的 html 文件。
    • grunt-contrib-qunit 中似乎不存在该功能,因此我认为您必须提交补丁或功能请求。
    【解决方案2】:

    我认为一个可行的解决方法是定义一个模块过滤器选项,如果它存在,请将其附加到 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 不支持运行多个模块。

    参考资料:

    【讨论】:

    • 这当然看起来是一个更可行的解决方案,我会在我回到这个项目时尝试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多