Parameterized 中各方法的执行顺序

参数化测试(Parameterized Tests),即多次执行同一个测试方法,每次使用不同的参数。这里debug一个简单的例子来确定类中各方法的执行顺序。

执行顺序

setParam() -> @BeforeClass -> 
Parameterized (name1) -> @Before -> test1 ->@After ->
Parameterized (name1) -> @Before -> test2 ->@After ->
Parameterized (name2) -> @Before -> test1 ->@After ->
Parameterized (name2) -> @Before -> test2 ->@After ->
@AfterClass

附简单例子

import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;

@RunWith(Parameterized.class)
public class ParameterizedExampleTest {
    private final String testName;
    public static String[] testNames = {"name1", "name2"};

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("before class");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("after class");
    }

    @Before
    public void setUp() {
        System.out.println("before test");
    }

    @After
    public void tearDown() {
        System.out.println("after test");
    }

    @Parameterized.Parameters(name = "{index}: {0}")
    public static List<String> setParam() {
        System.out.println("setParam");
        return Arrays.asList(testNames);
    }

    public ParameterizedExampleTest(String testName) throws Exception {
        this.testName = testName;
        System.out.println("Parameterized method");

    }

    @Test
    public void test01() throws Exception {
        System.out.println("test01");
    }

    @Test
    public void test02() throws Exception {
        System.out.println("test02");
    }
}

 

posted @ 2023-08-30 12:03  panda4671  阅读(60)  评论(0编辑  收藏  举报