12

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?

Community
  • 1
  • 1
mosaad
  • 2,276
  • 5
  • 27
  • 49

3 Answers3

10

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.

Vitaly Filatenko
  • 415
  • 3
  • 12
  • 4
    It should be noted that this answer refers specifically to the NUnit class `TestContext`. The class `Microsoft.VisualStudio.TestTools.UnitTesting.TestContext` does not have a definition for `CurrentContext`. – Erick G. Hagstrom Jun 10 '16 at 18:11
7

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

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
0

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, i.e. 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              |
Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38