Creating multiple unit tests from a loop in Java

三世轮回 提交于 2019-12-24 08:17:39

问题


I'm writing unit tests for my (small) program, and the test cases are specified in around 30 different files.
To test it, all I need is a loop that goes over all the files, parse them, and execute what's needed.

The problem is that in that case, all of my tests will be considered as one, since it's all in the same function with the @Test notation.
Is it possible to split it somehow without having to have a separate function for each test file?

The problem with all the tests as one test case, is that I can't see which test case fail the process; and if one fails it fail the rest (I'll get 1 test failed instead of 5/30 failed)

I'm currently using JUnit (4.12), but I'm not obligated to keep using it, so I can switch framework if there's a better solution.

Thanks!

Example:

public class MyTests {
    @Test
    public void testFromFiles {
        // loop through all the files
    }
}

output: 1 test run successfully 

Update: The selected answer worked great for me, and I added another solution with JUnit 5 (instead of 4), in case it will help someone.


回答1:


Try this way:

@RunWith(Parameterized.class)
public class EdiTest {

    @SuppressWarnings("WeakerAccess")
    @Parameterized.Parameter(value = 0)
    public String model;

    @SuppressWarnings("WeakerAccess")
    @Parameterized.Parameter(value = 1)
    public String filename;

    @Parameterized.Parameters(name = "{index}: testEDI({0}, {1})")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {"753", "edi753_A.edi"},
                {"753", "edi753_B.edi"},
                {"754", "edi754.edi"},
                {"810", "edi810-withTax.edi"},
                {"810", "edi810-withoutTax.edi"},
        });
    }

    @Before
    public void setUpContext() throws Exception {
        TestContextManager testContextManager = new TestContextManager(getClass());
        testContextManager.prepareTestInstance(this);
    }

    @Test
    public void testEDI() throws IOException {
        String edi = IOUtils.toString(ClassLoader.getSystemResource(filename));
        EdiConverter driver = ediConverterProvider.getConverter(model);

        // your test code here
    }

}



回答2:


With JUnit 5 and Dynamic Tests:

  • Basic example:

    class DynamicTests {
    
      @TestFactory
      List<DynamicTest> createSomeTests() {
    
        return Arrays.asList(
          DynamicTest.dynamicTest("First dynamically created test",
            () -> assertTrue(true)),
    
          DynamicTest.dynamicTest("Second dynamically created test",
            () -> assertTrue(true))
        );
      }
    }
    
  • Example of getting and filtering all test files:

    class MyTestsClass {
        @TestFactory
        List<DynamicTest> runAllTestFiles() {
            List<DynamicTest> list = new ArrayList<DynamicTest>();
            try (Stream<Path> paths = Files.walk(Paths.get("tests_dir"))) {
                List<Path> files = paths
                        .filter(path -> path.getFileName().toString().endsWith(".mytests"))
                        .collect(Collectors.toList());
                files.forEach(file -> list.add(
                        DynamicTest.dynamicTest(
                            file.getFileName().toString(), 
                            () -> testFileWithSomeAsserts(file))
                ));
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return list;
        }
    }
    

The @TestFactory works differently than regular tests, and @BeforeEach / @AfterEach cannot be used in that case:

Dynamic Test Lifecycle

The execution lifecycle of a dynamic test is quite different than it is for a standard @Test case. Specifically, there are no lifecycle callbacks for individual dynamic tests. This means that @BeforeEach and @AfterEach methods and their corresponding extension callbacks are executed for the @TestFactory method but not for each dynamic test. In other words, if you access fields from the test instance within a lambda expression for a dynamic test, those fields will not be reset by callback methods or extensions between the execution of individual dynamic tests generated by the same @TestFactory method.

More examples in the official documentation.



来源:https://stackoverflow.com/questions/43830391/creating-multiple-unit-tests-from-a-loop-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!