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

Selenium webdriver 如何在BDD Specflow C中获取背景标题#,selenium-webdriver,cucumber,nunit,bdd,specflow,Selenium Webdriver,Cucumber,Nunit,Bdd,Specflow,如何使用C#中的Specflow+Nunit在特性中获取测试前条件“Background”标记的名称 我可以这样得到“功能”的名称: return FeatureContext.Current.FeatureInfo.Title; “场景”的名称如下: return ScenarioContext.Current.ScenarioInfo.Title; 我还需要获得“背景”的名称,或者检查它是否存在 Feature: FeatureTest Description Feature.

如何使用C#中的Specflow+Nunit在特性中获取测试前条件“Background”标记的名称

我可以这样得到“功能”的名称:

return FeatureContext.Current.FeatureInfo.Title;
“场景”的名称如下:

 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

此信息当前在SpecFlow的运行时不可用。请在GitHub上为此打开一个问题,我们可以在以后的版本中添加此问题

但一般来说,依赖这种脆弱的(可能经常变化的魔法弦)是不好的做法。 更好的方法是在场景或功能级别上检查标记

还要注意的是,在运行时您不知道您当前正在执行部分后台。这些给定步骤与场景中的给定步骤没有区别。

TL;博士 无法访问后台的原因是,其中包含的步骤在运行时会在功能中的所有场景中重复,从而有效地删除后台

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

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"
如果场景描述涵盖了您实际测试的内容,为什么需要访问背景描述


这就是为什么目前他们没有提供这一功能。它是文件中的附加信息,便于阅读和理解实际正在测试的内容-但是如果您的步骤足够描述性,您是否需要测试设置的说明?

我需要这些信息来在.html报告中写入日志,出于组织原因,对于无法访问该功能的用户,为了便于阅读或识别步骤,我目前记录了所有步骤,使报告有点多余。。。示例:我需要在html报告中注册执行结果,但这些功能并不总是有背景,因此它是动态工作的,我需要至少检查背景是否存在。例子: