Skip to content

Dispose a test's container when its case has run - #56

Merged
ipjohnson merged 1 commit into
mainfrom
dispose-container-per-case
Sep 5, 2026
Merged

ipjohnson merged 1 commit into
mainfrom
dispose-container-per-case

Conversation

@ipjohnson

Copy link
Copy Markdown
Owner

What

ModuleTestCase now implements ISelfExecutingXunitTestCase: it runs the case the way xUnit would have, through XunitRunnerHelper.RunXunitTestCase, and disposes every provider it built in a finally once the run returns.

Why

The provider went into the case's DisposalTracker, and xUnit disposes a test case only in InProcessFrontController.FindAndRun, after Run has returned for every test case in the assembly. So every container a run built stayed alive, with every singleton in it, until the run ended. A probe in Hardened.Framework that handed three [HardenedTest] providers to an [assembly: AssemblyFixture] found all three undisposed at the end of the run. The NUnit integration has always disposed in ModuleTestCommand's finally, and its IterationLifetimeTests holds it to that; nothing held xUnit to it.

Hardened is about to add test hosts that bind a loopback socket per test, and a listener per test held open until the last test is the leak that has to be ruled out first.

Notes

  • Per case, which for every test but a data-driven one is per test. The rows of a data-driven test share the case and are released together when the last has run. Per row would need a case runner of this package's own overriding RunTest; not done here.
  • ContainerLifetimeTests in integ-tests/SutProject.Tests mirrors the NUnit IterationLifetimeTests: two tests in one class, whichever runs second asserts the first's container was disposed. It fails on main (Assert.Contains() Failure: Item not found in collection: []) and passes here, on both target frameworks.
  • Hardened.Framework's whole suite, 7054 tests, passes with this runner wired in through the sibling-checkout project reference.
  • The xUnit public API snapshot gains the interface and the Run method.
  • No CHANGELOG entry, following Seed the test's environment before its modules apply #54; the cut commit carries it. Hardened takes the bump, so a 1.3.1 cut is what it waits on.

🤖 Generated with Claude Code

ModuleTestCase handed each provider to the case's DisposalTracker, and
xUnit disposes a test case only in InProcessFrontController.FindAndRun,
after every case in the assembly has run. Every container a run built
stayed alive, with every singleton in it, until the run ended. The case
now executes itself and disposes what it built in a finally once its
run returns, per case; NUnit's ModuleTestCommand has always done this
in its own finally.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@ipjohnson
ipjohnson merged commit cef41ce into main Sep 5, 2026
2 checks passed
ipjohnson added a commit that referenced this pull request Sep 5, 2026
Version and changelog for #56. A patch: no public API changes shape, the
xUnit test case gains an interface it already satisfied in spirit.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant