I have an integration testing setup in .NET using xUnit v3, Testcontainers (for SQL Server), and MassTransit. My goal is to have:
I’m also using WebApplicationFactory (inheriting my own IntegrationTestFactory class) and calling AddMassTransitTestHarness(...) to inject a test harness.
Because each test class shares the same ITestHarness instance, messages remain from prior test methods. That means my MassTransit consumers are not fully isolated between tests.
I noticed that the ITestHarness is registered as Singleton. Is it possible and supported to override the ITestHarness lifetime to be Scoped?
Any help would be greatly appreciated. Thanks!
You don't. Straight from the documentation. The test harness should not be used for more than one test.
Because these actions are performed asynchronously, MassTransit's test harness exposes several asynchronous collections allowing test assertions verifying developer expectations. These asynchronous collections are backed by an over test timer and an inactivity timer, so it's important to use a test harness only once for a given scenario. Multiple test assertions, messages, and behaviors are normal in a given test, but unrelated scenarios should not share a single test harness.
When using the Web Application Factory, that's a lot of startup/shutdown time, but focus on what's important to test with the WAF, and what can be tested in isolation without it to reduce overall test execution times.