implementing ITest

481 views
Skip to first unread message

null.in

unread,
Jul 19, 2010, 6:52:14 PM7/19/10
to testng-users
Hi,

If I have a test class with a single @BeforeMethod, @AfterMethod and
@Test implement ITest and have getTestName return "FooBar", I see the
following in testng-results.xml:

<testng-results>
<reporter-output>
</reporter-output>
<suite name="Command line suite" duration-ms="26" started-
at="2010-07-19T15:42:01Z" finished-at="2010-07-19T15:42:01Z">
<groups>
</groups>
<test name="Command line test" duration-ms="7" started-
at="2010-07-19T15:42:01Z" finished-at="2010-07-19T15:42:01Z">
<class name="test.itest.TestSample">
<test-method status="PASS" signature="beforeMethod()"
name="FooBar" is-config="true" duration-ms="2" started-
at="2010-07-19T15:42:01Z" finished-at="2010-07-19T15:42:01Z">
</test-method>
<test-method status="PASS" signature="test1()" name="FooBar"
duration-ms="0" started-at="2010-07-19T15:42:01Z" finished-
at="2010-07-19T15:42:01Z">
</test-method>
<test-method status="PASS" signature="afterMethod()"
name="FooBar" is-config="true" duration-ms="0" started-
at="2010-07-19T15:42:01Z" finished-at="2010-07-19T15:42:01Z">
</test-method>
</class>
</test>
</suite>
</testng-results>

All three methods are named FooBar. IMO, only the test method should
be returned with name "FooBar"

Also, in the HTML report, all the methods and TestClass names are
shown as FooBar.


What is that test name from ITest.getTestName() supposed to represent?
For starters, I was thinking of changing two things:

1. TestClass name still remains the same.
2. Only the test method is renamed to the value of getTestName() and
not the related configuration methods.

Cédric Beust ♔

unread,
Jul 19, 2010, 7:01:06 PM7/19/10
to testng...@googlegroups.com
Definitely a bug in the XML and HTML generators, which do extract the information of ITest#getTestName if it's there, but don't seem to put it at the right place.

getTestName is an instance name, so it might make sense to still surface it on individual methods (both test and configuration) but maybe with an attribute of its own, i.e. test-name.

--
Cédric



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


Nalin Makar

unread,
Jul 19, 2010, 7:40:38 PM7/19/10
to testng...@googlegroups.com
I can look into a fix for this.

You said getTestName is an instance name. So, it's a name for the test class instance or the test method instance? Can you clarify again.


-nalin

2010/7/19 Cédric Beust ♔ <ced...@beust.com>

Cédric Beust ♔

unread,
Jul 19, 2010, 8:04:27 PM7/19/10
to testng...@googlegroups.com
On Mon, Jul 19, 2010 at 4:40 PM, Nalin Makar <nalin...@gmail.com> wrote:
I can look into a fix for this.

You said getTestName is an instance name. So, it's a name for the test class instance or the test method instance? Can you clarify again.

Well, it depends how the user implement getTestName(). If it returns the same string regardless of the instance, it's a class-wide value. People mostly use this to give different names to different instances (so they will include the hashcode() in the string they return, for example) or sometimes for different threads (they will include Thread.currentThread().getId() in the test name).

Does this answer your question?

--
Cédric


Nalin Makar

unread,
Jul 19, 2010, 8:45:06 PM7/19/10
to testng...@googlegroups.com
ok. I'll change testng-result.xml to change the values of name attribute to be the name of the method (as is the case with any test that doesn't implement ITest) and add test-instance-name attribute:

<class name="test.itest.TestSample">
   <test-method status="PASS" signature="testSetup()" name="testSetup" test-instance-name="FooBar" is-config="true" />
   <test-method status="PASS" signature="test1()" name="test1" test-instance-name="FooBar" />
   <test-method status="PASS" signature="testCleanup()" name="testCleanup" test-instance-name="FooBar" is-config="true" />
</class>

Thanks,
Nalin

2010/7/19 Cédric Beust ♔ <ced...@beust.com>
--
Cédric


KV

unread,
Jul 29, 2013, 12:56:36 PM7/29/13
to testng...@googlegroups.com
Hi,

I have tried this but the test-instance name always is being set to the last executed method. But when I print out the values on command line it gets generated as expected. Could you please tell me where I am going wrong.
I am trying to get unique test names using testNG.

public class SampleTest implements ITest {

    // Has to be set to prevent NullPointerException from reporters

   String mTestCaseName = "hello";

   


    @DataProvider(name="BasicDataProvider")

    public Object[][] getTestData() {

        Object[][] data = new Object[][] {

                { new TestParameters("TestCase1", "Sample test 1")},

                { new TestParameters("TestCase2", "Sample test 2")},

                { new TestParameters("TestCase3", "Sample test 3")},

                { new TestParameters("TestCase4", "Sample test 4")},

                { new TestParameters("TestCase5", "Sample test 5") }

        };

        return data;

    }


    @BeforeMethod(alwaysRun = true)

    public void testData(Method method, Object[] testData) {

        String testCase = "";

        if (testData != null && testData.length > 0) {

            TestParameters testParams = null;

            //Check if test method has actually received required parameters

            for (Object testParameter : testData) {

                if (testParameter instanceof TestParameters) {

                    testParams = (TestParameters)testParameter;

                    break;

                }

            }

            if (testParams != null) {

                testCase = testParams.getTestName();

            }

        }

        this.mTestCaseName = String.format("%s(%s)", method.getName(), testCase);

        System.out.println(">>>>>>><<<<<<<<<<<<<<"+this.mTestCaseName);

    }


    @Override

    public String getTestName() {

        return this.mTestCaseName;

    }


    @Test(dataProvider="BasicDataProvider")

    public void testSample1(TestParameters testParams){

        //test code here

    }


    @Test(dataProvider="BasicDataProvider")

    public void testSample2(TestParameters testParams){

        //test code here

    }


    @Test

    public void testSample3(){

        //test code here

Reply all
Reply to author
Forward
0 new messages