如何在每个场景之前输出Cucumber后台步骤? [英] How to output Cucumber background steps before each scenario?

查看:262
本文介绍了如何在每个场景之前输出Cucumber后台步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,Cucumber将输出后台步骤,以便与您在要素文件中定义的步骤一致(一次在顶部)。

Normally, Cucumber will output background steps to look the same as you defined them in your feature file (once at the top).

$ bundle exec cucumber --color --format pretty

Feature: Something

  Background:
    Given step 1
    And step 2

  Scenario: a scenario
    When I do step 3
    Then it works

  Scenario: another scenario
    When I do a different step 3
    Then it works

这将是更容易看到当一个后台步骤成功执行如果您可以始终显示场景开始的步骤。如何启用此行为?

It would be much easier to see when a background step is executed successfully if you could always display the steps at the beginning of a scenario. How can I enable this behavior?

Feature: Something

  Scenario: a scenario
    Given step 1
    And step 2
    When I do step 3
    Then it works

  Scenario: another scenario
    Given step 1
    And step 2
    When I do a different step 3
    Then it works


推荐答案

您必须创建自定义格式化程序。

You will have to create a custom formatter.

假设你想要的东西像漂亮的格式化程序,你可以创建类继承自漂亮的格式化程序,并替换所需的方法。基本上,您需要更改以下逻辑:

Assuming you want something like the pretty formatter, you can create class that inherits from the pretty formatter and replaces the required methods. Basically, you would want to change the logic that:


  • 不显示背景

  • 显示其背景步骤

  • The background is not displayed
  • Scenarios show their background steps

此格式化工具似乎有效:

This formatter appears to work:

require 'cucumber/formatter/pretty'

module Cucumber
  module Formatter
    class MyFormatter < Pretty

      def background_name(keyword, name, file_colon_line, source_indent)        
        # Do nothing
      end

      def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
        @hide_this_step = false
        if exception
          if @exceptions.include?(exception)
            return
          end
          @exceptions << exception
        end
        if @in_background
          @hide_this_step = true
          return
        end
        @status = status
      end

    end # MyFormatter
  end # Formatter
end # Cucumber

在您的支持文件夹中,您可以在启动cucumber时使用它:

Assuming this is in your support folder, you can use it when start cucumber:

cucumber -f Cucumber::Formatter::MyFormatter

这篇关于如何在每个场景之前输出Cucumber后台步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆