My question is How to setup multiple fixtures in one test class?
But the constructor of Zoo class can not handle multiple fixtures.
For exemple:
public class Zoo : IClassFixture<Tiger>, IClassFixture<Wolf>, IClassFixure<Bird>
{
private IFixture fixture;
public Zoo(IFixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void TestAnimal()
{
//Arrange
int actualBonesCount = this.fixture.BonesCount;
int expectedBonesCount = 2;
//Act & Assert
Assert.Equal(expectedBonesCount, actualBonesCount );
}
}
A tiger class
public class Tiger : FixtureBase
{
public Tiger()
{
this.BonesCount = 4;
}
}
A bird class
public class Bird: FixtureBase
{
public Bird()
{
this.BonesCount = 2;
}
}
Test fixture base class
public class FixtureBase : IFixture
{
public int BonesCount { get; set; }
}
And interface
public interface IFixture
{
int BonesCount { get; set; }
}
First of all, let's recall when we want to use Class Fixtures in xUnit:
When to use: when you want to create a single test context and share it among all the tests in the class, and have it cleaned up after all the tests in the class have finished.
As you've said, you'd like to reuse methods from Zoo
test class to implementation tests. Since implementations use inheritance, why not use inheritance for test classes too?
public abstract class Zoo
{
protected IFixture Fixture;
[Fact]
public void TestAnimal()
{
//Arrange
int actualBonesCount = Fixture.BonesCount;
int expectedBonesCount = 2;
//Act & Assert
Assert.Equal(expectedBonesCount, actualBonesCount);
}
}
public class BirdTests : Zoo, IClassFixture<Bird>
{
public BirdTests(Bird fixture)
{
Fixture = fixture;
}
}
public class TigerTests : Zoo, IClassFixture<Tiger>
{
public BirdTests(Tiger fixture)
{
Fixture = fixture;
}
}
Still, I don't really get how would you like for each test to pass as you've hardcoded BonesCount
to 2 in a generic test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With