繁体   English   中英

如何在反射C#中调用单元测试类

[英]How to invoke unit test class in reflection c#

我有一个用[TestFixture]属性修饰的类,该类包含用[Test]属性修饰的方法,每个方法签名是

public void MethodName([ValueSource("TestConfigurations")] TestConfiguration tConf)

也有设置和拆卸方法

    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {
    }

    [SetUp]
    public void TestSetUp() { }

    [TearDown]
    public void TestTearDown()
    {
    }

    [TestFixtureTearDown]
    public void TestFixtureTearDown()
    {
    }

如何在C#中通过反射运行此单元测试类?

谢谢高级

就像是:

public static class RunUnitTestsClass<TUnitTests> where TUnitTests : new()
{
    private static IEnumerable<MethodInfo> WithAttribute<TAttribute>()
    {
        return typeof(TUnitTests).GetMethods().Where(method => method.GetCustomAttributes(typeof(TAttribute), true).Any());
    }

    private static void RunWithAttribute<TAttribute>()
    {
        var unitTests = new TUnitTests();
        foreach (var method in WithAttribute<TAttribute>())
            method.Invoke(unitTests, new object[0]);
    }

    public static void RunTestFixtureSetup()
    {
        RunWithAttribute<TestFixtureSetUp>();
    }

    // same for the rest of them

    public static void RunTests(TestConfiguration tConf)
    {
        var unitTests = new TUnitTests();
        foreach (var method in WithAttribute<Test>())
            method.Invoke(unitTests, new []{tConf});
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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