Java 如何从testng结果中忽略@BeforeMethod和@AfterMethod?

Java 如何从testng结果中忽略@BeforeMethod和@AfterMethod?,java,testng,maven-surefire-plugin,Java,Testng,Maven Surefire Plugin,下面是使用testng框架创建的示例测试用例 public class MyTest { @BeforeMethod public void beforeMethod() { } @Test public void test1() { } @Test public void test2() { } @AfterMethod public void 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>

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

我有以下问题,

  • 在测试结果xml文件中是否有任何方法可以避免使用
    @beforethod
    @AfterMethod
    方法
  • 从测试方法XML标记中是否可以区分前后方法? (也就是说,我可以严格地将我的方法命名为
    before方法\u Ignore()
    after方法\u Ignore()
    ,但这将是一种黑客行为,而不是解决方案)
我浏览了以下链接,但没有找到任何内容

  • 等等

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 -->
这对下游xml处理没有帮助吗

如果这仍然不适用于您,那么其他备选方案包括:

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

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 -->
这对下游xml处理没有帮助吗

如果这仍然不适用于您,那么其他备选方案包括:

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