IInvokedMethodListener is not working as expected.

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

IInvokedMethodListener is not working as expected.

MadanReddy78
My sample Code:  When running in sequential , it is running and getting the output as expected.

@DataProvider(name = "DataEntrySet", parallel = true)
        public static Object[][] getDataEntryRecord() {

                Object[][] obData = null;
                List<TestDataHolder> ltdh = new ArrayList<TestDataHolder>();
                TDHHelper tdhHelper = new TDHHelper();

                String[][] sheet = tdhHelper.getDataFromSheet(
                                ".\\src\\test\\resources\\TestData\\data.xlsx",
                                "DataEntry");

                tdhHelper.formatArrayToTestDataHolder(ltdh, sheet);
                obData = new Object[ltdh.size()][1];
                for (int i = 0; i < ltdh.size(); i++) {
                        obData[i][0] = new TestDataHolder(ltdh.get(i));
                }

                return obData;
        }

        @Test(groups = "general", dataProvider = "DataEntrySet", threadPoolSize = 5)
        public void testCreateApplication(TestDataHolder tdh, ITestContext ctx)
                        throws IOException, TwfException, InterruptedException {
                ctx.setAttribute("testName",
                                " -" + tdh.getTestcaseId());
                System.out.println(ctx.getAttribute(arg0));
                Thread.sleep(15000);
        }

My listerner code looks like

 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
   
                       
               
    if (method.isTestMethod()) {
    ITestContext cx = testResult.getTestContext();
    System.out.println("End Test"+cx.getAttribute("testName"));
    ReportListner rl = new ReportListner();  //Tis is not a listener , just named like that
       
   
        switch(testResult.getStatus()){
        case ITestResult.SUCCESS:
        rl.onTestSuccess(testResult);
        break;
        case ITestResult.FAILURE:
        rl.onTestFailure(testResult);
        break;
        case ITestResult.SKIP:
        rl.onTestSkipped(testResult);
        break;
        }
       
       
    }
    }

When I run in parallel , the testmethod name repeating.
some thing like below:

Running TestSuite
Started Test..
Started Test..
Started Test..
Started Test..
Started Test..
End Test-CA001.0004
PASSED -CA001.0004
End Test-CA001.0001
PASSED -CA001.0001
End Test-CA001.0005
PASSED -CA001.0005
End Test-CA001.0003
PASSED -CA001.0003
End Test-CA001.0003
PASSED -CA001.0003
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 62.339 sec

**If you check the above output , CA001.0003 displayed twice.

Reply | Threaded
Open this post in threaded view
|

Re: IInvokedMethodListener is not working as expected.

⇜Krishnan Mahadevan⇝
There is nothing wrong with TestNG. Its your code at fault.

Basically when you run things in parallel, there would be a lot of concurrent invocations to afterInvocation() which are going to be accessing the same attribute from ITestContext. Since your test methods are running in parallel, they are perhaps causing the same attribute "testName" to be overwritten with different values. That is why you are seeing this problem only in parallel executions, but things work fine when you run in sequence.

You would need to figure out another way in which you can basically get hold of the testName. One of the ways that I can think of is, by accessing the parameters via ITestResult.getParameters() and then iterating through this array to find out which of them are of type TestDataHolder (you can use the instanceof check for finding this out) and if you find it, cast the Object into TestDataHolder and then access the test name.




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Thu, Oct 29, 2015 at 2:52 AM, MadanReddy78 <[hidden email]> wrote:
My sample Code:  When running in sequential , it is running and getting the
output as expected.

@DataProvider(name = "DataEntrySet", parallel = true)
        public static Object[][] getDataEntryRecord() {

                Object[][] obData = null;
                List<TestDataHolder> ltdh = new ArrayList<TestDataHolder>();
                TDHHelper tdhHelper = new TDHHelper();

                String[][] sheet = tdhHelper.getDataFromSheet(
                                ".\\src\\test\\resources\\TestData\\data.xlsx",
                                "DataEntry");

                tdhHelper.formatArrayToTestDataHolder(ltdh, sheet);
                obData = new Object[ltdh.size()][1];
                for (int i = 0; i < ltdh.size(); i++) {
                        obData[i][0] = new TestDataHolder(ltdh.get(i));
                }

                return obData;
        }

        @Test(groups = "general", dataProvider = "DataEntrySet", threadPoolSize =
5)
        public void testCreateApplication(TestDataHolder tdh, ITestContext ctx)
                        throws IOException, TwfException, InterruptedException {
                ctx.setAttribute("testName",
                                " -" + tdh.getTestcaseId());
                System.out.println(ctx.getAttribute(arg0));
                Thread.sleep(15000);
        }

My listerner code looks like

 public void afterInvocation(IInvokedMethod method, ITestResult testResult)
{



        if (method.isTestMethod()) {
                ITestContext cx = testResult.getTestContext();
                System.out.println("End Test"+cx.getAttribute("testName"));
                ReportListner rl = new ReportListner();  //Tis is not a listener ,
just named like that


                switch(testResult.getStatus()){
                        case ITestResult.SUCCESS:
                                rl.onTestSuccess(testResult);
                                break;
                        case ITestResult.FAILURE:
                                rl.onTestFailure(testResult);
                                break;
                        case ITestResult.SKIP:
                                rl.onTestSkipped(testResult);
                                break;
                }


        }
    }

When I run in parallel , the testmethod name repeating.
some thing like below:

Running TestSuite
Started Test..
Started Test..
Started Test..
Started Test..
Started Test..
End Test-CA001.0004
PASSED -CA001.0004
End Test-CA001.0001
PASSED -CA001.0001
End Test-CA001.0005
PASSED -CA001.0005
End Test-CA001.0003
PASSED -CA001.0003
End Test-CA001.0003
PASSED -CA001.0003
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 62.339 sec

**If you check the above output , CA001.0003 displayed twice.





--
View this message in context: http://testng.1065351.n5.nabble.com/IInvokedMethodListener-is-not-working-as-expected-tp22067.html
Sent from the testng-users mailing list archive at Nabble.com.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.