Search code examples
javatestngmaven-surefire-plugin

How to ignore @BeforeMethod and @AfterMethod from testng results?


Following is a sample test case created using testng framework,

public class MyTest {

    @BeforeMethod
    public void beforeMethod() {
    }

    @Test
    public void test1() {
    }

    @Test
    public void test2() {
    }

    @AfterMethod
    public void afterMethod() {
    }

}

I am getting following testng-results.xml file,

<?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>

Notice that afterMethod and beforeMethod are also considered as test-methods. I want o ignore these methods from testng-result.xml as I am using this XML for further processing.

I have following questions,

  • Is there anyway to avoid @BeforeMethod and @AfterMethod methods in test result xml file?
  • Is there anyway to differentiate between before and after methods from test-method XML tag? (i.e. I can strictly name my methods to beforeMethod_Ignore() and afterMethod_Ignore() but it will be kind of hack instead of solution)

I've gone through following links but couldn't find anything yet,


Solution

  • AFAIK, there is no direct way of excluding configuration methods from the testng-results.xml file.

    But all said and done, the current entries in the testng-results.xml still lets you distinguish a configuration method from a test method.

    For a configuration method there's an extra attribute named is-config="true".

    See below for excerpts from your own xml file

    <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 -->
    

    But for a regular @Test method, the attribute is-config=true would not be available.

    See below :

    <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 -->
    

    Would that not help you with your downstream xml processing ?

    If that still doesn't work for you, then the other alternatives would include:

    • you would build your own version of org.testng.IReporter implementation wherein you construct this xml and then wire it in as a listener.
    • You create a package called org.testng.reporters wherein you duplicate the contents of the class org.testng.reporters.XMLReporter into this package, alter the method org.testng.reporters.XMLSuiteResultWriter#getTestResultAttributes to add extra attributes to the method if its not a @Test method.