TestNG:我怎样才能多次运行相同的测试用例?

Fei*_*Liu 30 testng

我想多次运行一个测试用例.这是可配置的testng.xml吗?如果我在测试方法中添加一个循环,那么每次运行的结果都不会在testng报告中受到影响.

nih*_*neo 41

您无法从xml中执行此操作,但在@Test注释中 - 您可以添加一个invocationCount属性,其中包含您要运行它的次数.它将在报告中运行许多测试时出现.

例如.

@Test(invocationCount = 10)
public void testCount() {..}
Run Code Online (Sandbox Code Playgroud)

你错过了最后的大括号,所以一个小的修正.


小智 8

TestNg有一种方法.您可以使用此方法并多次运行测试用例:

@Test(invocationCount = 100)

public void testCount() {

}
Run Code Online (Sandbox Code Playgroud)


Joh*_*hir 7

到目前为止,没有一个答案真正让用户能够从 testng 文件中增加调用计数,这正是所要求的。该解决方案搭载了 gaurav25 的 DataProvider 解决方案。

class TestClass() {
    int invocations;

    @Parameters({ "invocationCount" })
    @BeginClass
    void BeginClass( @Optional("1") String invocationCount) {
        this.invocations = Ingeter.parse(invocationCount)
    }

    // It will return a 2D array of size 3x1
    @DataProvider(name="URLprovider")
    private Object[][] getURLs() {
        ArrayList<Object []> obj = new ArrayList<>(3 * this.invocations);

        for(int iCount = 0; iCount < this.invocations; ++iCount) {
            list.add( new Object[] {"https://www.google.co.in/"} );
            list.add( new Object[] {"http://www.gmail.com/"} );
            list.add( new Object[] {"http://stackoverflow.com/"} );
        }

        return list.toArray();
    }

    /* Since Data provider for this test method returns 2D array of size
     (3*invocations)x1, this test method will run 3*invocations 
     times **automatically** with 1 parameter every time. */
    @Test(dataProvider="URLprovider")
    private void notePrice(String url) {
        driver.get(url);
        System.out.println(driver.getTitle());  
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用此 testng.xml 文件更改通过测试功能运行的测试集数量:

<suite name="ESFService" verbose="1" parallel="methods" thread-count="1" data-provider-thread-count="10" >
    <test name="Basic">
        <classes>
            <class name="TestClass">
                <parameter name="invocationCount" value="5"/>
            </class>
        </classes>
    </test>
</suite>
Run Code Online (Sandbox Code Playgroud)