多个测试类在MSTest中使用相同的类型fixture

时间:2011-08-18 15:22:38

标签: c# mstest

为了将MbUnit中的TypeFixtures迁移到MSTest,我们可以为测试方法和其他一些继承构造函数包含原始TypeFixture定义的抽象类的类构建一个抽象类。

现在的问题是,在最初的MbUnit测试中,有多个测试类使用同一组TypeFixture。这样做的方法是拥有一个包含这些fixture的基类,并拥有实际的测试类来继承它。

我不认为这适用于MSTest。 MSTest中是否有任何解决方法可以实现相同的目标?谢谢!

1 个答案:

答案 0 :(得分:0)

不好,但你可以使用反射。你将获得红色/绿色pr类而不是方法,这意味着它更难导航到破坏测试:

[TestClass]
public class RunMbUnitTests
{
    [TestMethod]
    public void RunAllTests()
    {
        var fixture = new YourMbUnitFixture();
        foreach (var method in GetTestMethods(fixture))
        {
            method.Invoke(fixture);
        }
    }

    private IEnumerable<MethodInfo> GetTestMethods(object mbUnitFixture)
    {
        //reflection code to return all test methods from the MbUnit fixture
    }
}