可以看出,目前gTest(1.5版本)对参数化测试的支持还是有较多局限性的,体现在:
● 仅支持对被测函数是单参函数时使用。
● 一旦指定了参数类型,对于其他参数类型的被测方法则不能使用了。
● 无法指定每个输入参数值对应的期望返回值。
希望今后的版本会有所加强。
4.3.2.2 NUnit对参数化测试的支持
NUnit对参数化测试提供了很好的支持,其用法如下表所示。
NUnit对参数化测试的支持 |
|
Test Fixture和Test Method |
using NUnit.Framework; using Rhino.Mocks; [TestFixture] public class [ClassUnderTest]Test { [SetUp] public void SetUp() { ... } [TearDown] public void TearDown() { ... } [Test] public void [Feature]_[Scenario]_[ExpectedResult]() { ... } [RowTest] [Row(param1_1, param2_1, param3_1, expectedResult_1)] [Row(param1_2, param2_2, param3_2, expectedResult_2)] [Row(param1_3, param2_3, param3_3, expectedResult_3)] [ ... ] public void [Feature]_[Scenario]_[ExpectedResult](param1_type param1, param2_type param2, param3_type param3, result_type expectedResult) { Assert.XXX(param1, param2, param3, expectedResult); } } |
4.3.2.3 JUnit对参数化测试的支持
使用JUnit进行参数化测试的用法如下:
JUnit对参数化测试的支持 |
|
Test Fixture和Test Method |
import org.junit.*; import org.junit.rules.*; import org.junit.runner.*; import org.junit.runners.*; import static org.junit.Assert; import static org.easymock.EasyMock.*; // Test Fixture must be annotated with @RunWith. @RunWith(Parameterized.class) public class [ClassUnderTest]Test { private param1_type param1; private param2_type param2; ... private result_type expectedResult; // The data-preparation method must be annotated with @Parameters, be public static, have no arguments, and return Collection. @Parameters public static List<Object[]> prepareData() { Object [][] data = new Object[][]{ { param1_1, param2_1, ..., expectedResult_1 }, { param1_2, param2_2, ..., expectedResult_2 }, { ... }, }; return Arrays.asList(data); } // Test Fixture must have a constructor. public [ClassUnderTest]Test(param1_type param1, param2_type param2, ..., result_type expectedResult) { this.param1 = param1; this.param2 = param2; ... this.expectedResult = expectedResult; } @Test public void [Feature]_[Scenario]_[ExpectedResult]() { assertXXX(param1, param2, ..., expectedResult); } } |