如何在BDD Specflow C#中获取背景标题

时间:2018-03-06 20:53:22

标签: selenium-webdriver cucumber nunit bdd specflow

如何获得预测试条件的名称"背景"使用C#中的Specflow + Nunit在功能中标记?

我可以得到"功能"的名称。像这样:

return FeatureContext.Current.FeatureInfo.Title;

" Scenario"的名称像这样:

 return ScenarioContext.Current.ScenarioInfo.Title;

我还需要获取"背景"的名称,或检查它是否存在。

Feature: FeatureTest
    Description Feature...

Background: Get Background name or check it exists (Return It)
    ...Given, And

Scenario: Scenario Test
    ...Given, And

2 个答案:

答案 0 :(得分:0)

此信息目前在SpecFlow中无法在运行时获得。请在GitHub上打开一个问题,我们可以在以后的版本中添加它。

但通常依靠这种脆弱的(潜在的经常变化的魔法弦)是不好的做法。 更好的方法是在场景或功能级别上检查标记。

另请注意,您在运行时并不知道您当前正在执行部分后台操作。这些给定步骤与您的方案的给定步骤之间没有区别。

答案 1 :(得分:0)

TL; DR

您无法访问Background的原因是因为其中包含的步骤在运行时在功能中的所有方案中重复,从而有效地删除了背景。

答案:

一般来说,背景没有标题:

Ability: Adding and Removing items from the basket

   As a customer,
   I want the ability to add and remove items from my basket
   In order to choose the items that I want to purchase from the site

 Background:
   Given I have logged in as a customer
   And I visit the "Clothing" page

 Scenario: Adding items to my basket
   When I add "Black Jeans" to my basket in size "M"
   Then the total should be "£9.99"

 Scenario: Removing items from the basket
   Given I have added an item of clothing to my basket
   When I empty my basket
   Then the total should be "£0.00"

后台步骤在功能中的场景中重复,这意味着我的示例中的场景是有效的:

 Scenario: Adding items to my basket
   Given I have logged in as a customer
   And I visit the "Clothing" page
   When I add "Black Jeans" to my basket in size "M"
   Then the total should be "£9.99"

 Scenario: Removing items from the basket
   Given I have logged in as a customer
   And I visit the "Clothing" page
   Given I have added an item of clothing to my basket
   When I empty my basket
   Then the total should be "£0.00"

如果方案描述涵盖了您实际测试的内容,为什么需要访问背景说明?

这就是为什么他们目前不提供此功能。它是文件中的附加信息,可以方便地阅读和理解实际测试内容 - 但如果您的步骤足够描述,您是否需要描述测试的设置?