简体   繁体   中英

Visual studio unit test for different implementation of interface

I need do unit testing for two implementation classes of one particular interface. The unit test class is generic covered all the necessary test for the interface. I want to instantiate the implementation class in test unit class TestInitialize method.

Is there any way I could force the test class run twice with different implementation class instance.

[TestClass]
public class MyFixture
{
    [TestInitialize()]
    public void MyTestInitialize()
    {
        ITest mockInstance = new TestImplement1();
        //ITest mockInstance = new TestImplement2();
    }

    [TestMethod]
    public void Test1 ()
    {
        mockInstance.Func1();
        ...  
    }

    [TestMethod]
    public void Test2 ()
    {
        ...  
    }

    ...other unit tests

 }

For this pattern, typically you would have a base test class with the test methods, and then you would subclass it and fill in the setup method. So it would become something like this: (I use NUnit, so I apologize if the test framework methods are a little off)

// don't mark this one as TestClass!
public abstract class MyBaseFixture
{
    protected ITest mockInstance;

    [TestMethod]
    public void Test1 ()
    {
        Assert(this.mockInstance.Func1() == 0);
    }
}

[TestClass]
public class MyConcreteFixture : MyBaseFixture
{
    [TestInitialize]
    public void Setup()
    {
        this.mockInstance = new ConcreteInstance1();
    }
}    

[TestClass]
public class MyOtherConcreteFixture : MyBaseFixture
{
    [TestInitialize]
    public void Setup()
    {
        this.mockInstance = new ConcreteInstance2();
    }
}

你应该查看Greg Young的界面不变NUnit插件: https//github.com/gregoryyoung/grensesnitt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM