gpt4 book ai didi

java - TestNG 报告中的自定义测试方法名称

转载 作者:IT老高 更新时间:2023-10-28 20:41:16 35 4
gpt4 key购买 nike

我正在开发一个需要以编程方式调用 TestNG(使用数据提供程序)的项目。一切都很好,只是在报告中,我们得到了 @Test 方法的名称,它是处理许多情况的通用方法。我们希望在报告中获得一个有意义的名称。

我对此进行了研究,发现了 3 种方法,但不幸的是,我都失败了。

1) 实现 ITest

我发现了herehere

我在输入@Test 方法后立即设置我想要的名称(对于我尝试的所有 3 种方法,这就是我设置名称的方式)。这个名称是从 getTestName() 返回的。我观察到的是 getTestName() 在我的@Test 之前和之后被调用。最初,它返回 null(为了处理 NullPointerException,我返回 ""而不是 null),后来它返回正确的值。但我没有看到这反射(reflect)在报告中

编辑:还尝试按照 artdanil 的建议从@BeforeMethod 设置名称

2 和 3

两者都基于 second link above 中给出的解决方案

通过覆盖 XmlSuite 中的 setName,我得到了

Exception in thread "main" java.lang.AssertionError: l should not be null
at org.testng.ClassMethodMap.removeAndCheckIfLast(ClassMethodMap.java:58)
at org.testng.internal.TestMethodWorker.invokeAfterClassMethods(TestMethodWorker.java:208)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:114)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
...

通过覆盖 toString(),我在日志中看到了这些(以及我的评论),但在报告中没有更新

[2013-03-05 14:53:22,174] (Main.java:30) - calling execute 
[2013-03-05 14:53:22,346] GenericFunctionTest.<init>(GenericFunctionTest.java:52) - inside constructor
[2013-03-05 14:53:22,372] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning **//this followed by 3 invocations before arriving at @Test method**
[2013-03-05 14:53:22,410] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning
[2013-03-05 14:53:22,416] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning
[2013-03-05 14:53:22,455] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning
[2013-03-05 14:53:22,892] GenericFunctionTest.<init>(GenericFunctionTest.java:52) - inside constructor
[2013-03-05 14:53:23,178] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning **//again blank as i havent set it yet**
[2013-03-05 14:53:23,182] GenericFunctionTest.getResult(GenericFunctionTest.java:69) - inside with test case:TestCase{signature=Signature{...}}**//I am setting it immedietely after this**
[2013-03-05 14:53:23,293] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning MyMethodName **//What i want**
[2013-03-05 14:53:23,299] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning MyMethodName **// again**

编辑:通过硬编码一个值而不是在我的测试方法入口处设置它,再次尝试了所有 3 个。但结果相同

最佳答案

我也遇到了同样的问题,发现在@BeforeMethod注解的方法中设置存储测试用例名称的字段,使用native injection of TestNG会有所帮助提供方法名称和测试参数。测试名称取自 DataProvider 提供的测试参数。如果您的测试方法没有参数,只需报告方法名称即可。

//oversimplified for demontration purposes
public class TestParameters {
private String testName = null;
private String testDescription = null;

public TestParameters(String name,
String description) {
this.testName = name;
this.testDescription = description;
}

public String getTestName() {
return testName;
}
public String getTestDescription() {
return testDescription;
}
}

public class SampleTest implements ITest {
// Has to be set to prevent NullPointerException from reporters
protected String mTestCaseName = "";

@DataProvider(name="BasicDataProvider")
public Object[][] getTestData() {
Object[][] data = new Object[][] {
{ new TestParameters("TestCase1", "Sample test 1")},
{ new TestParameters("TestCase2", "Sample test 2")},
{ new TestParameters("TestCase3", "Sample test 3")},
{ new TestParameters("TestCase4", "Sample test 4")},
{ new TestParameters("TestCase5", "Sample test 5") }
};
return data;
}

@BeforeMethod(alwaysRun = true)
public void testData(Method method, Object[] testData) {
String testCase = "";
if (testData != null && testData.length > 0) {
TestParameters testParams = null;
//Check if test method has actually received required parameters
for (Object testParameter : testData) {
if (testParameter instanceof TestParameters) {
testParams = (TestParameters)testParameter;
break;
}
}
if (testParams != null) {
testCase = testParams.getTestName();
}
}
this.mTestCaseName = String.format("%s(%s)", method.getName(), testCase);
}

@Override
public String getTestName() {
return this.mTestCaseName;
}

@Test(dataProvider="BasicDataProvider")
public void testSample1(TestParameters testParams){
//test code here
}

@Test(dataProvider="BasicDataProvider")
public void testSample2(TestParameters testParams){
//test code here
}

@Test
public void testSample3(){
//test code here
}
}

编辑:根据下面的评论,我意识到报告中的示例会很有用。

从上面的运行代码中提取报告:

<testng-results skipped="0" failed="0" total="5" passed="5">
<suite name="SampleTests" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test name="Test1" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test-method
status="PASS"
signature="testSample1(org.example.test.TestParameters)[pri:0, instance:org.example.test.TimeTest@c9d92c]"
test-instance-name="testSample1(TestCase5)"
name="testSample1"
duration-ms="1014"
started-at="<some-time-before>"
data-provider="BasicDataProvider"
finished-at="<some-time-later>" >
<!-- excluded for demonstration purposes -->
</test-method>
<!-- the rest of test results excluded for brevity -->
</test>
</suite>
</testng-result>

注意,从 getTestName() 方法返回的值是在 test-instance-name 属性中,而不是在 name属性。

关于java - TestNG 报告中的自定义测试方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15220262/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com