TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

bipo1980

Hello: is there another way to get the ITestContext in a test method? Or what am i doing wrong?


I included ITestContext as a parameter in the TestMethod but Retry fails.

In the example below if i include ITestContext in the parameter then during the retry method Test1() is not called the second time.


BUT if i remove ITestContext from the method parameter then method Test1() is called the second time and result is as expected.


public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }

    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }       
}

Result: if i exclude parameter "ITestContext context" in the test method  
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). Test1(): 1 : apple [this is what is desired after retry]

Result: if i include parameter "ITestContext context" in the test method
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). ::: note here that test method was not called the second time [WHY????]

--
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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

Krishnan Mahadevan

You can get hold of the current ITestResult by invoking: Reporter.getCurrentTestResult() from within a @Test annotated method.

 

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/

 

From: <[hidden email]> on behalf of <[hidden email]>
Reply-To: <[hidden email]>
Date: Sunday, March 26, 2017 at 1:11 PM
To: testng-users <[hidden email]>
Subject: [testng-users] TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

 

Hello: is there another way to get the ITestContext in a test method? Or what am i doing wrong?

 

I included ITestContext as a parameter in the TestMethod but Retry fails.

In the example below if i include ITestContext in the parameter then during the retry method Test1() is not called the second time.

 

BUT if i remove ITestContext from the method parameter then method Test1() is called the second time and result is as expected.

 

public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }
 
    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }       
}

 

Result: if i exclude parameter "ITestContext context" in the test method  
Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
Test1(): 1 : apple  [this is what is desired after retry]

Result: if i include parameter "ITestContext context" in the test method
Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
::: note here that test method was not called the second time [WHY????]

--
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 https://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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

bipo1980
In reply to this post by bipo1980
Hello Krishnan:

Thank you. Worked perfectly. Reporter.getCurrentTestResult().getTestContext().setAttribute("type", type);

Yours is better. I was working on getting the context from the @DataProvider through a class variable.

Question still that passing the ITestContext as a @Test parameter caused the problem with TestNG's Retry feature Only.
Any idea if this was intentional behaviour?

Thanks,
B
 

On Sunday, March 26, 2017 at 1:11:09 PM UTC+5:30, [hidden email] wrote:

Hello: is there another way to get the ITestContext in a test method? Or what am i doing wrong?


I included ITestContext as a parameter in the TestMethod but Retry fails.

In the example below if i include ITestContext in the parameter then during the retry method Test1() is not called the second time.


BUT if i remove ITestContext from the method parameter then method Test1() is called the second time and result is as expected.


public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }

    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }       
}

Result: if i exclude parameter "ITestContext context" in the test method  
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). Test1(): 1 : apple [this is what is desired after retry]

Result: if i include parameter "ITestContext context" in the test method
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). ::: note here that test method was not called the second time [WHY????]

--
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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

Krishnan Mahadevan
Can you please help share a quick sample which I can execute to recreate the problem ?


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 Mon, Mar 27, 2017 at 9:50 AM, <[hidden email]> wrote:
Hello Krishnan:

Thank you. Worked perfectly. Reporter.getCurrentTestResult().getTestContext().setAttribute("type", type);

Yours is better. I was working on getting the context from the @DataProvider through a class variable.

Question still that passing the ITestContext as a @Test parameter caused the problem with TestNG's Retry feature Only.
Any idea if this was intentional behaviour?

Thanks,
B

 

On Sunday, March 26, 2017 at 1:11:09 PM UTC+5:30, [hidden email] wrote:

Hello: is there another way to get the ITestContext in a test method? Or what am i doing wrong?


I included ITestContext as a parameter in the TestMethod but Retry fails.

In the example below if i include ITestContext in the parameter then during the retry method Test1() is not called the second time.


BUT if i remove ITestContext from the method parameter then method Test1() is called the second time and result is as expected.


public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }

    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }       
}

Result: if i exclude parameter "ITestContext context" in the test method  
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). Test1(): 1 : apple [this is what is desired after retry]

Result: if i include parameter "ITestContext context" in the test method
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). ::: note here that test method was not called the second time [WHY????]

--
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 https://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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

bipo1980
Thanks Krishnan. Here is the sample code:

public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }

    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }      
}


Result: if i exclude parameter "ITestContext context" in the test method

Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
Test1(): 1 : apple  [this is what is desired after retry]

Result: if i include parameter "ITestContext context" in the test method

Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
::: note here that test method was not called the second time [WHY????]

The Retry Code:

public class RetryListener implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if (retry == null) {
            annotation.setRetryAnalyzer(RetryAnalyzer.class);
        }
    }
}

public class RetryAnalyzer implements IRetryAnalyzer{
    private int retryCount = 0;
    private int maxRetryCount = 1;

    @Override
    public boolean retry(ITestResult result) {
        if (retryCount < maxRetryCount) {
            System.out.println("Retrying test " + result.getName() + " with status "
                    + getResultStatusName(result.getStatus()) + " for the " + (retryCount + 1) + " time(s).");
            retryCount++;
            return true;
        }
        return false;
    }

    public String getResultStatusName(int status) {
        String resultName = null;
        if (status == 1) resultName = "SUCCESS";
        if (status == 2) resultName = "FAILURE";
        if (status == 3) resultName = "SKIP";
        return resultName;
    }
}

The xml:

...<suite name="NGBug Suite">
    <listeners><listener class-name="com.NGtest.RetryListener" /></listeners>
    <test name="NGBug Test"><classes><class name="com.NGtest.RetryProvider" /></classes>
    </test>
</suite>





On Monday, March 27, 2017 at 10:15:46 AM UTC+5:30, Krishnan wrote:
Can you please help share a quick sample which I can execute to recreate the problem ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/
My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

On Mon, Mar 27, 2017 at 9:50 AM, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Zhhv6BthAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">bipo...@...> wrote:
Hello Krishnan:

Thank you. Worked perfectly. Reporter.getCurrentTestResult().getTestContext().setAttribute("type", type);

Yours is better. I was working on getting the context from the @DataProvider through a class variable.

Question still that passing the ITestContext as a @Test parameter caused the problem with TestNG's Retry feature Only.
Any idea if this was intentional behaviour?

Thanks,
B

 

On Sunday, March 26, 2017 at 1:11:09 PM UTC+5:30, [hidden email] wrote:

Hello: is there another way to get the ITestContext in a test method? Or what am i doing wrong?


I included ITestContext as a parameter in the TestMethod but Retry fails.

In the example below if i include ITestContext in the parameter then during the retry method Test1() is not called the second time.


BUT if i remove ITestContext from the method parameter then method Test1() is called the second time and result is as expected.


public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }

    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }       
}

Result: if i exclude parameter "ITestContext context" in the test method  
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). Test1(): 1 : apple [this is what is desired after retry]

Result: if i include parameter "ITestContext context" in the test method
Test1(): 1 : apple This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s). ::: note here that test method was not called the second time [WHY????]

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Zhhv6BthAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Zhhv6BthAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
Visit this group at <a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

Krishnan Mahadevan

What version of TestNG are you working with ? When I attempted at running your sample using TestNG 6.11, I see a “wrong number of arguments” exception.

 

AFAIK, TestNG does not know how to inject a ITestContext when there’s a data provider involved.

 

So you would need to either inject the ITestContext as shown below :

 

public class RetryProvider {
   
@Test(dataProvider = "datasource", retryAnalyzer = RetryAnalyzer.class)
   
public void Test1(int code, String type, ITestContext context) {
        System.
out.println("Test1(): " + code + " : " + type);
        Assert.fail();
//** enforce a retry **/
   
}

   
@DataProvider(name = "datasource")
   
public Object[][] getData(ITestContext ctx) {
        Object[][] data =
new Object[1][3];
        data[
0][0] = 1;
        data[
0][1] = "apple";
        data[
0][2] = ctx;
       
return data;
    }
}

 

 

(or) you should just skip injecting the ITestContext

 

 

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/

 

From: <[hidden email]> on behalf of <[hidden email]>
Reply-To: <[hidden email]>
Date: Monday, March 27, 2017 at 10:38 AM
To: testng-users <[hidden email]>
Subject: Re: [testng-users] Re: TestNG Retry failing using DataProvider if i include ITestContext context in Test Method

 

Thanks Krishnan. Here is the sample code:

public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }

    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }      
}


Result: if i exclude parameter "ITestContext context" in the test method

Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
Test1(): 1 : apple  [this is what is desired after retry]


Result: if i include parameter "ITestContext context" in the test method

Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
::: note here that test method was not called the second time [WHY????]


The Retry Code:

public class RetryListener implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if (retry == null) {
            annotation.setRetryAnalyzer(RetryAnalyzer.class);
        }
    }
}

public class RetryAnalyzer implements IRetryAnalyzer{
    private int retryCount = 0;
    private int maxRetryCount = 1;

    @Override
    public boolean retry(ITestResult result) {
        if (retryCount < maxRetryCount) {
            System.out.println("Retrying test " + result.getName() + " with status "
                    + getResultStatusName(result.getStatus()) + " for the " + (retryCount + 1) + " time(s).");
            retryCount++;
            return true;
        }
        return false;
    }

    public String getResultStatusName(int status) {
        String resultName = null;
        if (status == 1) resultName = "SUCCESS";
        if (status == 2) resultName = "FAILURE";
        if (status == 3) resultName = "SKIP";
        return resultName;
    }
}

The xml:

...<suite name="NGBug Suite">
    <listeners><listener class-name="com.NGtest.RetryListener" /></listeners>
    <test name="NGBug Test"><classes><class name="com.NGtest.RetryProvider" /></classes>
    </test>
</suite>






On Monday, March 27, 2017 at 10:15:46 AM UTC+5:30, Krishnan wrote:

Can you please help share a quick sample which I can execute to recreate the problem ?

 


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 Mon, Mar 27, 2017 at 9:50 AM, <<a href="javascript:" target="_blank">bipo...@...> wrote:

Hello Krishnan:

Thank you. Worked perfectly. Reporter.getCurrentTestResult().getTestContext().setAttribute("type", type);

Yours is better. I was working on getting the context from the @DataProvider through a class variable.

Question still that passing the ITestContext as a @Test parameter caused the problem with TestNG's Retry feature Only.
Any idea if this was intentional behaviour?

Thanks,
B


 

On Sunday, March 26, 2017 at 1:11:09 PM UTC+5:30, [hidden email] wrote:

Hello: is there another way to get the ITestContext in a test method? Or what am i doing wrong?

 

I included ITestContext as a parameter in the TestMethod but Retry fails.

In the example below if i include ITestContext in the parameter then during the retry method Test1() is not called the second time.

 

BUT if i remove ITestContext from the method parameter then method Test1() is called the second time and result is as expected.

 

public class RetryProvider {
    @Test (dataProvider = "datasource")
    public void Test1(int code, String type, ITestContext context){
        System.out.println("Test1(): " + code + " : " + type);
        Assert.fail(); //** enforce a retry **/
    }
 
    @DataProvider(name = "datasource")
    public Object[][] getData(){
        Object[][] data = new Object[1][2];
        data[0][0] = 1;
        data[0][1] = "apple";
        return data;
    }       
}

 

Result: if i exclude parameter "ITestContext context" in the test method  
Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
Test1(): 1 : apple  [this is what is desired after retry]

Result: if i include parameter "ITestContext context" in the test method
Test1(): 1 : apple
This is from IRetryAnalyzer ::: Retrying test Test1 with status FAILURE for the 1 time(s).
::: note here that test method was not called the second time [WHY????]

--
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 <a href="javascript:" target="_blank">testng-users...@....
To post to this group, send email to <a href="javascript:" target="_blank">testng...@....
Visit this group at https://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 https://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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Loading...