Adam Drew Adam Drew - 2 months ago 15
C# Question

NUnit Generic test fixture not displaying when parameter implements generic interface

I have an interface defined like so:

public interface ITaskGenerator<T> where T : ITaskRequest
{
int CreateTask(T request);
}


and an implementer:

public class OrderTaskGenerator : ITaskGenerator<OrderTaskRequest>
{
public int CreateTask(OrderTaskRequest request)
{
..
}
}


What I want to do is make a generic test fixture using NUnit, that I can just specify all of the implementers of the
ITaskGenerator<ITaskRequest>
interface, to run the contract tests for all of them.

Luckily, NUnit has the ability to do that, with the Generic Test Fixture specified here . I've written the following (the
[TestFixture(typeof(OrderTaskGenerator))]
attribute is what's important for NUnit to pass through the correct type):

[TestFixture(typeof(OrderTaskGenerator))]
public class ITaskGeneratorContract<T> where T : ITaskGenerator<ITaskRequest>, new()
{

[Test]
public void CreateTask_Returns_InvalidException_If_Task_ID_Is_Empty()
{
// Test goes here
}
}


However, this doesn't show up in Test explorer. If I make the interface non-generic it does.

So does anyone know a work around to this, or is there something else that I'm missing. I think it's because
ITaskGenerator
is a generic interface, but I could be wrong.

Answer Source

Try this:

[TestFixture(typeof(OrderTaskGenerator), typeof(OrderTaskRequest))]
public class TaskGeneratorContractTest<T, TRequest> where T : ITaskGenerator<TRequest>, new() where TRequest : ITaskRequest
{
    private T _sut;

    public TaskGeneratorContractTest()
    {
        _sut = new T();
    }
    [Test]
    public void CreateTask_Returns_InvalidException_If_Task_ID_Is_Empty()
    {
        Assert.IsTrue(true);
    }
}