使用不同的参数testng依次执行多项测试 [英] Executing multiple test sequentially with different parameters testng

查看:126
本文介绍了使用不同的参数testng依次执行多项测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用数据提供程序按顺序运行具有不同参数的多个测试,基本上该场景是假设有5个测试完成一个测试流程,并且所有测试用例都应按顺序运行,假设在数据提供程序中返回的第一个参数为"air"那么所有5个测试都应使用参数"air"运行,然后使用第二个参数,依此类推.

目前在dataprovider中,如果假设参数是"air","earth",那么会发生什么情况,然后首先使用这两个参数执行测试,然后继续进行下一个测试.

所以我担心的是,所有测试应该首先使用第一个参数air运行,然后再次所有测试都应该使用下一个参数"earth"执行.

解决方案

所以我担心的是,所有测试应首先使用第一个参数air运行,然后所有测试应再次使用下一个参数"earth"执行.

这是我输入空气"和地球"得到的输出

Test-1 with data: Air
Test-2 with data: Air
Test-1 with data: Water
Test-2 with data: Water

测试类-RandomTest

public class RandomTest {
    private String str = "";

    public RandomTest(String str) {
    this.str = str;
    }

    @Test
    public void firstTest() {
    System.out.println("Test-1 with data: "+str);
    }

    @Test
    public void secondTest() {
    System.out.println("Test-2 with data: "+str);
}}

Factory类-SampleFactory

public class SampleFactory {
@Factory(dataProvider="dp")
public Object[] createInstances(String str) {
    return new Object[] {new RandomTest(str)};
}

@DataProvider(name="dp")
public static Object[][] createData() {
    return new Object[][] {
            new Object[] { new String("Air") },
            new Object[] { new String("Water") }
    };
}}

从testng.xml运行类SampleFactory, 请注意: group-by-instances ="true"

<suite name="Suite-A" verbose="1">
<test name="test" group-by-instances="true">
    <classes>
        <class name="tests.SampleFactory"></class>
    </classes>
</test>
</suite>

参考: http://testng.org/doc/documentation-main.html#工厂
参考: http://java.dzone.com/articles/testng-run-tests -顺序地

I was trying to run multiple test with different parameters sequentially using data providers, basically the scenario is suppose there are 5 test completing a test flow and all test cases should run in sequence suppose in data provider first parameter returned is "air" then all 5 test should run with parameter "air" and then second parameter and so on.

Currently in dataprovider what happens is if supposingly parameters are "air", "earth" then first test executes with both the parameters and then move on to next test.

So my concern is that all test should run first with first parameter air and then again all test should execute with next parameter "earth".

解决方案

So my concern is that all test should run first with first parameter air and then again all test should execute with next parameter "earth"

Here is the output that i got for the input "air" and "earth"

Test-1 with data: Air
Test-2 with data: Air
Test-1 with data: Water
Test-2 with data: Water

Test Class - RandomTest

public class RandomTest {
    private String str = "";

    public RandomTest(String str) {
    this.str = str;
    }

    @Test
    public void firstTest() {
    System.out.println("Test-1 with data: "+str);
    }

    @Test
    public void secondTest() {
    System.out.println("Test-2 with data: "+str);
}}

Factory class - SampleFactory

public class SampleFactory {
@Factory(dataProvider="dp")
public Object[] createInstances(String str) {
    return new Object[] {new RandomTest(str)};
}

@DataProvider(name="dp")
public static Object[][] createData() {
    return new Object[][] {
            new Object[] { new String("Air") },
            new Object[] { new String("Water") }
    };
}}

Run the class SampleFactory from testng.xml, Please note: group-by-instances="true"

<suite name="Suite-A" verbose="1">
<test name="test" group-by-instances="true">
    <classes>
        <class name="tests.SampleFactory"></class>
    </classes>
</test>
</suite>

Ref: http://testng.org/doc/documentation-main.html#factories
Ref: http://java.dzone.com/articles/testng-run-tests-sequentially

这篇关于使用不同的参数testng依次执行多项测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆