【问题标题】:How can I run one particular CucumberJS feature using GruntJS?如何使用 GruntJS 运行一项特定的 CucumberJS 功能?
【发布时间】:2014-02-04 05:14:23
【问题描述】:

我正在使用 CucumberJS 在我的 NodeJS Web 应用程序上运行测试。

目前,我可以通过执行 grunt 来运行我所有的 grunt 任务,或者使用 grunt cucumberjs 只运行 CucumberJS 任务。

但现在我只想执行特定的功能。

例如,假设我有以下功能文件:

  • 登录功能
  • Favorite.feature

我只想运行收藏功能测试,使用如下命令:

grunt cucumberjs Favourite

这可能吗?


顺便说一句,这是我的gruntfile.js

'use strict';

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        ...
        cucumberjs: {
            src: 'features',
            options: {
                steps: 'features/step_definitions',
                format: 'pretty'
            }
        }
    });

    ...
    grunt.loadNpmTasks('grunt-cucumber');

    grunt.registerTask('default', [... 'cucumberjs']);
};

【问题讨论】:

    标签: node.js gruntjs cucumberjs


    【解决方案1】:

    基于tags,我终于找到了一个似乎运行良好的解决方案。

    所以对于我的每个功能文件,我都添加了一个标签。

    例如,对于 Favourite.feature:

    @favourite
    Feature: Favourite
        As a user of the system
        I would like to favourite items
    

    然后我使用GruntJS option 来指定我想通过命令行参数运行的标签。

    我通过在我的 gruntfile 中调用 grunt.option() 来做到这一点:

    cucumberjs: {
        src: 'features',
        options: {
            steps: 'features/step_definitions',
            format: 'pretty',
            tags: grunt.option('cucumbertags')
        }
    }
    

    所以现在我可以像这样从命令行运行 GruntJS:

    grunt cucumberjs --cucumbertags=@favourite
    

    它只会运行带有@favourite 标签的功能。耶!

    【讨论】:

      【解决方案2】:

      这是我的任务,允许您按 feature.name 过滤:

      https://github.com/webforge-labs/cuked-zombie/blob/master/tasks/cucumber.js (将其下载到“任务”文件夹中,您可以使用:grunt.loadTasks('tasks') 加载它)

      像这样配置它(Gruntfile.js):

      grunt.loadNpmTasks('grunt-cucumber');
      
      grunt.initConfig({
        cucumberjs: {
          // config for all features when called with: `grunt cucumber`
          all: {
            src: 'features',
            options: {
              steps: "tests/js/cucumber/bootstrap.js",
              format: "pretty"
            }
          },
      
          // config for single features when called with `grunt --filter some-feature`
          features: {
            src: 'features',
            options: {
              steps: "tests/js/cucumber/bootstrap.js",
              format: "pretty"
            }
          }
        }
      });
      

      使用它:

      grunt cucumber --filter post
      

      它将运行 post.feature(在您的功能目录中的某个位置)

      使用新的 cucumber-js 版本,可以通过功能线运行它: https://github.com/cucumber/cucumber-js/pull/168

      【讨论】:

        【解决方案3】:

        我没有用 grunt 测试过它,但是 cucumber 可以选择在不修改 gherkin 文件的情况下执行场景。只需调用cucumber-js --name "Scenario Name",因此我假设发送相同的参数来授予它会完成它的工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多