Cucumber: how to organize a complex test set

2019-04-09 08:36发布

I've got three versions of a backend that I'm testing. I would like to run similar feature specifications against the three versions.

Initially, I thought I'd just organize everything in a directory structure, as such:

features/
  v1/
    something.feature
    step_definitions/
      something_steps.rb
  v2/
    something.feature
    step_definitions/
      something_steps.rb
  v3/
    something.feature
    step_definitions/
      something_steps.rb

However, cucumber seems to flatten everything, which means that I end up with ambiguous step definitions.

I then thought of the following structure:

features/
  v1/
    something.feature
  v2/
    something.feature
  v3/
    something.feature
  step_definitions/
    something_steps.rb

I'd define a variable in the feature file somewhere, indicating which version that one is for, and I'd have a bunch of "ifs" inside the steps file, to choose code paths depending on that version variable. However, I haven't found an obvious way of defining that variable in the feature file.

Is there any way I can organize things, or will I just have to create multiple "feature" roots, one per version, which would be an awful solution given that it would mean multiple invocations of cucumber?

v1/
  features/
    something.feature
    step_definitions/
      something_steps.rb
v2/
  features/
    something.feature
    step_definitions/
      something_steps.rb
v3/
  features/
    something.feature
    step_definitions/
      something_steps.rb

标签: cucumber
1条回答
Animai°情兽
2楼-- · 2019-04-09 09:04

Why would multiple invocations of cucumber be a bad thing? Personally, that's how I'd do it, and run all three features from a Rakefile so I didn't have to do one after the other.

Also, if the conflicts are so large that you're rewriting the entire step definition for each version, maybe you should reconsider how you're wording them. Should you really describe it as doing the same thing if the code is so different?

If the changes are smaller, then I'd suggest looking at tags and hooks to achieve what you want.

So your feature might look like this:

@v1
Feature: some feature
  In order to test different versions
  As a cucumber user
  I want to be able to change step definitions based on what version feature is running

  Scenario: some scenario
    Given some thing
    When I pass some method
    Then I should get something

And your step definition might look like this:

Before('@v1') do
  Thing = V1Thing
  VERSION = 1
end

Given /^some thing&/ do
  @thing = Thing.new
end

When /^I pass some method$/ do
  @thing.some_action!
end

Then /^I should get something$/
  thing = "something" if VERSION == 1
  thing = "something else" if VERSION == 2
  @thing.prop.should == thing
end

You can use this method even if the step definitions are very different, but I think it will be harder to manage.

查看更多
登录 后发表回答