Cucumber:如何组织复杂的测试集

发布于 2024-10-17 18:27:59 字数 1013 浏览 3 评论 0原文

我正在测试三个版本的后端。我想针对这三个版本运行类似的功能规范。

最初,我以为我只是将所有内容组织在目录结构中,如下所示:

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

但是,黄瓜似乎将所有内容展平,这意味着我最终会得到不明确的步骤定义。

然后我想到了以下结构:

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

我会在功能文件中的某处定义一个变量,指示该变量适用于哪个版本,并且在步骤文件内有一堆“if”,以根据情况选择代码路径该版本变量。但是,我还没有找到在功能文件中定义该变量的明显方法。

有什么方法可以组织事物,或者我只需要创建多个“功能”根,每个版本一个,这将是一个糟糕的解决方案,因为这意味着多次调用黄瓜?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2024-10-24 18:27:59

为什么多次调用 Cucumber 会是一件坏事?就我个人而言,我就是这样做的,并从 Rakefile 运行所有三个功能,这样我就不必一个接一个地执行。

另外,如果冲突太大以至于您要重写每个版本的整个步骤定义,也许您应该重新考虑如何措辞它们。如果代码如此不同,您真的应该将其描述为做同样的事情吗?

如果更改较小,那么我建议查看 标签hooks 来实现你想要的。

因此,您的功能可能如下所示:

@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

您的步骤定义可能如下所示:

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

即使步骤定义非常不同,您也可以使用此方法,但我认为这将更难管理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文