如何在TestNG中获取有关方法调用的测试用例数据?

时间:2018-02-09 02:35:41

标签: java testng testng-dataprovider

我在TestNG包中使用IInvokedMethodListener来监听我的测试用例的每个方法执行。在每种方法中,我都试图检索每种情况的数据(测试用例数据)。

我一直在搜索API,但无法找到任何有用的方法。有没有人尝试类似的东西并且成功了?

1 个答案:

答案 0 :(得分:0)

这是你怎么做的。

从xml套件中提取参数的测试类示例。

import org.assertj.core.api.Assertions;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestClassUsingParameters {

    @Parameters({"studentName", "studentAge"})
    @Test
    public void testMethod(String name, int age) {
        Assertions.assertThat(name).isNotEmpty();
        Assertions.assertThat(age).isGreaterThan(0);
    }
}

从数据提供程序中提取参数的测试类示例。

import org.assertj.core.api.Assertions;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClassUsingDataProvider {
    @Test(dataProvider = "dp")
    public void testMethod(String name, int age) {
        Assertions.assertThat(name).isNotEmpty();
        Assertions.assertThat(age).isGreaterThan(0);
    }

    @DataProvider(name = "dp")
    public Object[][] getData() {
        return new Object[][]{
                {"Jack Daniels", 10},
                {"Napolean", 15}
        };
    }
}

这里是监听器实现的样子,它打印出在任何一种情况下都被送入测试方法的参数。

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

import java.util.Arrays;

public class SampleListener implements IInvokedMethodListener {

    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        Object[] parameters = testResult.getParameters();
        if (parameters != null) {
            printer("beforeInvocation", method, parameters);
        }
    }

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        Object[] parameters = testResult.getParameters();
        if (parameters != null) {
            printer("afterInvocation", method, parameters);
        }
    }

    private void printer(String prefix, IInvokedMethod method, Object[] parameters) {
        String msg = String.format("Running %s() for method %s() with parameters: %s", prefix,
                method.getTestMethod().getMethodName(),
                Arrays.toString(parameters));
        System.err.println(msg);
    }
}

最后,这里是xml套件的样子

<?xml version="1.0"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="48697918_suite" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.stackoverflow.qn48697918.SampleListener"/>
    </listeners>
    <test name="48697918_test1">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn48697918.TestClassUsingParameters">
                <parameter name="studentName" value="Jack Daniels"/>
                <parameter name="studentAge" value="15"/>
            </class>
            <class name="com.rationaleemotions.stackoverflow.qn48697918.TestClassUsingDataProvider"/>
        </classes>
    </test>
</suite>

这是输出:

...
... TestNG 6.14.2 by Cédric Beust (cedric@beust.com)
...

Running beforeInvocation() for method testMethod() with parameters: [Jack Daniels, 15]

Running afterInvocation() for method testMethod() with parameters: [Jack Daniels, 15]
Running beforeInvocation() for method testMethod() with parameters: [Jack Daniels, 10]
Running afterInvocation() for method testMethod() with parameters: [Jack Daniels, 10]
Running beforeInvocation() for method testMethod() with parameters: [Napolean, 15]
Running afterInvocation() for method testMethod() with parameters: [Napolean, 15]
PASSED: testMethod("Jack Daniels", 15)
PASSED: testMethod("Jack Daniels", 10)
PASSED: testMethod("Napolean", 15)

===============================================
    48697918_test1
    Tests run: 3, Failures: 0, Skips: 0
===============================================

===============================================
48697918_suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================