I recently started using XUnit. I’m still discovering what is possible using this unit test framework.
Before I was using NUnit, although I really liked it I always forgot what exact attribute I needed to control the lifecycle of my tests. I had to look up in the documentation if I needed a SetupFixture, a OneTimeSetup or a Setup attribute.
In XUnit there is a lot less magic going on and you can fallback to standard .NET idioms like the usage of the constructor and the IDisposable.Dispose() method.
With these 2 you can already get quite far, but if necessary you can also use
Before I was using NUnit, although I really liked it I always forgot what exact attribute I needed to control the lifecycle of my tests. I had to look up in the documentation if I needed a SetupFixture, a OneTimeSetup or a Setup attribute.
In XUnit there is a lot less magic going on and you can fallback to standard .NET idioms like the usage of the constructor and the IDisposable.Dispose() method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MailTests : IDisposable | |
{ | |
private readonly ITestOutputHelper _testOutputHelper; | |
public MailValidationTests(ITestOutputHelper testOutputHelper) | |
{ | |
_testOutputHelper = testOutputHelper; | |
//xUnit.net creates a new instance of the test class for every test that is run, | |
// so this is a good place to add initialization logic that will be run before every single test. | |
} | |
[Fact] | |
public void Mail_MustHaveASubject() | |
{ | |
} | |
public void Dispose() | |
{ | |
//xUnit.net disposes the class instance after every test that is run, | |
// so this is a good place to add cleanup logic that will be run after every single test. | |
} | |
} |
With these 2 you can already get quite far, but if necessary you can also use
- Class Fixtures: shared object instance across tests in a single class
- Collection Fixtures: shared object instances across multiple test classes