This question is about the unit testing framework xUnit.net.
I need to run some code before any test is executed, and also some code after all tests are done. I thought there should be some kind of attribute or marker interface to indicate the global initialization and termination code, but couldn't find them.
Alternatively, if I invoke xUnit programmatically, I can also achieve what I want with the following code:
static void Main()
{
try
{
MyGlobalSetup();
RunAllTests(); // What goes into this method?
}
finally
{
MyGlobalTeardown();
}
}
Can anyone provide me a hint about how to declaratively or programmatically run some global setup/teardown code?
As far as I know, xUnit does not have a global initialization/teardown extension point. However, it is easy to create one. Just create a base test class that implements
IDisposable
and do your initialization in the constructor and your teardown in theIDisposable.Dispose
method. This would look like this:However, the base class setup and teardown code will be executed for each call. This might not be what you want, as it is not very efficient. A more optimized version would use the
IClassFixture<T>
interface to ensure that the global initialization/teardown functionality is only called once. For this version, you don't extends a base class from your test class but implement theIClassFixture<T>
interface whereT
refers to your fixture class:This will result in the constructor of
TestsFixture
only being run once for every class under test. It thus depends on what you want exactly to choose between the two methods.There is an easy easy solution. Use the Fody.ModuleInit plugin
https://github.com/Fody/ModuleInit
It's a nuget package and when you install it it adds a new file called
ModuleInitializer.cs
to the project. There is one static method in here that gets weaved into the assembly after build and is run as soon as the assembly is loaded and before anything is run.I use this to unlock the software license to a library that I have purchased. I was always forgetting to unlock the license in each test and even forgetting to derive the test from a base class which would unlock it. The bright sparks that wrote this library, instead of telling you it was license locked introduced subtle numerical errors which causes tests to fail or pass when they shouldn't. You would never know if you had correctly unlocked the library or not. So now my module init looks like
and all tests that are placed into this assembly will have the license unlocked correctly for them.
I was looking for the same answer, and at this time the xUnit documentation is very helpful in regards to how to implement Class Fixtures and Collection Fixtures that give developers a wide range of setup/teardown functionality at the class or group of classes level. This is in line with the answer from Geir Sagberg, and gives good skeleton implementation to illustrate what it should look like.
https://xunit.github.io/docs/shared-context.html
To share SetUp/TearDown-code between multiple classes, you can use xUnit's CollectionFixture.
Quote: