0

I currently have about 15 scenarios in one feature file and want to share data between them. I thought context injection would work and it is working between steps within a single scenario but I can't get it to pass data from one scenario to another. How does everyone else achieve this ?

Mr Specs
  • 11
  • 1
  • 3

2 Answers2

1

Short answer:

No one does this, as it's a Bad Idea™

Long answer:

If you have data valid for the whole feature, place it in the feature context. But this data can't be modified in one scenario and accessed in another.

The tests will be executed in an order determined by your test runner. Different runners may choose different orders. Execution order may be changed from one release of a runner to the next. Having temporal coupling between your tests, or implicit dependencies causes other problems as well, such as what happens if I want to run a test on its own? Now it will fail as the previous tests have not been run first. What If I want to run the tests in parallel? Now I can't as the tests have dependencies which need to be run first.

So what can I do?

My suggestion would be to use background steps (or explicit steps in your givens) to set up the data your individual scenario requires. Specflow makes reusing these steps, or have these steps reuse other steps, fairly simple. So if you need a customer and a product to create an order and you have scenarios like this:

Scenario: Creating a customer
Given a create a new customer called 'bob'
When I query for customers called 'bob'
Then I should get back a customer

Scenario: Creating a product
Given a create a new product called 'foo'
And 'foo' has a price of £100
When I query for products called 'foo'
Then I should get back a product
And the price should be £100

Scenario: customer places an order
Given I have a customer called 'bob'
And I have a product called 'foo' with a price £100
When 'bob' places an order for a 'foo'
Then an order for 1 'foo' should be created

here the last scenario creates all the data it needs. It can reuse the same step (with a different Given attribute) as Given a create a new customer called 'bob' and it can have a new step And I have a product called 'foo' with a price £100 which just calls the two existing steps Given a create a new product called 'foo' And 'foo' has a price of £100

This ensures that the test is isolated and does not have any dependencies.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • also see [my answer here](http://stackoverflow.com/a/30777405/97614), which provides some additional details of how you can implement some of this stuff – Sam Holder May 11 '16 at 13:57
1

you can create a variable static IDictionary<String, Object> globalData in another class say Global.cs

Now, in scenario 1: save any object

Globle.globalData.Set("Key", Object);

in scenario 2: retrieve the object by its key and cast it into previous type

var dataFromScen1 = Global.globalData.Get("Key");

in this way you can use data from scenario 1 into scenario 2 but you will face issues during parallel execution