ITest.getTestName() method from an @Factory => same name for every test

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ITest.getTestName() method from an @Factory => same name for every test

Ed Randall-5

I'm using a custom ITestListener to create our test reports, (we
needed to maintain JUnit compatibility but the existing ones don't
quite do the job).  Quite a few of our tests are migrated from a JUnit
suite() method so an @Factory was the easiest way to port them
across.  However in the report, it doesn't seem that the same String
from ITest.getTestName() method is returned for all tests.

A sample test to illustrate what I mean ...

// Want to identify each "TestNameTest" with the name given to it by
the factory
public class TestFactory  {

    @Factory
    public Object[] createTests() {
        Object[] tests = new Object[10];
        for (int i=0; i<tests.length; i++) {
            String name = "test-name-"+String.valueOf(i+1);
            tests[i] = new TestNameTest(name);
        }
        return tests;
    }

    public static class TestNameTest implements ITest {
        private String  name;

        public TestNameTest(String name) {
            this.name = name;
        }

        public String getTestName() {
            return name;
        }

        @Test(enabled=true)
        public void factoryTest() {
            Assert.assertNotNull("test name is null", name);
        }
    }
}


The reporter ...

package reports;
import org.testng.IClass ;
import org.testng.ITestContext ;
import org.testng.ITestListener ;
import org.testng.ITestNGMethod ;
import org.testng.ITestResult ;

public class TestNameReporter implements ITestListener {


    private static void ppp(String s) {
        System.out.println("TestNameReporter: "+s);
    }


    @Override
    public void onStart(ITestContext context) {
        ppp("onStart");
    }
    @Override
    public void onFinish(ITestContext context) {
        ppp("onFinish");
    }

    @Override
    public void onTestStart(ITestResult result) {
        ppp("onTestStart:"+getTestName(result));
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult
result) {
        ppp("onTestFailedButWithinSuccessPercentage:"+getTestName
(result));
    }

    @Override
    public void onTestFailure(ITestResult result) {
        ppp("onTestFailure:"+getTestName(result));
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        ppp("onTestSkipped:"+getTestName(result));
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        ppp("onTestSuccess:"+getTestName(result));
    }

    private String getTestName(ITestResult result) {
        return toString(result);
    }



    private String toString(ITestResult result) {
        IClass testClass = result.getTestClass();
        ITestNGMethod testMethod = result.getMethod();

        StringBuilder sb = new StringBuilder();
        sb.append(result.getClass().getSimpleName());
        sb.append("@");
        sb.append(Integer.toHexString(System.identityHashCode
(result)));
        sb.append("_{");
        sb.append("STATUS:");
        int stat = result.getStatus();
        switch(result.getStatus()) {
            case ITestResult.FAILURE: sb.append("FAILURE"); break;
            case ITestResult.SKIP: sb.append("SKIP"); break;
            case ITestResult.STARTED: sb.append("STARTED"); break;
            case ITestResult.SUCCESS: sb.append("SUCCESS"); break;
            case ITestResult.SUCCESS_PERCENTAGE_FAILURE: sb.append
("SUCCESS%FAIL"); break;
            default: sb.append("UNKNOWN["); sb.append(stat); sb.append
("]"); break;
        }
        sb.append(", CLASS:");
        sb.append(testClass.getName());
        sb.append(", METHOD:");
        sb.append(testMethod.getMethodName());
        sb.append(", NAME:");
        sb.append(result.getName());
        String iTestName = testClass.getTestName();
        if (iTestName != null) {
            sb.append(", ITESTNAME:");
            sb.append(iTestName);
        }
        Object[] params = result.getParameters();
        if (params != null && params.length > 0) {
            sb.append(", PARAMS(");
            for (int i=0; i<params.length; i++) {
                if (i>0) sb.append(", ");
                sb.append(String.valueOf(params[i]));
            }
            sb.append(")");
        }
        Throwable thr = result.getThrowable();
        if (thr != null) {
            sb.append(", THROWABLE:");
            sb.append(thr.toString());
        }
        if (stat == ITestResult.SKIP) {
            sb.append(", SKIP");
        }
        sb.append("}");
        return sb.toString();
    }
}


The output ...
   [testng] TestNameReporter: onTestStart:TestResult@d12eea_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@d12eea_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@15f157b_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@15f157b_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@17b40fe_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@17b40fe_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@e7f6eb_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@e7f6eb_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@103de90_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@103de90_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@108f8e0_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@108f8e0_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@b6ef8_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@b6ef8_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@a29c6e_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@a29c6e_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@1cc0a7f_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@1cc0a7f_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@c52200_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@c52200_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onFinish

I'd expect to see the ITESTNAME:test-name-1 thorough test-name-10, but
the reporter just gets given the name of the last test every time -
not what I need at all!

Thanks for any info ...

Ed

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

nr
Reply | Threaded
Open this post in threaded view
|

Re: ITest.getTestName() method from an @Factory => same name for every test

nr
Hi,
I am also looking for the same kind of report where i want my own test case name should be shown in the report but i am getting the same name every where.

I am using dataprovider to pass the data to the test case and in the report i am getting same test case name for all the datasets provided.
can you give me some pointers how i can do this?

Thanks,
Neeraja

On Tuesday, 6 October 2009 19:00:55 UTC+5:30, Ed Randall wrote:
I'm using a custom ITestListener to create our test reports, (we
needed to maintain JUnit compatibility but the existing ones don't
quite do the job).  Quite a few of our tests are migrated from a JUnit
suite() method so an @Factory was the easiest way to port them
across.  However in the report, it doesn't seem that the same String
from ITest.getTestName() method is returned for all tests.

A sample test to illustrate what I mean ...

// Want to identify each "TestNameTest" with the name given to it by
the factory
public class TestFactory  {

    @Factory
    public Object[] createTests() {
        Object[] tests = new Object[10];
        for (int i=0; i<tests.length; i++) {
            String name = "test-name-"+String.valueOf(i+1);
            tests[i] = new TestNameTest(name);
        }
        return tests;
    }

    public static class TestNameTest implements ITest {
        private String  name;

        public TestNameTest(String name) {
            this.name = name;
        }

        public String getTestName() {
            return name;
        }

        @Test(enabled=true)
        public void factoryTest() {
            Assert.assertNotNull("test name is null", name);
        }
    }
}


The reporter ...

package reports;
import org.testng.IClass ;
import org.testng.ITestContext ;
import org.testng.ITestListener ;
import org.testng.ITestNGMethod ;
import org.testng.ITestResult ;

public class TestNameReporter implements ITestListener {


    private static void ppp(String s) {
        System.out.println("TestNameReporter: "+s);
    }


    @Override
    public void onStart(ITestContext context) {
        ppp("onStart");
    }
    @Override
    public void onFinish(ITestContext context) {
        ppp("onFinish");
    }

    @Override
    public void onTestStart(ITestResult result) {
        ppp("onTestStart:"+getTestName(result));
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult
result) {
        ppp("onTestFailedButWithinSuccessPercentage:"+getTestName
(result));
    }

    @Override
    public void onTestFailure(ITestResult result) {
        ppp("onTestFailure:"+getTestName(result));
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        ppp("onTestSkipped:"+getTestName(result));
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        ppp("onTestSuccess:"+getTestName(result));
    }

    private String getTestName(ITestResult result) {
        return toString(result);
    }



    private String toString(ITestResult result) {
        IClass testClass = result.getTestClass();
        ITestNGMethod testMethod = result.getMethod();

        StringBuilder sb = new StringBuilder();
        sb.append(result.getClass().getSimpleName());
        sb.append("@");
        sb.append(Integer.toHexString(System.identityHashCode
(result)));
        sb.append("_{");
        sb.append("STATUS:");
        int stat = result.getStatus();
        switch(result.getStatus()) {
            case ITestResult.FAILURE: sb.append("FAILURE"); break;
            case ITestResult.SKIP: sb.append("SKIP"); break;
            case ITestResult.STARTED: sb.append("STARTED"); break;
            case ITestResult.SUCCESS: sb.append("SUCCESS"); break;
            case ITestResult.SUCCESS_PERCENTAGE_FAILURE: sb.append
("SUCCESS%FAIL"); break;
            default: sb.append("UNKNOWN["); sb.append(stat); sb.append
("]"); break;
        }
        sb.append(", CLASS:");
        sb.append(testClass.getName());
        sb.append(", METHOD:");
        sb.append(testMethod.getMethodName());
        sb.append(", NAME:");
        sb.append(result.getName());
        String iTestName = testClass.getTestName();
        if (iTestName != null) {
            sb.append(", ITESTNAME:");
            sb.append(iTestName);
        }
        Object[] params = result.getParameters();
        if (params != null && params.length > 0) {
            sb.append(", PARAMS(");
            for (int i=0; i<params.length; i++) {
                if (i>0) sb.append(", ");
                sb.append(String.valueOf(params[i]));
            }
            sb.append(")");
        }
        Throwable thr = result.getThrowable();
        if (thr != null) {
            sb.append(", THROWABLE:");
            sb.append(thr.toString());
        }
        if (stat == ITestResult.SKIP) {
            sb.append(", SKIP");
        }
        sb.append("}");
        return sb.toString();
    }
}


The output ...
   [testng] TestNameReporter: onTestStart:TestResult@d12eea_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@d12eea_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@15f157b_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@15f157b_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@17b40fe_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@17b40fe_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@e7f6eb_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@e7f6eb_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@103de90_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@103de90_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@108f8e0_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@108f8e0_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@b6ef8_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@b6ef8_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@a29c6e_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@a29c6e_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@1cc0a7f_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@1cc0a7f_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@c52200_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@c52200_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onFinish

I'd expect to see the ITESTNAME:test-name-1 thorough test-name-10, but
the reporter just gets given the name of the last test every time -
not what I need at all!

Thanks for any info ...

Ed

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/FatEluEtzp0J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: ITest.getTestName() method from an @Factory => same name for every test

Cédric Beust ♔-2
getTestName() is an instance name. When you use a data provider, you are still running it on one instance, so it's not surprising you only see this name once.

If you want different instances, and therefore different names, use a factory to create these instances.

-- 
Cédric




On Wed, Aug 29, 2012 at 4:41 AM, Neeraja <[hidden email]> wrote:
Hi,
I am also looking for the same kind of report where i want my own test case name should be shown in the report but i am getting the same name every where.

I am using dataprovider to pass the data to the test case and in the report i am getting same test case name for all the datasets provided.
can you give me some pointers how i can do this?

Thanks,
Neeraja

On Tuesday, 6 October 2009 19:00:55 UTC+5:30, Ed Randall wrote:
I'm using a custom ITestListener to create our test reports, (we
needed to maintain JUnit compatibility but the existing ones don't
quite do the job).  Quite a few of our tests are migrated from a JUnit
suite() method so an @Factory was the easiest way to port them
across.  However in the report, it doesn't seem that the same String
from ITest.getTestName() method is returned for all tests.

A sample test to illustrate what I mean ...

// Want to identify each "TestNameTest" with the name given to it by
the factory
public class TestFactory  {

    @Factory
    public Object[] createTests() {
        Object[] tests = new Object[10];
        for (int i=0; i<tests.length; i++) {
            String name = "test-name-"+String.valueOf(i+1);
            tests[i] = new TestNameTest(name);
        }
        return tests;
    }

    public static class TestNameTest implements ITest {
        private String  name;

        public TestNameTest(String name) {
            this.name = name;
        }

        public String getTestName() {
            return name;
        }

        @Test(enabled=true)
        public void factoryTest() {
            Assert.assertNotNull("test name is null", name);
        }
    }
}


The reporter ...

package reports;
import org.testng.IClass ;
import org.testng.ITestContext ;
import org.testng.ITestListener ;
import org.testng.ITestNGMethod ;
import org.testng.ITestResult ;

public class TestNameReporter implements ITestListener {


    private static void ppp(String s) {
        System.out.println("TestNameReporter: "+s);
    }


    @Override
    public void onStart(ITestContext context) {
        ppp("onStart");
    }
    @Override
    public void onFinish(ITestContext context) {
        ppp("onFinish");
    }

    @Override
    public void onTestStart(ITestResult result) {
        ppp("onTestStart:"+getTestName(result));
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult
result) {
        ppp("onTestFailedButWithinSuccessPercentage:"+getTestName
(result));
    }

    @Override
    public void onTestFailure(ITestResult result) {
        ppp("onTestFailure:"+getTestName(result));
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        ppp("onTestSkipped:"+getTestName(result));
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        ppp("onTestSuccess:"+getTestName(result));
    }

    private String getTestName(ITestResult result) {
        return toString(result);
    }



    private String toString(ITestResult result) {
        IClass testClass = result.getTestClass();
        ITestNGMethod testMethod = result.getMethod();

        StringBuilder sb = new StringBuilder();
        sb.append(result.getClass().getSimpleName());
        sb.append("@");
        sb.append(Integer.toHexString(System.identityHashCode
(result)));
        sb.append("_{");
        sb.append("STATUS:");
        int stat = result.getStatus();
        switch(result.getStatus()) {
            case ITestResult.FAILURE: sb.append("FAILURE"); break;
            case ITestResult.SKIP: sb.append("SKIP"); break;
            case ITestResult.STARTED: sb.append("STARTED"); break;
            case ITestResult.SUCCESS: sb.append("SUCCESS"); break;
            case ITestResult.SUCCESS_PERCENTAGE_FAILURE: sb.append
("SUCCESS%FAIL"); break;
            default: sb.append("UNKNOWN["); sb.append(stat); sb.append
("]"); break;
        }
        sb.append(", CLASS:");
        sb.append(testClass.getName());
        sb.append(", METHOD:");
        sb.append(testMethod.getMethodName());
        sb.append(", NAME:");
        sb.append(result.getName());
        String iTestName = testClass.getTestName();
        if (iTestName != null) {
            sb.append(", ITESTNAME:");
            sb.append(iTestName);
        }
        Object[] params = result.getParameters();
        if (params != null && params.length > 0) {
            sb.append(", PARAMS(");
            for (int i=0; i<params.length; i++) {
                if (i>0) sb.append(", ");
                sb.append(String.valueOf(params[i]));
            }
            sb.append(")");
        }
        Throwable thr = result.getThrowable();
        if (thr != null) {
            sb.append(", THROWABLE:");
            sb.append(thr.toString());
        }
        if (stat == ITestResult.SKIP) {
            sb.append(", SKIP");
        }
        sb.append("}");
        return sb.toString();
    }
}


The output ...
   [testng] TestNameReporter: onTestStart:TestResult@d12eea_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@d12eea_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@15f157b_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@15f157b_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@17b40fe_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@17b40fe_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@e7f6eb_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@e7f6eb_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@103de90_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@103de90_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@108f8e0_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@108f8e0_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@b6ef8_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@b6ef8_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@a29c6e_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@a29c6e_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@1cc0a7f_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@1cc0a7f_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@c52200_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@c52200_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onFinish

I'd expect to see the ITESTNAME:test-name-1 thorough test-name-10, but
the reporter just gets given the name of the last test every time -
not what I need at all!

Thanks for any info ...

Ed

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/FatEluEtzp0J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.
nr
Reply | Threaded
Open this post in threaded view
|

Re: ITest.getTestName() method from an @Factory => same name for every test

nr
Hi,
 Thanks for the quick reply.

To use Factory class we have to write one Factory for one dataprovider. I have lots of tests (thousands) already written using Dataprovider. I can not re write all of them now. But still Factory provides only the instance name of the testcase. My requirement is some thing like this.

In the data provider i will be passing the testcase name as one of the parameter which i want it to be shown in the report, so that each and every dataset will be shown as a different testcase in the report. I have tried writing a custom report as well, but i was not able to inject my testcase name to the test result report. Here is teh sample test class how my tests look like

public class AuditIntegrationTest implements ITest{
    private String name;
     
      public AuditIntegrationTest(String name) {
          this.name = name;
      }

    @Test(groups = { "Smoke","example"}, dataProvider =
"sampleProvider")

    public void testSampleTestcase(String testCase,
            String testDesc) throws Exception {
         assertNotNull(testDesc);       

    }


    @DataProvider(name = "sampleProvider")
    public static Object[][] getSampleProvider() {
        Object[][] retObjArr = new Object[][] {
                {"Testcase1", "sample1"},
                {"Testcase2", "sample2"},
                {"Testcase3", "sample3"},
                               
        };

        return (retObjArr);
    }

    @Override
    public String getTestName() {
        // TODO Auto-generated method stub
        return name;
    }

}

I want the first parameter from the dataprovider to be shown as my testcase name in the report.With the above code i am always getting "defaultTestcase" as my testcase name for all the datasets in the report.

Thanks a lot in advance.
Neeraja

On Thu, Aug 30, 2012 at 2:53 AM, Cédric Beust ♔ <[hidden email]> wrote:
getTestName() is an instance name. When you use a data provider, you are still running it on one instance, so it's not surprising you only see this name once.

If you want different instances, and therefore different names, use a factory to create these instances.

-- 
Cédric




On Wed, Aug 29, 2012 at 4:41 AM, Neeraja <[hidden email]> wrote:
Hi,
I am also looking for the same kind of report where i want my own test case name should be shown in the report but i am getting the same name every where.

I am using dataprovider to pass the data to the test case and in the report i am getting same test case name for all the datasets provided.
can you give me some pointers how i can do this?

Thanks,
Neeraja

On Tuesday, 6 October 2009 19:00:55 UTC+5:30, Ed Randall wrote:
I'm using a custom ITestListener to create our test reports, (we
needed to maintain JUnit compatibility but the existing ones don't
quite do the job).  Quite a few of our tests are migrated from a JUnit
suite() method so an @Factory was the easiest way to port them
across.  However in the report, it doesn't seem that the same String
from ITest.getTestName() method is returned for all tests.

A sample test to illustrate what I mean ...

// Want to identify each "TestNameTest" with the name given to it by
the factory
public class TestFactory  {

    @Factory
    public Object[] createTests() {
        Object[] tests = new Object[10];
        for (int i=0; i<tests.length; i++) {
            String name = "test-name-"+String.valueOf(i+1);
            tests[i] = new TestNameTest(name);
        }
        return tests;
    }

    public static class TestNameTest implements ITest {
        private String  name;

        public TestNameTest(String name) {
            this.name = name;
        }

        public String getTestName() {
            return name;
        }

        @Test(enabled=true)
        public void factoryTest() {
            Assert.assertNotNull("test name is null", name);
        }
    }
}


The reporter ...

package reports;
import org.testng.IClass ;
import org.testng.ITestContext ;
import org.testng.ITestListener ;
import org.testng.ITestNGMethod ;
import org.testng.ITestResult ;

public class TestNameReporter implements ITestListener {


    private static void ppp(String s) {
        System.out.println("TestNameReporter: "+s);
    }


    @Override
    public void onStart(ITestContext context) {
        ppp("onStart");
    }
    @Override
    public void onFinish(ITestContext context) {
        ppp("onFinish");
    }

    @Override
    public void onTestStart(ITestResult result) {
        ppp("onTestStart:"+getTestName(result));
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult
result) {
        ppp("onTestFailedButWithinSuccessPercentage:"+getTestName
(result));
    }

    @Override
    public void onTestFailure(ITestResult result) {
        ppp("onTestFailure:"+getTestName(result));
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        ppp("onTestSkipped:"+getTestName(result));
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        ppp("onTestSuccess:"+getTestName(result));
    }

    private String getTestName(ITestResult result) {
        return toString(result);
    }



    private String toString(ITestResult result) {
        IClass testClass = result.getTestClass();
        ITestNGMethod testMethod = result.getMethod();

        StringBuilder sb = new StringBuilder();
        sb.append(result.getClass().getSimpleName());
        sb.append("@");
        sb.append(Integer.toHexString(System.identityHashCode
(result)));
        sb.append("_{");
        sb.append("STATUS:");
        int stat = result.getStatus();
        switch(result.getStatus()) {
            case ITestResult.FAILURE: sb.append("FAILURE"); break;
            case ITestResult.SKIP: sb.append("SKIP"); break;
            case ITestResult.STARTED: sb.append("STARTED"); break;
            case ITestResult.SUCCESS: sb.append("SUCCESS"); break;
            case ITestResult.SUCCESS_PERCENTAGE_FAILURE: sb.append
("SUCCESS%FAIL"); break;
            default: sb.append("UNKNOWN["); sb.append(stat); sb.append
("]"); break;
        }
        sb.append(", CLASS:");
        sb.append(testClass.getName());
        sb.append(", METHOD:");
        sb.append(testMethod.getMethodName());
        sb.append(", NAME:");
        sb.append(result.getName());
        String iTestName = testClass.getTestName();
        if (iTestName != null) {
            sb.append(", ITESTNAME:");
            sb.append(iTestName);
        }
        Object[] params = result.getParameters();
        if (params != null && params.length > 0) {
            sb.append(", PARAMS(");
            for (int i=0; i<params.length; i++) {
                if (i>0) sb.append(", ");
                sb.append(String.valueOf(params[i]));
            }
            sb.append(")");
        }
        Throwable thr = result.getThrowable();
        if (thr != null) {
            sb.append(", THROWABLE:");
            sb.append(thr.toString());
        }
        if (stat == ITestResult.SKIP) {
            sb.append(", SKIP");
        }
        sb.append("}");
        return sb.toString();
    }
}


The output ...
   [testng] TestNameReporter: onTestStart:TestResult@d12eea_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@d12eea_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@15f157b_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@15f157b_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@17b40fe_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@17b40fe_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@e7f6eb_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@e7f6eb_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@103de90_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@103de90_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@108f8e0_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@108f8e0_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@b6ef8_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@b6ef8_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@a29c6e_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@a29c6e_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@1cc0a7f_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@1cc0a7f_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestStart:TestResult@c52200_
{STATUS:STARTED, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onTestSuccess:TestResult@c52200_
{STATUS:SUCCESS, CLASS:tests.TestFactory$TestNameTest,
METHOD:factoryTest, NAME:factoryTest, ITESTNAME:test-name-10}
   [testng] TestNameReporter: onFinish

I'd expect to see the ITESTNAME:test-name-1 thorough test-name-10, but
the reporter just gets given the name of the last test every time -
not what I need at all!

Thanks for any info ...

Ed

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/FatEluEtzp0J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

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

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