TestNG迭代测试数据而不是测试方法 [英] TestNG iterate over test data instead of test methods

查看:137
本文介绍了TestNG迭代测试数据而不是测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将DataProvider与多个TestNG方法一起使用时,每个方法都按顺序运行所有数据集。相反,我想迭代数据集并在每次迭代时执行所有方法。
我不在乎结果是显示每个测试方法的结果还是每个方法的运行摘要。

When using DataProvider with multiple TestNG methods, every method is run with all data sets in sequence. Instead I want to iterate over the data sets and execute all methods on each iteration. I do not care if the results show every single test method result or a summary of the runs per method.

我已经尝试过选项

order-by-instances="true"

$ b $在suite.xml中b

但没有成功。

in a suite.xml with no success.

示例代码:

public class TestNGTest
{
    @DataProvider(name = "dp")
    public Object[][] createData(Method m) {
      return new Object[][] { new Object[] { "Cedric" }, new Object[] {"Martina"}};
    }

    @Test(dataProvider = "dp")    
    public void test1(String s) throws InterruptedException {
        System.out.println("test1 " + s);
        Thread.sleep(1000);
    }

    @Test(dataProvider = "dp")
    public void test2(String s) throws InterruptedException {
        System.out.println("test2 " + s);
        Thread.sleep(1000);
    }
}

实际结果:

test1 Cedric
test1 Martina
test2 Cedric
test2 Martina
PASSED: test1("Cedric")
PASSED: test1("Martina")
PASSED: test2("Cedric")
PASSED: test2("Martina")

通缉结果:

test1 Cedric
test2 Cedric
test1 Martina
test2 Martina
PASSED: test1("Cedric")
PASSED: test2("Cedric")
PASSED: test1("Martina")
PASSED: test2("Martina")


推荐答案

请试试跟随侦听器GroupByInstanceEnabler。你可以把这个监听器放在你的测试类(或测试基类,如果有的话)中,或者只是更简单和更好的解决方案是将它放在META-INF中让TestNg使用ServiceLoader加载它( http://testng.org/doc/documentation-main.html#listeners-service-loader

Please try with following listener GroupByInstanceEnabler. You can put this listener in Listeners annotation in your test class (or test base class if have such) or just even simpler and better solution is to put it in META-INF to let TestNg load it using ServiceLoader (http://testng.org/doc/documentation-main.html#listeners-service-loader)

这将允许您摆脱suite.xml,并且只允许您在类路径上保留此META-INF和启用程序所需的内容。任何时候你都会运行任何测试,这将被加载 - 不需要配置任何像IDE,创建套件来运行 - 它总是会加载您的听众开箱即用。

This will allow you to get rid of suite.xml and only what you will need to keep this META-INF and enabler on your classpath. Anytime you will run any test this will be loaded - not need to configure anything like IDE, create suites to run - it always will load your listener out-of-the-box.

import org.testng.ISuite;
import org.testng.ISuiteListener;

public class GroupByInstanceEnabler implements ISuiteListener {

    @Override
    public void onStart(ISuite suite) {
        suite.getXmlSuite().setGroupByInstances(true);
    }

    @Override
    public void onFinish(ISuite suite) {

    }
}

Pawel

这篇关于TestNG迭代测试数据而不是测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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