it:ad:xunit:howto:develop_tests
Summary
XUnit Tests change quite a few things from IT:AD:NUnit.
Process
For one, you don't have to have a TestFixtureAttribute. Any class can have tests within it (not sure what the advantage of that really is, but still, interesting…)
The second big change isyou don't use [Test]s anymore – they're all [Fact]s.
That said, if you want to Setup/Teardown before every project, your class has to inherit from one of their base class (hence why I'm not sure what getting rid of the TestFixtureAttribute actually improved in terms of portability). When you do that, the class is discarded, and a new class is created on every test – but just to run that test.
In their simplest forms tests look like this:
using XUnit;
class ATestClass {
Stack<int> _classWideState;
public ATestClass()
{
// Use the Constructor as the equivalent to FixtureSetup
// This is state that is shared across all Facts:
_classWideState = new Stack<int>();
}
public void Dispose()
{
// Use the Disposer as the equivalent to FixtureTeardown
_classWideState.Dispose();
}
[Fact]
public void ATest(){
Assert.True(...);
}
}
using XUnit;
class ATestState {
public string Foo {get;set;}
}
class ATestClass : IClassFixture<ATestState>{
Stack<int> _classWideState;
public ATestClass()
{
// Use the Constructor as the equivalent to FixtureSetup
// This is state that is shared across all Facts:
_classWideState = new Stack<int>();
}
public void Dispose()
{
// Use the Disposer as the equivalent to FixtureTeardown
_classWideState.Dispose();
}
[Fact]
public void ATest(YourTestState state){
}
}