我正在学习单元测试,我正在尝试了解各个部分的组合。
假设我有这样的东西:
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() 或其他东西中构建它们?
最佳答案
创建参数化测试。这允许您在不同的项目上运行一个测试类:
http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/
这个甚至是关于汽车的:
http://techinsides.blogspot.com/2010/08/dont-repeat-yourself-junit-4s.html
或者不使用参数化运行器:
关于java - 在单元测试中组织测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011606/