|
I try to add Reqnroll tests within my existing TUnit project, and want all the tests to use the same ASP.NET Core test server (using a My current setup uses TUnit’s I’ve tried a few things, but for now, nothing works. Is there a way to make this mechanism work with Reqnroll ? Or do you have alternative ideas to share this instance between all the tests ? |
Replies: 3 comments
|
Is maybe https://docs.reqnroll.net/latest/automation/context-injection.html what you're looking for? |
|
In theory yes. But the problem is that I need to inject a class that should be created by the TUnit framework. The standard way to do that is by using either constructor injection or property injection. https://tunit.dev/docs/writing-tests/class-data-source This works well in a class containing normal tests. But when the test is executed by Reqnroll, the injection doesn't work. So I don't know if I'm missing something or if it's impossible to do it. As far as I know, this injection is done using a code generator. So if the class is not instantiated by TUnit, then it seems normal that it doesn't work. |
|
Ok, I discovered by chance that you can do property injection in static properties. So I was able to make it work using this support class [Binding]
public class TUnitDataBridgeSupport
{
[ClassDataSource(Shared = [SharedType.PerTestSession])]
public static WebApplicationFactory? Factory
{
get;
set;
}
[BeforeTestRun]
public static void RegisterFactoryInContainer(IObjectContainer objectContainer)
{
if (Factory == null)
{
throw new InvalidOperationException("Factory should have been initialized by the test framework before the test run starts.");
}
objectContainer.RegisterInstanceAs(Factory);
}
} |
Ok, I discovered by chance that you can do property injection in static properties. So I was able to make it work using this support class