NUnit TestCase with Generics

后端 未结 9 1073
梦毁少年i
梦毁少年i 2021-01-31 13:53

Is there any way to pass generic types using a TestCase to a test in NUnit?

This is what I would like to do but the syntax is not correct...

<         


        
9条回答
  •  眼角桃花
    2021-01-31 14:40

    Attributes in C# cannot be generic, so you won't be able to do things exactly as you'd like. Perhaps the easiest thing would be to put TestCase attributes onto a helper method which uses reflection to call the real method. Something like this might work (note, untested):

        [TestCase(typeof(MyClass), "SomeResponse")]
        public void TestWrapper(Type t, string s)
        {
            typeof(MyClassUnderTest).GetMethod("MyMethod_GenericCall_MakesGenericCall").MakeGenericMethod(t).Invoke(null, new [] { s });
        }
    

提交回复
热议问题