New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In SpecFlow 2.2.1, how do I access the MsTest TestContext? #936
Comments
Do you have access to the TestContext in a normal binding? |
BTW, *Context.Current.Count is looking into the context dictionary for values, these are not the container registrations. You can get it from the container directly:
or via context injection:
|
So the following does indeed work within a Test Definition method.
But it is not working from within my scoped hook.
I get an exception: "Cannot create an instance of Microsoft.VisualStudio.TestTools.UnitTesting.TestContext because it is an abstract class." |
ok, you get the exception because the class is not registered. |
Thanks for you help on this. I've uploaded a sample project here: |
I found the issue. Look at the generated code of the ScenarioSetup method:
The TestContext is registered in the ScenarioContainer after the OnScenarioStart call. Which is necessary, as the ScenarioContainer is created within this call. @gasparnagy Any idea how to fix this? |
@SabotageAndi a solution could be to extend the OnScenarioStart to accept a delegate that would be called after the context is created but before the hooks are executed. @CharlieMott a workaround for you could be to use a "BeforeStep" hook and remember if it was called:
|
Thanks @gasparnagy , this workaround does indeed work for my needs. |
Using Specflow 2.2.1 with MSTest framework: As per #96
And...
|
@JasonArm I am not aware of any changes in v2.2.1 that would change the availabilit of ScenarioContex or FeatureContext in an AfterScenario hook. They should not be null! Please double check and if you still experience this behavior than create a new issue for that. Here we were only talking about the availability of the test context in the scenario context. Since the generated code adds it to the scenario context after the "before" hooks it is not available there. But once it is added, it remains there so there should be no problem with the "after" hooks. |
Are there any another means of obtaining the TestContext? My use case is test runner utility specflow plugin which requires the MsTest TestContext. The main work is done by a class which inherits from the ITestRunner. Ideally I'd like either an AfterFirstStep() method or an event to subscribe to, to do the task required |
@akinsolb there is no such thing, I think. You can handle the different step methods and check if it was the first within your plugin. |
@SabotageAndi a solution could be to extend the OnScenarioStart to accept a delegate that would be called after the context is created but before the hooks are executed. Any plans to fix this so the testcontext can be accessed using a scoped before scenario hook? |
Same problem exists also for xUnit: https://groups.google.com/forum/#!topic/specflow/EmiG2gRkIz4 |
@githubdhina The delegate suggestion could be a solution. I have currently no plan at looking at this. Reason is, that we will start in the next 2 weeks with .NET Core support. This takes all available time and energy. |
@SabotageAndi would you be willing to accept/review a PR, or prefer to wait after the .NET core work is complete? |
@alexvv I will always accept/reviews PRs. It doesn't depend on our work on .NET Core. As long the .NET Core changes are not in the main branch, it's my problem to merge it back into. @gasparnagy Which way of implementation are you preffering? |
@alexvv any update ? |
@MummanaSubramanya a workaround would be to use 'BeforeStep' and a flag remembering it has already been called instead of 'BeforeScenario', as suggested by @gasparnagy's comment |
@alexvv any other work around ? |
Hi @SabotageAndi, I've submitted PR #1093 to solve this issue. Could you take a look at this please? @MummanaSubramanya are you sure you removed all [BeforeScenario] attributes in your code? If so, then unfortunately I don't know any other workaround. |
@alexvv I had a look at it and made some comments. |
…tions so that they are available in BeforeScenario hooks. Also added NUnit current TestContext scenario container registration. See SpecFlowOSS#936
…tions so that they are available in BeforeScenario hooks. Also added NUnit current TestContext scenario container registration. See SpecFlowOSS#936
resolved with #1093 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
We were using this partial class technique (https://stackoverflow.com/a/16891022) to hold the Microsoft.VisualStudio.TestTools.UnitTesting.TestContext in the TechTalk.SpecFlow.FeatureContext.
I have just upgraded to specflow 2.2.1. The test projects with features using this technique no longer compile as we have two TextContext properties (one in the generated feature.cs code and one in our custom partial file). This was introduced into 2.2.1 as part of the pull request #882. Looking at the Specflow generated feature.cs file I see the following generated code.
public virtual void ScenarioSetup(TechTalk.SpecFlow.ScenarioInfo scenarioInfo) { testRunner.OnScenarioStart(scenarioInfo); testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<Microsoft.VisualStudio.TestTools.UnitTesting.TestContext>(TestContext); }
But How do I now access the TestContext from my code?
After removing the custom partial class, when debugging my updated BeforeScenario hook code and from a Step Definition, I see the following:
--
SpecFlow Version: 2.2.1
Test Runner: MSTest
Visual Studio Version: 2013
.NET Framework: >= .NET 4
The text was updated successfully, but these errors were encountered: