-
Notifications
You must be signed in to change notification settings - Fork 53
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
How to get IReqnrollOutputHelper in BeforeTestRun? #247
Comments
Not easy. I believe your solution was only working because you haven't used parallel execution (can you confirm?). The challenge is that the test logging infrastructure provided by the test runners (mstest, nunit, xUnit) is bound to a test execution, so you cannot "remember" the output helper in a global service, because it would not know which test's logging infrastructure to be used. This is because if you run the tests parallel, there is no single "current" test, whose logging infrastructure could be used. So basically what you did (because of the bug we fixed in #59) was picking the first execution worker thread and used the "current" test's logger within that worker thread. If you would like to mimic the same behavior (that works only when you don't use parallel), you can do with a before scenario hook:
For a real global service, all interfaces of your service should get the actual context and retrieve the output helper from it instead of storing it:
|
Thank you very much for the detailed answer! You are right, the tests are executed sequentially. I guess it's the best to avoid logging in the way I did and maybe I can also activate parallel execution. Thanks again. I guess the docs doesn't need to be updated with this very specific topic so I close this issue. |
@gasparnagy |
@StefH You mean the before scenario hook code? Can you try with |
Note that I'm using Reqnroll.Microsoft.Extensions.DependencyInjection and this is my code: [Binding]
public class Startup
{
private static IReqnrollOutputHelper? _outputHelper;
protected Startup()
{
}
[BeforeScenario]
public static void Before(TestThreadContext testThreadContext)
{
if (_outputHelper != null)
{
return;
}
_outputHelper = testThreadContext.TestThreadContainer.Resolve<IReqnrollOutputHelper>();
}
[ScenarioDependencies]
public static IServiceCollection CreateServices()
{
return new ServiceCollection()
.AddLogging(builder => builder
.AddProvider(new ReqnrollLoggerProvider(_outputHelper!)))
.AddTransient<Calculator>();
}
} When using If I use
The only code which does work is like: [BeforeTestRun]
public static void BeforeTestRun(ITestRunnerManager testRunnerManager)
{
if (_outputHelper != null)
{
return;
}
var runner = testRunnerManager.GetTestRunner();
var context = runner.TestThreadContext;
_outputHelper = context.TestThreadContainer.Resolve<IReqnrollOutputHelper>();
testRunnerManager.ReleaseTestThreadContext(context);
} Although I'm not sure this is the correct solution.... |
@StefH which test execution fw (mstest, nunit, xunit) do you use? |
@StefH and do you (plan) to run your tests parallel? |
|
@StefH So the problem is that with xUnit the The proper solution would be (or should be) that the Reqnroll.Microsoft.Extensions.DependencyInjection plugin should call this Of course, as you run your tests non-parallel, this thing could be patched somehow. I think your workaround of calling You can make a quick test by saving the runner created by the Nevertheless, your case shows an interesting use-case that is not properly supported by the Reqnroll.Microsoft.Extensions.DependencyInjection plugin, so please create a separate issue (about how to access |
Thank you for this extended explanation. The reason I ask this is because I'm creating a simple project which will implement a ILogger which uses this IReqnrollOutputHelper , so that I can forward the logging and see this in the test result.
I need to think on this how to correctly formulate this. |
@StefH The reason statement you mentioned is just enough and please link your comment with the code example. It will mainly serve as a reminder for us that we don't forget, so no need to be super detailed. |
@gasparnagy Link to my project pull request is here: (Maybe when this solution is accepted, I can move/donate my project into this repository?) |
@StefH That's cool! Are you using the |
The unit under tests uses a ILogger, so in that case the logging is forwarded to IReqnrollOutputHelper. I'm intending to use the ILogger in my Reqnroll unit tests, in that case I'll just use IReqnrollOutputHelper to write some output. I'll take a look if I can make a PR in this repository for this logger. |
@gasparnagy |
Related Documentation Page
automation/hooks.md
Type of the problem
Missing information
Problem Description
In #59 it says:
I just migrated to Reqnroll (awesome work btw!) and I'm impacted by this. Before I used the
SpecFlowOutputHelper
in aBeforeTestRun
hook to configure some global services whichobjectContainer
Is there any other way to get the output helper for global services?
The text was updated successfully, but these errors were encountered: