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);
}