❔ xUnit single setup and teardown

Hello,

So, I'm currently in the progress of switching from NUnit to XUnit and I was wondering how I could achieve this behaviour:

I have a test class containing around 10 integration tests. This class inherits from a base class which prepares test containers for this context.

The goal would be:

  • Call the base classes constructor once, which prepares the testcontainer
  • For every test, call an initialize to start the container before the test, and a teardown to stop the container after the test
  • After all 10 tests ran, call a single teardown which makes sure the container is gone.
With NUnit this was fairly easy, but I'm not entirely sure how to recreate this behaviour with xUnit, since it would create a new instance of the test class for every test, it also passes the testcontainer preparation logic for every invocation.

There are ways around this with static fields / properties, but I don't feel like this is the clean approach here.

Any advice?
Was this page helpful?