How not to run a method based on Data provider values

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

How not to run a method based on Data provider values

sheetal singh
Hi,

I read lot of forums but not getting solutions.

Case:
1. I am reading a csv files using Data Provider
2. @Test method runs for every value given by Data Provider
 Based on csv data I skip few test, but in results I get skipped test as well

Requirement:
What i was trying that when some data comes from Data provider and when I find that data is not what i want based on some condition then that test should not get run at all i.e. enabled = false
So this way skipped cased will not come into report.



@Test(dataProvider = "csvdataprovider",dataProviderClass=DataProviderClass.class)
    public void testRequest(String suite, String data){
       
        if(suite.equals("reg")){
            //further code
        }else{
            System.out.println("Dont report this test run");
            throw new SkipException("SUITE NOT MATCHING.....");   
        }

    }

in this case all skipped cases are getting logged into report which i dont want.        

--
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: How not to run a method based on Data provider values

Krishnan Mahadevan
There's nothing in TestNG that would let you do this out of the box.

But you can try building something like below [ Its a dirty hack but it will work ]


import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.Iterator;

@Listeners(SampleTestClass.Listener.class)
public class SampleTestClass implements IHookable {
    @Test(dataProvider = "dp")
    public void testMethod(String name) {
        System.err.println("Incoming parameter was " + name);
    }

    @DataProvider(name = "dp")
    public Object[][] getData() {
        return new Object[][] {
            {"junit"},
            {"testng"}
        };
    }

    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        String parameter = (String) callBack.getParameters()[0];
        if (! "junit".equalsIgnoreCase(parameter)) {
            callBack.runTestMethod(testResult);
        } else {
            testResult.setAttribute("disabled", true);
            throw new SkipException("Skipping ");
        }
    }

    public static class Listener implements ITestListener {

        @Override
        public void onTestStart(ITestResult result) {

        }

        @Override
        public void onTestSuccess(ITestResult result) {

        }

        @Override
        public void onTestFailure(ITestResult result) {

        }

        @Override
        public void onTestSkipped(ITestResult result) {

        }

        @Override
        public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

        }

        @Override
        public void onStart(ITestContext context) {

        }

        @Override
        public void onFinish(ITestContext context) {
            Iterator<ITestResult> iterator = context.getSkippedTests().getAllResults().iterator();
            while (iterator.hasNext()) {
                ITestResult result = iterator.next();
                if (Boolean.parseBoolean(result.getAttribute("disabled").toString())) {
                    iterator.remove();
                }
            }
        }
    }
}



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 Wed, Mar 1, 2017 at 2:34 PM, sheetal singh <[hidden email]> wrote:
Hi,

I read lot of forums but not getting solutions.

Case:
1. I am reading a csv files using Data Provider
2. @Test method runs for every value given by Data Provider
 Based on csv data I skip few test, but in results I get skipped test as well

Requirement:
What i was trying that when some data comes from Data provider and when I find that data is not what i want based on some condition then that test should not get run at all i.e. enabled = false
So this way skipped cased will not come into report.



@Test(dataProvider = "csvdataprovider",dataProviderClass=DataProviderClass.class)
    public void testRequest(String suite, String data){
       
        if(suite.equals("reg")){
            //further code
        }else{
            System.out.println("Dont report this test run");
            throw new SkipException("SUITE NOT MATCHING.....");   
        }

    }

in this case all skipped cases are getting logged into report which i dont want.        

--
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: How not to run a method based on Data provider values

sheetal singh
Hi Krishnan,

Thanks for your quick reply...

I tried running your code but this is showing skipped test in TestNG report ideally we should not get any skipped test in report (only the pass or failed cases)

See the results screenshot

On Wednesday, 1 March 2017 23:41:21 UTC+5:30, Krishnan wrote:
There's nothing in TestNG that would let you do this out of the box.

But you can try building something like below [ Its a dirty hack but it will work ]


import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.Iterator;

@Listeners(SampleTestClass.Listener.class)
public class SampleTestClass implements IHookable {
    @Test(dataProvider = "dp")
    public void testMethod(String name) {
        System.err.println("Incoming parameter was " + name);
    }

    @DataProvider(name = "dp")
    public Object[][] getData() {
        return new Object[][] {
            {"junit"},
            {"testng"}
        };
    }

    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        String parameter = (String) callBack.getParameters()[0];
        if (! "junit".equalsIgnoreCase(parameter)) {
            callBack.runTestMethod(testResult);
        } else {
            testResult.setAttribute("disabled", true);
            throw new SkipException("Skipping ");
        }
    }

    public static class Listener implements ITestListener {

        @Override
        public void onTestStart(ITestResult result) {

        }

        @Override
        public void onTestSuccess(ITestResult result) {

        }

        @Override
        public void onTestFailure(ITestResult result) {

        }

        @Override
        public void onTestSkipped(ITestResult result) {

        }

        @Override
        public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

        }

        @Override
        public void onStart(ITestContext context) {

        }

        @Override
        public void onFinish(ITestContext context) {
            Iterator<ITestResult> iterator = context.getSkippedTests().getAllResults().iterator();
            while (iterator.hasNext()) {
                ITestResult result = iterator.next();
                if (Boolean.parseBoolean(result.getAttribute("disabled").toString())) {
                    iterator.remove();
                }
            }
        }
    }
}



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 Wed, Mar 1, 2017 at 2:34 PM, sheetal singh <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="dfYeMpLFCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">sheet...@...> wrote:
Hi,

I read lot of forums but not getting solutions.

Case:
1. I am reading a csv files using Data Provider
2. @Test method runs for every value given by Data Provider
 Based on csv data I skip few test, but in results I get skipped test as well

Requirement:
What i was trying that when some data comes from Data provider and when I find that data is not what i want based on some condition then that test should not get run at all i.e. enabled = false
So this way skipped cased will not come into report.



@Test(dataProvider = "csvdataprovider",dataProviderClass=DataProviderClass.class)
    public void testRequest(String suite, String data){
       
        if(suite.equals("reg")){
            //further code
        }else{
            System.out.println("Dont report this test run");
            throw new SkipException("SUITE NOT MATCHING.....");   
        }

    }

in this case all skipped cases are getting logged into report which i dont want.        

--
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="dfYeMpLFCAAJ" 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="dfYeMpLFCAAJ" 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.

testng.png (116K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How not to run a method based on Data provider values

Krishnan Mahadevan
As I mentioned before there is no out of the box way of doing it. What I suggested was a dirty hack and it will affect the reports.

Getting the same behavior in the IDE plugin is not something it might be able to do.

But then again people run via IDEs only when developing tests. After that you should be worried about only the reports of the execution via a build tool. My testing indicated that this solution works when you run tests via maven.


On Thu, Mar 2, 2017, 11:11 sheetal singh <[hidden email]> wrote:
Hi Krishnan,

Thanks for your quick reply...

I tried running your code but this is showing skipped test in TestNG report ideally we should not get any skipped test in report (only the pass or failed cases)

See the results screenshot

On Wednesday, 1 March 2017 23:41:21 UTC+5:30, Krishnan wrote:
There's nothing in TestNG that would let you do this out of the box.

But you can try building something like below [ Its a dirty hack but it will work ]


import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.Iterator;

@Listeners(SampleTestClass.Listener.class)
public class SampleTestClass implements IHookable {
    @Test(dataProvider = "dp")
    public void testMethod(String name) {
        System.err.println("Incoming parameter was " + name);
    }

    @DataProvider(name = "dp")
    public Object[][] getData() {
        return new Object[][] {
            {"junit"},
            {"testng"}
        };
    }

    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        String parameter = (String) callBack.getParameters()[0];
        if (! "junit".equalsIgnoreCase(parameter)) {
            callBack.runTestMethod(testResult);
        } else {
            testResult.setAttribute("disabled", true);
            throw new SkipException("Skipping ");
        }
    }

    public static class Listener implements ITestListener {

        @Override
        public void onTestStart(ITestResult result) {

        }

        @Override
        public void onTestSuccess(ITestResult result) {

        }

        @Override
        public void onTestFailure(ITestResult result) {

        }

        @Override
        public void onTestSkipped(ITestResult result) {

        }

        @Override
        public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

        }

        @Override
        public void onStart(ITestContext context) {

        }

        @Override
        public void onFinish(ITestContext context) {
            Iterator<ITestResult> iterator = context.getSkippedTests().getAllResults().iterator();
            while (iterator.hasNext()) {
                ITestResult result = iterator.next();
                if (Boolean.parseBoolean(result.getAttribute("disabled").toString())) {
                    iterator.remove();
                }
            }
        }
    }
}



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 Wed, Mar 1, 2017 at 2:34 PM, sheetal singh <[hidden email]> wrote:
Hi,

I read lot of forums but not getting solutions.

Case:
1. I am reading a csv files using Data Provider
2. @Test method runs for every value given by Data Provider
 Based on csv data I skip few test, but in results I get skipped test as well

Requirement:
What i was trying that when some data comes from Data provider and when I find that data is not what i want based on some condition then that test should not get run at all i.e. enabled = false
So this way skipped cased will not come into report.



@Test(dataProvider = "csvdataprovider",dataProviderClass=DataProviderClass.class)
    public void testRequest(String suite, String data){
       
        if(suite.equals("reg")){
            //further code
        }else{
            System.out.println("Dont report this test run");
            throw new SkipException("SUITE NOT MATCHING.....");   
        }

    }

in this case all skipped cases are getting logged into report which i dont want.        

--
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.
--

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/

--
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: How not to run a method based on Data provider values

sheetal singh
Thanks Krishnan,

I tried using mvn command and it worked :)


On Thu, Mar 2, 2017 at 12:20 PM, ⇜Krishnan Mahadevan⇝ <[hidden email]> wrote:
As I mentioned before there is no out of the box way of doing it. What I suggested was a dirty hack and it will affect the reports.

Getting the same behavior in the IDE plugin is not something it might be able to do.

But then again people run via IDEs only when developing tests. After that you should be worried about only the reports of the execution via a build tool. My testing indicated that this solution works when you run tests via maven.


On Thu, Mar 2, 2017, 11:11 sheetal singh <[hidden email]> wrote:
Hi Krishnan,

Thanks for your quick reply...

I tried running your code but this is showing skipped test in TestNG report ideally we should not get any skipped test in report (only the pass or failed cases)

See the results screenshot

On Wednesday, 1 March 2017 23:41:21 UTC+5:30, Krishnan wrote:
There's nothing in TestNG that would let you do this out of the box.

But you can try building something like below [ Its a dirty hack but it will work ]


import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.Iterator;

@Listeners(SampleTestClass.Listener.class)
public class SampleTestClass implements IHookable {
    @Test(dataProvider = "dp")
    public void testMethod(String name) {
        System.err.println("Incoming parameter was " + name);
    }

    @DataProvider(name = "dp")
    public Object[][] getData() {
        return new Object[][] {
            {"junit"},
            {"testng"}
        };
    }

    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        String parameter = (String) callBack.getParameters()[0];
        if (! "junit".equalsIgnoreCase(parameter)) {
            callBack.runTestMethod(testResult);
        } else {
            testResult.setAttribute("disabled", true);
            throw new SkipException("Skipping ");
        }
    }

    public static class Listener implements ITestListener {

        @Override
        public void onTestStart(ITestResult result) {

        }

        @Override
        public void onTestSuccess(ITestResult result) {

        }

        @Override
        public void onTestFailure(ITestResult result) {

        }

        @Override
        public void onTestSkipped(ITestResult result) {

        }

        @Override
        public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

        }

        @Override
        public void onStart(ITestContext context) {

        }

        @Override
        public void onFinish(ITestContext context) {
            Iterator<ITestResult> iterator = context.getSkippedTests().getAllResults().iterator();
            while (iterator.hasNext()) {
                ITestResult result = iterator.next();
                if (Boolean.parseBoolean(result.getAttribute("disabled").toString())) {
                    iterator.remove();
                }
            }
        }
    }
}



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 Wed, Mar 1, 2017 at 2:34 PM, sheetal singh <[hidden email]> wrote:
Hi,

I read lot of forums but not getting solutions.

Case:
1. I am reading a csv files using Data Provider
2. @Test method runs for every value given by Data Provider
 Based on csv data I skip few test, but in results I get skipped test as well

Requirement:
What i was trying that when some data comes from Data provider and when I find that data is not what i want based on some condition then that test should not get run at all i.e. enabled = false
So this way skipped cased will not come into report.



@Test(dataProvider = "csvdataprovider",dataProviderClass=DataProviderClass.class)
    public void testRequest(String suite, String data){
       
        if(suite.equals("reg")){
            //further code
        }else{
            System.out.println("Dont report this test run");
            throw new SkipException("SUITE NOT MATCHING.....");   
        }

    }

in this case all skipped cases are getting logged into report which i dont want.        

--
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 testng-users...@googlegroups.com.
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.
--

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/

--
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/Wv-84-X8ZgU/unsubscribe.
To unsubscribe from this group and all its topics, 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...