Ula Karzelek Ula Karzelek - 8 months ago 53
Java Question

How to generate tests with different names in TestNG?

I'm using TestNG to run Selenium based tests in Java. I have a bunch of repeated tests. Generally, they do all the same except of test name and one parameter.

I want to automate generation of it. I was thinking about using factory. Is there a way to generate tests with different name? What would be the best approach to this?

As for now I have something like below and I want to create 10 tests like LinkOfInterestIsActiveAfterClick

public class SmokeTest extends BrowserStartingStoping{

public void LinkOfInterestIsActiveAfterClick(){
String link = "link_of_interest";


  • My XML suite is auto-generated from Java code.

  • Test names are crucial for logging which link is active, and which one is not.


So I connected Factory with DataProvider and used attributes of contexts.

@DataProvider(name = "DP1")
public Object[][] createData() {
  Object[][] retObjArr={
  return retObjArr;

@Test (dataProvider = "DP1")
public void isActive(String name, String link){
  this.context.setAttribute("name", name);

And in the Listener

public class MyListener extends TestListenerAdapter{
  public void onTestSuccess(ITestResult tr){
  //and similar

  private void log(String string, ITestResult tr){
    List<ITestContext> k = this.getTestContexts();
    String testName = tr.getTestClass().getName();      
    for (ITestContext i: k)
      if (i.getAttribute("name") != null)