NUnit TestCase with Generics

后端 未结 9 828
梦毁少年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:46

    Start with the test first--even when testing. What do you want to do? Probably something like this:

    [Test]
    public void Test_GenericCalls()
    {
        MyMethod_GenericCall_MakesGenericCall("an int response");
        MyMethod_GenericCall_MakesGenericCall("a string response");
          :
    }
    

    Then you can just make your test a plain old function test. No [Test] marker.

    public void MyMethod_GenericCall_MakesGenericCall(string expectedResponse)
    {
        // Arrange
    
        // Act
        var response = MyClassUnderTest.MyMethod();
    
        // Assert
        Assert.AreEqual(expectedResponse, response);
    }
    

提交回复
热议问题