如何使用 datarovider 按顺序 运行 测试

How to run tests in order with datarovider

我需要按顺序为每个数据提供者对象执行所有测试。 我的尝试:


public class TestTest extends BaseTest {

    private String creds;

    @Factory(dataProvider = "creds")
    TestTest(String creds){
        this.creds = creds;
    }

    @DataProvider
    public static Object[][] creds(){
        return new Object[][]{
                {"Creds1"},
                {"Creds2"}
        };
    }

    @Test()
    public void test1(){
        this.creds = creds;
        Log.info("test1 creds: " + creds);
    }

    @Test(dependsOnMethods = "test1")
    public void test2(){
        Log.info("test2 creds: " + this.creds);
    }
    @Test(dependsOnMethods = "test2")
    public void test3(){
        Log.info("test3 creds: " + this.creds);
    }
    
}


输出:

> test1 creds: Creds2 
> test1 creds: Creds1 
> test2 creds: Creds2 
> test2 creds: Creds1
> test3 creds: Creds2
> test3 creds: Creds1

但我需要:

> test1 creds: Creds1 
> test2 creds: Creds1 
> test3 creds: Creds1 
> test1 creds: Creds2
> test2 creds: Creds2
> test3 creds: Creds3

最简单的是删除 @Test 并制作一个新的 @Test public void test123()。处理失败有点困难,但是,上下文更清楚:之前发生了什么,预期的管道是什么。

缺点:统计。

@Test
public void test123() {
    test1();
    test2();
    test3();
}

private void test1() {
    this.creds = creds;
    Log.info("test1 creds: " + creds);
}

private void test2() {
    Log.info("test2 creds: " + this.creds);
}

private void test3() {
    Log.info("test3 creds: " + this.creds);
}

尝试使用 @Factory 注释并通过测试 class 的构造函数创建对象(具有所需的字段值),并使用传递的字段值调用该 class 的所有测试方法。

需要在xml测试文件中添加group-by-instances="true"参数:

    <test name="test" group-by-instances="true">
        <classes>
            <class name="tests.users.TestTest"/>
        </classes>
    </test>

当然,需要通过 XML(来自 maven/Gradle 任务)执行。