简体   繁体   中英

How execute code after each dynamic test?

There is a test:

package com.cdek.qa_auto.config;
import com.cdek.qa_auto.utils.CdekJUnitListener;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.core.LauncherFactory;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;


/***
 *
 */
@SpringBootTest
public class JUnit5Test {
    public JUnit5Test() throws Exception {}

    @BeforeEach
    public void beforeEach() throws Exception {
        Launcher launcher = LauncherFactory.create();
        TestExecutionListener listener = new CdekJUnitListener();
        launcher.registerTestExecutionListeners(listener);
    }

    @TestFactory
    public Stream<DynamicTest> test() throws Exception {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("12");
        list.add("123");
        list.add("1234");
        list.add("12345");

        return list.stream().map(item -> (
                dynamicTest("test_" + item, () -> {
                    if ("1".equalsIgnoreCase(item)) {
                        System.out.println("fail");
                        fail("fail");
                    } else if ("12".equalsIgnoreCase(item)) {
                        assertTrue(false);
                    } else if ("123".equalsIgnoreCase(item)) {
                        throw new Exception("msg");
                    } else {
                        assertTrue(true);
                    }
                        }
                )));
    }
}

For example, make a screen for fallen tests. Written implementation of import org.junit.platform.launcher.TestExecutionListener.

Connect so normally did not work. Does not go into executionFinished.

Basis: JUnit5-Maven-SpringBoot

How do execute specific code after each dynamic test?

As stated in the JUnit 5 User Guide :

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.

Thus, you cannot use an @AfterEach method or one of the "after" lifecycle callback extensions (ie, AfterEachCallback or AfterTestExecutionCallback ).

Depending on what you are trying to achieve in your "listener", you may be able to accomplish that in a TestExecutionListener , but you cannot register that from within the test class. See Plugging in your own Test Execution Listener in the User Guide for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM