Programatiically setting a name of a test

9 views
Skip to first unread message

Kohsuke Kawaguchi

unread,
Jun 5, 2006, 7:59:18 PM6/5/06
to testng-users
I'd like to be able to set test names from within the test code.

To explain my use case, let's take TestNG tutorial 5.7 as an example
(http://testng.org/doc/documentation-main.html#factories)

With @Factory, you can create multiple instances of test classes that
are configured differently. This is very convenient, but when you see
the report, all those instances will look the same, as they are all
named after the test method. So I can't easily distinguish which
WebTest instance failed.

What I'd like to do is to give each WebTest class a different name. I
don't really care how I tell the name to TestNG, but it could be
something like this:

public class WebTest {

private int m_numberOfTimes;

public WebTest(int numberOfTimes) { ... }

@Name
public String getTestName() {
return "WebTest "+m_numberOfTimes;
}

@Test
public void testServer() { ... }
}

The expected semantics is that TestNG finds a method via annotation.
Another possibility might be to do it like IHookable --- define an
interface and a method that returns the name (I like this one better
actually.)

Internally, there's already ITestResult.getName() --- it's just a
matter of having Invoker update the name accordingly.

I hope the development team considers this.

Cédric Beust ♔

unread,
Jun 6, 2006, 6:44:47 PM6/6/06
to testng...@googlegroups.com
Kohsuke,

I implemented this (in CVS).  I didn't use an annotation but an interface, though, because it's safer when you need to enforce a contract on a method.

The interface is org.testng.ITest and it only contains one method right now:  getTestName().  Here is an example:

@Test
public class FactoryTest {
 
  @Factory
  public Object[] init() {
    return new Object[] {
        new B("Test1"),
        new B("Test2")
    };
  }
}

public class B implements ITest {
  private String m_name;
 
  public B(String s) {
    m_name = s;
  }
 
  @Test
  public void t() {
   
  }

  public String getTestName() {
    return m_name;
  }
}

Then in the reports and the output, you will see:

Creating c:\t\test-outputs\Single.html
PASSED: test.tmp.B.t (Test1)
PASSED: test.tmp.B.t (Test2)
PASSED: test.tmp.FactoryTest.f2

PASSED TESTS
Test method Time (seconds) Exception
test.tmp.B.t (Test1) 0 none
test.tmp.B.t (Test2) 0 none
test.tmp.FactoryTest.f2 0 none

Is this what you had in mind?

You can sync to the CVS head or let me know if you need a jar file.

--
Cedric

Kohsuke Kawaguchi

unread,
Jun 6, 2006, 7:06:17 PM6/6/06
to testng-users
Thanks! I'll build it by myself

Kohsuke Kawaguchi

unread,
Jun 7, 2006, 2:23:09 AM6/7/06
to testng-users
I think you forgot to commit the changes to the Invoker class. The code
doesn't even compile right now.

igor...@gs.com

unread,
Jun 20, 2006, 10:14:32 AM6/20/06
to testng-users
Hi Cedric,

I'm interested in the same functionality as Kohsuke. Would it be
possible to get the jar file you mentioned?

Regards,
Igor

Reply all
Reply to author
Forward
0 new messages