1

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?"

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • I believe this is not possible via constructor arguments. See e.g. [this GitHub thread](https://github.com/xunit/xunit/issues/1908). It also recommends a workaround: reading configuration from a file. or some such. – Jeroen Sep 03 '19 at 19:49

1 Answers1

2

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> { ... }
E-Riz
  • 31,431
  • 9
  • 97
  • 134