简体   繁体   中英

How to get scenario name and parameters? specflow

In this question scenario.getName was used to the name of the scenario. I need to get the name in addition to the parameters. For example if scenario is :

Scenario Outline: name of scenario
Given I am on the proper page
When I apply <filter>  with <params>
And I click filter
Then the data should be filtered
Examples:
| filter    | params      |
| Date      | Today       |
| Name      | Some Name   |

I want to get nameOfScenario(Date,Today).

Also I am using C# not java

UPDATE

I know when I open test cases with NUnit they show as nameOfScenario(Date,Today) . Any ideas how Nunit does it?

Feel free to use TestContext.CurrentContext.Test.Name -- it will definitely help you to get exact parametrized Scenario name.

Specflow doesn't leads with parameters at runtime, it is NUnit (or other unit test framework) responsibility.

At the least you can explore TestContext.CurrentContext.Test properties for obtaining parameters list.

You can get the title of the current scenario using ScenarioContext.Current.ScenarioInfo.Title but I don't think there is any way to get the names of the parameters.

NUnit has the name of the paramaters as Specflow genereates the unit test classes with these names at design time, it doesn't get them from specflow at run time

I don't believe there is any direct support in SpecFlow for doing this. However, with just a little effort, you can achieve the result you want.

Change your Scenario definition like this:

Background:
Given parameters <filter> and <params>

Scenario Outline: name of scenario
Given I am on the proper page
When I apply <filter>  with <params>
And I click filter
Then the data should be filtered
Examples:
| filter    | params      |
| Date      | Today       |
| Name      | Some Name   |

and implement the step definition that corresponds to the Given parameters etc. step.

Alternatively, if all you want is a way to distinguish between the examples, ie you don't care that it's "Date" and "Today", you just want to know that it was that line and not the other, you could add another column to your examples:

Scenario Outline: name of scenario
Given I am on the proper page
And I am working example number <example number>
When I apply <filter>  with <params>
And I click filter
Then the data should be filtered
Examples:
| filter    | params      | example number |
| Date      | Today       | 1              |
| Name      | Some Name   | 2              |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM