簡體   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