使用 NUnit,您可以执行以下操作:
[TestMethod]
public void TestDerpMethod(int a, string b, bool c)
{
//...test code...
}
您可以像这样执行多个测试用例:
[TestMethod]
[TestCase(12, "12", true)]
[TestCase(15, "15", false)]
public void TestDerpMethod(int a, string b, bool c)
{
//...test code...
}
您还可以使用此方法将此方法与 XML 一起使用:
<Rows>
<Row>
<A1>1</A1>
<A2>1</A2>
<Result>2</Result>
</Row>
<Row>
<A1>1</A1>
<A2>2</A2>
<Result>3</Result>
</Row>
<Row>
<A1>1</A1>
<A2>-1</A2>
<Result>1</Result>
</Row>
</Rows>
和 C#:
[TestMethod]
[DeploymentItem("ProjectName\\SumTestData.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"|DataDirectory|\\SumTestData.xml",
"Row",
DataAccessMethod.Sequential)]
public void SumTest()
{
int a1 = Int32.Parse((string)TestContext.DataRow["A1"]);
int a2 = Int32.Parse((string)TestContext.DataRow["A2"]);
int result = Int32.Parse((string)TestContext.DataRow["Result"]);
ExecSumTest(a1, a2, result);
}
private static void ExecSumTest(int a1, int a2, int result)
{
Assert.AreEqual(a1 + a2, result);
}