Using xUnit.net what would be a clean (readable/understandable and maintainable) way to reuse the same test for multiple implementations of the same interface?
The act and assert part of my tests are always the same (because all implementations of the interface are supposed to behave the same). Just the SUT is different for each test run and for some particular tests the arrange part is slightly different.
For example I have multiple implementations (MemoryRepository
, FileReposity
...) of the following interface.
interface IRepository
{
object GetById(string id);
void Set(string id, object value);
}
Now my tests are supposed to assure that all implementations behave the same:
// all implementations of IRepository must behave like this
// so do this test for all implementations
[Fact]
public void GetRetrievesWhatWasPut()
{
IRepository sut = new MemoryRepository();
sut.Set("key", 10);
var result = sut.Get("key");
result.Should().Be(10);
}
You could consider writing test classes as generic classes:
public abstract class IntervalFacts<T>
{
[Theory, AutoCatalogData]
public void MinimumIsCorrect(IComparable<T> first,
IComparable<T> second)
{
var sut = new Interval<T>(first, second);
IComparable<T> result = sut.Minimum;
Assert.Equal(result, first);
}
}
public class DecimalIntervalFacts : IntervalFacts<decimal> { }
public class StringIntervalFacts : IntervalFacts<string> { }
public class DateTimeIntervalFacts : IntervalFacts<DateTime> { }
public class TimSpanIntervalFacts : IntervalFacts<TimeSpan> { }
This particular example takes advantage of AutoFixture's ability to compose concrete classes, which saves you the trouble of instantiating concrete instances of each T
.
Varying the arrange phase is more difficult, but depending on what you need to do, again, you may be able to introduce some conventions to AutoFixture so that it automatically varies the created instances based on type.
For the types in the OP, you could write the tests like this:
public abstract class RepositoryFacts<T> where T : IRepository
{
[Theory, AutoRepositoryData]
public void GetRetrievesWhatWasPut(T sut)
{
sut.Set("key", 10);
var result = sut.Get("key");
result.Should().Be(10);
}
}
public class MemoryRepositoryFacts : RepositoryFacts<MemoryRepository> { }
public class FileReposityRepositoryFacts : RepositoryFacts<FileReposity> { }
You can test multiple implements by having a abstract method like GetThingToBeTested()
public abstract class Tester
{
public abstract Func<string, string, bool> GetThingToBeTested();
[Theory]
[InlineData("carrot", "tarroc")]
[InlineData("apple", "papel")]
public void Will_be_a_permutation(string original, string valueToTest)
{
GetThingToBeTested()(original, valueToTest).Should().BeTrue();
}
[Theory]
[InlineData("hello", "llloh")]
public void Will_not_be_a_permutation(string original, string valueToTest)
{
GetThingToBeTested()(original, valueToTest).Should().BeFalse();
}
}
public class Sort : Tester
{
public override Func<string, string, bool> GetThingToBeTested()
{
return IsPermutation_Sort;
}
}
public class Count : Tester
{
public override Func<string, string, bool> GetThingToBeTested()
{
return IsPermutation_Count;
}
}
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