我正在学习单元测试,而我正在努力了解各个部分的组合点。
说我有类似的东西:
public class CarTest {
public CarTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of toString method, of class Car.
*/
@Test
public void testToString() {
System.out.println("toString");
Car instance = new Car(1, 2);
String expResult = "1/2";
String result = instance.toString();
assertEquals(expResult, result);
}
}
说我需要测试不同的汽车。也许一个构造函数的调用应该有两个负数,等等。不同的“案例”在哪里?我需要在每个上运行testToString(),我不想为我需要运行的每个方法复制和粘贴5个不同的汽车/断言。我是否将它们全部存储在测试类的数组中并将它们构建在每个setUp()或其他内容中?
答案 0 :(得分:1)
创建参数化测试。这允许您在不同的项目上运行一个测试类:
http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/
这个甚至是汽车:
http://techinsides.blogspot.com/2010/08/dont-repeat-yourself-junit-4s.html
或者不使用参数化跑步者: