Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing multiple implementations of an interface in a single test class

I need to pass test data on class level but Theory and InlineData attributes can only be used on methods.

public class ContainerTests : TestFixture
{
    private IContainer _container;

    public ContainerTests(string containerName)
    {
        _container = CreateContainer(containerName);
    }

    [Fact]
    public void ResolveJobFactory()
    {
        IJobFactory jobFactory = _container.Resolve<IJobFactory>();
    }

    private IContainer CreateContainer(string containerName)
    {
        if (containerName == "CastleWindsor")
        {
            return new WindsorContainerAdapter();
        }
        //other adapters

        throw new NotImplementedException();
    }
}

Is there a way to achieve something similar in xUnit.net?

like image 834
Ufuk Hacıoğulları Avatar asked May 17 '15 11:05

Ufuk Hacıoğulları


People also ask

CAN interface have multiple implementations?

Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

Can a unit test test multiple classes?

A test can exercise methods from three objects and still be a unit test. Some may claim that's integration but three lines of code are just as "integrated" so that's not really a thing. Method and object boundaries don't present any significant barrier to testing.

What is single unit testing?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.


1 Answers

Testing multiple implementations of an interface in a single test class

Don't! Have a test class per implementation.

Edit: As per comments, I recommend having a base TestClass with all of the common tests, and then a Test Class per implementation which inherits from this base class.

like image 99
Chris Avatar answered Sep 29 '22 09:09

Chris