Running two identical tests with different arguments

I have a test with 15-20 different test cases, I want to run the same test twice with two different parameters that must be passed to the method of the BeforeClass method, for example:

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 } 

As you can see, the only difference between the two tests is the installation method, which passes different values ​​to the ClassToTest constructor. I do not want to replicate testing methods in both classes, but would prefer either inheritance or some other reasonable way to achieve this in one class.

+6
source share
5 answers

This seems like the perfect fit for JUnit4 @Parameters ; see https://blogs.oracle.com/jacobc/entry/parameterized_unit_tests_with_junit or http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/ . However, you will have to move the initialization from the setUp method to the constructor for the test class.

+6
source

For what it's worth, here's how you do it with 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 } } 
+1
source

Thank you all for your quick answers. Here is how I did it finally

 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 is an abstract class that has all the tests, and FirstTest and SecondTest create their own objects with different parameters and pass them to the abstract class to use it.

+1
source

According to the documentation (http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html):

A subclass does not inherit private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, they can also be used by the subclass.

0
source

How about this:

 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 } .... } 

EDIT: Or save the countdown method:

 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 } } 
0
source

Source: https://habr.com/ru/post/911961/


All Articles