2016-03-15 54 views
0

我已装饰有[的TestFixture]属性和这个类包含装饰有方法的类[测试]属性,每个方法的签名是如何调用单元测试类在反射C#

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

也有设置和拆卸方法

[TestFixtureSetUp] 
    public void TestFixtureSetUp() 
    { 
    } 

    [SetUp] 
    public void TestSetUp() { } 

    [TearDown] 
    public void TestTearDown() 
    { 
    } 

    [TestFixtureTearDown] 
    public void TestFixtureTearDown() 
    { 
    } 

我怎样才能运行这个单元测试类通过反射在C#中?

谢谢你在先进

回答

0

喜欢的东西:

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}); 
    } 
} 
+0

非常感谢你,我会尝试 – user3132295