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

时间:2014-09-30 19:09:28

标签: testng

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

8 个答案:

答案 0 :(得分:34)

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

例如

@Test(invocationCount = 10)
public void testCount() {..}

你最后错过了结束大括号,所以做了一个很小的修正。

答案 1 :(得分:5)

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

@Test(invocationCount = 100)

public void testCount() {

}

答案 2 :(得分:4)

到目前为止,没有一个答案真正让用户能够从testng文件中提取调用计数,这就是所要求的。这个解决方案背负着gaurav25的DataProvider解决方案。

class TestClass() {
    int invocationCount;

    @Parameters({ "invocationCount" })
    @BeginClass
    void BeginClass( @Optional("1") String invocationCount) {
        this.invocationCount = 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.invocationCount);

        for(int iCount = 0; iCount < this.invocationCount; ++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*invocationCount)x1, this test method will run 3*invocationCount 
     times **automatically** with 1 parameter every time. */
    @Test(dataProvider="URLprovider")
    private void notePrice(String url) {
        driver.get(url);
        System.out.println(driver.getTitle());  
    }
}

现在,您可以使用此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>

答案 3 :(得分:1)

你不能从xml中做到这一点,但可以通过在TestNG中使用@DataProvider注释来实现。

以下是示例代码:

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

// It will return a 2D array of size 3x1
@DataProvider(name="URLprovider")
private Object[][] getURLs() {
  return new Object[][] {
    {"https://www.google.co.in/"},
    {"http://www.gmail.com/"},
    {"http://stackoverflow.com/"}
  };
}

答案 4 :(得分:1)

我知道派对很晚但如果你的目标是为每次运行获得报告,那么你可以试试TestNG Listener IAnnotationTransformer

代码段

public class Count implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        // TODO Auto-generated method stub
        annotation.setInvocationCount(numberOfTimesTOExecute);
    }

xml代码段

<listeners>
<listener class-name="multiple.Count"></listener>

答案 5 :(得分:1)

public class ProcessTest implements ITest {


    protected ProcessData processData;

    @Test
    public void executeServiceTest() {
        System.out.println(this.processData.toString());
    }

    @Factory(dataProvider = "processDataList")
    public RiskServiceTest(ProcessData processData) {
        this.processData = processData;
    }

    @DataProvider(name = "processDataList", parallel=true)
    public static Object[] getProcessDataList() {

        Object[] serviceProcessDataList = new Object[10];

    for(int i=0; i<=serviceProcessDataList.length; i++){
        ProcessData processData = new ProcessData();
        serviceProcessDataList[i] = processData
    }


        return serviceProcessDataList;
    }

    @Override
    public String getTestName() {

        return this.processData.getName();
    }
}

通过使用TestNG的@Factory和@DataProvider注释,您可以使用不同的数据多次执行相同的测试用例。

答案 6 :(得分:1)

您可以在testngSuite中添加多个测试并执行。在所有测试中,类名应该相同,以便多次执行相同的脚本。

答案 7 :(得分:0)

如果您不介意使用Sprint,可以创建此类:

package somePackage;

import org.junit.Ignore;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.springframework.test.annotation.Repeat;

public class ExtendedRunner extends BlockJUnit4ClassRunner {

    public ExtendedRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Description describeChild(FrameworkMethod method) {
        if (method.getAnnotation(Repeat.class) != null
                && method.getAnnotation(Ignore.class) == null) {
            return describeRepeatTest(method);
        }
        return super.describeChild(method);
    }

    private Description describeRepeatTest(FrameworkMethod method) {
        int times = method.getAnnotation(Repeat.class).value();

        Description description = Description.createSuiteDescription(
            testName(method) + " [" + times + " times]",
            method.getAnnotations());

        for (int i = 1; i <= times; i++) {
            description.addChild(Description.createTestDescription(
                getTestClass().getJavaClass(), "[" + i + "] "
                        + testName(method)));
        }
        return description;
    }

    @Override
    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
        Description description = describeChild(method);

        if (method.getAnnotation(Repeat.class) != null
                && method.getAnnotation(Ignore.class) == null) {
            runRepeatedly(methodBlock(method), description, notifier);
        }
        super.runChild(method, notifier);
    }

    private void runRepeatedly(Statement statement, Description description,
            RunNotifier notifier) {
        for (Description desc : description.getChildren()) {
            runLeaf(statement, desc, notifier);
        }
    }

}

然后在实际测试中:

package somePackage;

import *.ExtendedRunner;

import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Repeat;

@Ignore
@RunWith(ExtendedRunner.class)
public class RepeatedTest{
    @Repeat(value N)
    public void testToBeRepeated() {

    }
}

其中N是您希望测试重复的次数。