如何忽略测试结果中的@BeforeMethod和@AfterMethod?



下面是使用testng框架创建的示例测试用例,

public class MyTest {
@BeforeMethod
public void beforeMethod() {
}
@Test
public void test1() {
}
@Test
public void test2() {
}
@AfterMethod
public void afterMethod() {
}
}

我正在收到以下testng-results.xml文件,

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" total="2" passed="2">
<reporter-output>
</reporter-output>
<suite name="Surefire suite" duration-ms="15" started-at="2018-01-22T11:17:46Z" finished-at="2018-01-22T11:17:46Z">
<groups>
</groups>
<test name="Surefire test" duration-ms="15" started-at="2018-01-22T11:17:46Z" finished-at="2018-01-22T11:17:46Z">
<class name="com.my.test.MyTest">
<test-method status="PASS" signature="beforeMethod()[pri:0, instance:com.my.test.MyTest@3567135c]" name="beforeMethod" is-config="true" duration-ms="4" started-at="2018-01-22T16:47:46Z" finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- beforeMethod -->
<test-method status="PASS" signature="test1()[pri:0, instance:com.my.test.MyTest@3567135c]" name="test1" duration-ms="0" started-at="2018-01-22T16:47:46Z" finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test1 -->
<test-method status="PASS" signature="afterMethod()[pri:0, instance:com.my.test.MyTest@3567135c]" name="afterMethod" is-config="true" duration-ms="1" started-at="2018-01-22T16:47:46Z" finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- afterMethod -->
<test-method status="PASS" signature="test2()[pri:0, instance:com.my.test.MyTest@3567135c]" name="test2" duration-ms="0" started-at="2018-01-22T16:47:46Z" finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test2 -->
<test-method status="PASS" signature="beforeMethod()[pri:0, instance:com.my.test.MyTest@3567135c]" name="beforeMethod" is-config="true" duration-ms="0" started-at="2018-01-22T16:47:46Z" finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- beforeMethod -->
<test-method status="PASS" signature="afterMethod()[pri:0, instance:com.my.test.MyTest@3567135c]" name="afterMethod" is-config="true" duration-ms="0" started-at="2018-01-22T16:47:46Z" finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- afterMethod -->
</class> <!--com.my.test.MyTest -->
</test> <!-- Surefire test -->
</suite> <!-- Surefire suite -->
</testng-results>

请注意,afterMethodbeforeMethod也被视为测试方法。我想从testng-result中忽略这些方法.xml因为我正在使用这个XML进行进一步处理。

我有以下问题,

  • 有没有办法避免测试结果xml文件中@BeforeMethod@AfterMethod方法?
  • 是否有任何方法可以从测试方法 XML 标记中区分前后方法? (我可以严格命名我的方法beforeMethod_Ignore()afterMethod_Ignore()但这将是黑客而不是解决方案)

我已经浏览了以下链接,但尚未找到任何内容,

  • http://testng.org/doc/documentation-main.html#parameters-testng-xml
  • http://www.seleniumeasy.com/testng-tutorials/include-exclude-test-methods-in-testng
  • 在测试中排除测试等。

AFAIK,没有直接的方法可以从testng-results.xml文件中排除配置方法。

但综上所述,testng-results.xml中的当前条目仍然允许您区分配置方法和测试方法。

对于配置方法,有一个名为is-config="true"的额外属性。

请参阅下面的摘录,了解您自己的 xml 文件的摘录

<test-method 
status="PASS" 
signature="beforeMethod()[pri:0, instance:com.my.test.MyTest@3567135c]" 
name="beforeMethod" 
is-config="true" 
duration-ms="4" 
started-at="2018-01-22T16:47:46Z" 
finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- beforeMethod -->

但对于常规@Test方法,属性is-config=true将不可用。

见下文 :

<test-method 
status="PASS" 
signature="test1()[pri:0, instance:com.my.test.MyTest@3567135c]" 
name="test1" 
duration-ms="0" 
started-at="2018-01-22T16:47:46Z" 
finished-at="2018-01-22T16:47:46Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test1 -->

这对您的下游 xml 处理没有帮助吗?

如果这仍然不适合您,那么其他替代方案将包括:

  • 您将构建自己的org.testng.IReporter实现版本,其中构造此 XML,然后将其作为侦听器连接。
  • 创建一个名为org.testng.reporters的包,其中将类的内容复制到org.testng.reporters.XMLReporter到此包中,更改方法org.testng.reporters.XMLSuiteResultWriter#getTestResultAttributes以向该方法添加额外的属性(如果它不是@Test方法)。

最新更新