Reputation: 116
Is it possible to access the MSTest TestContext from within a SpecFlow (1.7.1) step binding class? In the generated code of a feature file there is a method FeatureSetup which takes the TestContext as an argument but apparently doesn't do anything with it.
Upvotes: 3
Views: 3747
Reputation: 61
I found a way to pass parameters to TestContext and then access them from SpecFlow.
By adding a [TestClass] which has a TestContext property and marking its AssemblyInit() method as [AssemblyInitialize] so it gets initialized early before runnig the tests and MSTest will be able to populate the TestContext.
{
[TestClass]
public class InitializeTestContext
{
public static TestContext Context { get; private set; }
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
Context = context;
}
}
}
And then can access it from my BaseSteps class:
{
public abstract class BaseSteps : TechTalk.SpecFlow.Steps
{
public string GetTestEnvironment()
{
TestContext testContext = InitializeTestContext.Context;
string testEnvironment = testContext.Properties["Environment"].ToString();
return testEnvironment;
}
}
}
Upvotes: 6
Reputation: 7303
Further to Valentin's answer. Here is an example of a test generator that will add in the test context. Its from the same Google group.
Gáspár Nagy said it may be added to the provider that ships in specflow.
So to answer the OP's question, yes it is possible.
Upvotes: 0
Reputation: 11
Gáspár Nagy answered on SpecFlow google group: https://groups.google.com/group/specflow/browse_thread/thread/5b038e3e283fdbfe#
By default not. We have a test-provider independent ScenarioContext.Current that can be used for similar purposes.
Upvotes: 3