我有15-20个不同测试用例的测试,我想用两个不同的参数运行相同的测试,这些参数应该被传递给测试的BeforeClass方法,例如:
public class TestOne {
private static ClassToTest classToTest;
@BeforeClass
public static void setUp() throws Exception {
classToTest = new ClassToTest("Argument1", "Argument2");
}
@Test
public void testOne() {
........roughly 15 - 20 tests here
}
public class TestTwo {
private static ClassToTest classToTest;
@BeforeClass
public static void setUp() throws Exception {
classToTest = new ClassToTest("Argument3", "Argument4");
}
@Test
public void testOne() {
........roughly 15 - 20 tests here, same as in TestOne
}
正如您所看到的,这两个测试之间的唯一区别在于setup方法,它将不同的值传递给ClassToTest的构造函数。我不想在两个类中复制测试方法,但更喜欢继承或其他一些智能方法在一个类中实现这一点。
答案 0 :(得分:6)
这似乎是JUnit4 @Parameters
的完美用例;请参阅https://blogs.oracle.com/jacobc/entry/parameterized_unit_tests_with_junit或http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/。也就是说,您必须将初始化从setUp方法移动到测试类的构造函数。
答案 1 :(得分:1)
对于它的价值,以下是使用TestNG:
的方法public class TestFactory {
@Factory
public Object[] createTests() {
return new Object[] {
new ClassToTest("arg1", "arg2"),
new ClassToTest("arg3", "arg4")
};
}
}
public class ClassToTest {
public ClassToTest(String arg1, String arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
@Test
public void testOne() {
// use arg1 and arg2
}
}
答案 2 :(得分:1)
感谢大家的快速回复。这就是我最终做到的方式
public abstract class Base {
final HeavyObject heavy;
protected Base(HeavyObject heavy) {
this.param = param;
}
@Test
public void test() {
param.doSomething();
}
@Test
.............More tests here
}
public class FirstTest extends Base{
private static HeavyObject param;
@BeforeClass
public static void init() {
param = new HeavyObject("arg1", "arg2");
}
public FirstTest() {
super(param);
}
}
public class SecondTest extends Base{
private static HeavyObject param;
@BeforeClass
public static void init() {
param = new HeavyObject("arg3", "arg4");
}
public FirstTest() {
super(param);
}
}
Base是一个抽象类,它包含所有测试,FirstTest和SecondTest使用不同的参数创建自己的对象,并将其传递给抽象类以使用它。
答案 3 :(得分:0)
根据文档(http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html):
子类不继承其父类的私有成员。 但是,如果超类具有公共或受保护的方法 访问其私有字段,子类也可以使用它们。
答案 4 :(得分:0)
这个怎么样:
public class TestOne {
private static ClassToTest classToTest1, classToTest2;
@BeforeClass
public static void setUp() throws Exception {
classToTest1 = new ClassToTest("Argument1", "Argument2");
classToTest2 = new ClassToTest("Argument3", "Argument4");
}
@Test
public void testOne() {
testOneImpl(classToTest1);
testOneImpl(classToTest2);
}
public void testOneImpl(ClassToTest classToTest) {
// exact samew as whatever your current testOne() test method is
}
....
}
编辑: 或者保持方法倒计时:
public class TestOne {
private static List<ClassToTest> classesToTest;
@BeforeClass
public static void setUp() throws Exception {
classesToTest = new ArrayList<>;
classesToTest.add( new ClassToTest("Argument1", "Argument2"));
classesToTest.add( new ClassToTest("Argument3", "Argument4"));
}
@Test
public void testOne() {
for (ClassToTest classToTest: classesToTest) {
... same test content as before
}
}