【问题标题】:Cucumber: the same background in many of featuresCucumber:许多功能的背景相同
【发布时间】:2021-10-11 07:50:50
【问题描述】:

我有很多具有相同背景的功能(单独的 *.feature 文件),例如: Test1.feature:

Feature: Multiple site support

Background: 
 Given a global administrator named "Greg"
 And a blog named "Greg's anti-tax rants"
 And a customer named "Dr. Bill"
 And a blog named "Expensive Therapy" owned by "Dr. Bill"

Scenario: Dr.Greg posts to his own blog
 Given I am logged in as Dr.Greg
 When I try to post to "Some" im my blog
 Then I should see "Some articles."

Test2.feature:

Feature: One site support

Background: 
 Given a global administrator named "Greg"
 And a blog named "Greg's anti-tax rants"
 And a customer named "Dr. Bill"
 And a blog named "Expensive Therapy" owned by "Dr. Bill"

Scenario: Greg posts to a client's blog
 Given I am logged in as Greg
 When I try to post to "Expensive Therapy"
 Then I should see "Your article was published.

Test3.feature:

Feature: Other site support

Background: 
 Given a global administrator named "Greg"
 And a blog named "Greg's anti-tax rants"
 And a customer named "Dr. Bill"
 And a blog named "Expensive Therapy" owned by "Dr. Bill"
...

多个不同的功能文件使用相同的背景步骤,我可以将它们放在一个文件中并将它们包含在不同的场景中吗? 或者有没有更好的做法来避免在某些功能中重复相同的步骤?

【问题讨论】:

  • 发布了答案。请让我知道你的 cmets。

标签: java cucumber


【解决方案1】:

使用@Before钩子。

@Before - 在每个场景之前执行方法。

    @Before 
     Given a global administrator named "Greg"
     And a blog named "Greg's anti-tax rants"
     And a customer named "Dr. Bill"
     And a blog named "Expensive Therapy" owned by "Dr. Bill"
    
    Scenario: Dr.Greg posts to his own blog
     Given I am logged in as Dr.Greg
     When I try to post to "Some" im my blog
     Then I should see "Some articles."

    Scenario: Greg posts to a client's blog
     Given I am logged in as Greg
     When I try to post to "Expensive Therapy"
     Then I should see "Your article was published.
     ...

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多