Reputation: 33024
I'm trying to implement a collection fixture where the work that the fixture needs to perform (in its constructor) requires a parameter. I want the fixture to be generic/reusable and it needs to know the Assembly of the unit being tested.
How can I parameterize a collection fixture, such that tests in the collection can give the fixture this "context?"
Upvotes: 1
Views: 4059
Reputation: 33024
I ended up creating an abstract collection fixture, with a protected constructor that takes the necessary parameter (in my case, the Assembly
). Then I defined small subclasses that have a no-arg constructor that calls the inherited one with the correct argument.
public abstract class BaseCollectionFixture<TFixture> : ICollectionFixture<TFixture>
where TFixture : class
{
protected BaseCollectionFixture(Assembly assemblyUnderTest)
{
// Do my fixture stuff with the assembly
}
}
[CollectionDefinition("Special tests")]
public class ConcreteFixture : BaseCollectionFixture<ConcreteFixture>
{
public ConcreteFixture() : base(typeof(MyClassUnderTest).Assembly) {}
}
Then I use it in a test like this:
public MyClassTests<ConcreteFixture> { ... }
Upvotes: 2