最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I isolate MassTransit test harness per test when using a shared SQL container and scoped transactions in xUnit? - Stack O

programmeradmin1浏览0评论

I have an integration testing setup in .NET using xUnit v3, Testcontainers (for SQL Server), and MassTransit. My goal is to have:

  1. A single SQL container spun up once at the assembly level (using [AssemblyFixture]).
  2. Each test class uses its own database (created when the class starts).
  3. Each test method runs inside its own transaction (rolled back after the method finishes).

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!

I have an integration testing setup in .NET using xUnit v3, Testcontainers (for SQL Server), and MassTransit. My goal is to have:

  1. A single SQL container spun up once at the assembly level (using [AssemblyFixture]).
  2. Each test class uses its own database (created when the class starts).
  3. Each test method runs inside its own transaction (rolled back after the method finishes).

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!

Share Improve this question asked Mar 14 at 16:10 geinkgeink 658 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论