简体   繁体   中英

How can I print in log the number of tests running as part of one group in TestNG?

I need to print this before actual execution of tests start. Anyways, we get the count at last of execution.

Say for example if my testng.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1">
   <test name="test1">

   <groups>
      <run>
      <include name="functest" />
      </run>
   </groups>

   <classes>
      <class name="GroupTestExample" />
   </classes>

   </test>
</suite>

And there are 10 methods in class GroupTestExample which comes under group functest, it should print 10 in log as soon as it starts running.

Create a custom Listener which extends the org.testng.TestListenerAdapter . After that override the following method:

public class MyCustomListener extends TestListenerAdapter {

  @Override
  public void onStart(ITestContext testContext) {
    super.onStart(testContext);
    System.out.println("Number of Test Methods: " + testContext.getAllTestMethods().length);
  }

}

You can add your custom listener by using @Listeners annotation on your main class of your test, like

@Listeners({MyCustomListener.class})
public class MyMainTestClass { ... }

You can find more information in TestNG doc:

TestNG Listeners - http://testng.org/doc/documentation-main.html#testng-listeners

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM