How not to run a method based on Data provider values

708 views
Skip to first unread message

sheetal singh

unread,
Mar 1, 2017, 12:21:06 PM3/1/17
to testng-users
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.        

⇜Krishnan Mahadevan⇝

unread,
Mar 1, 2017, 1:11:21 PM3/1/17
to testng...@googlegroups.com
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/

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

sheetal singh

unread,
Mar 2, 2017, 12:41:05 AM3/2/17
to testng-users
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
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
testng.png

⇜Krishnan Mahadevan⇝

unread,
Mar 2, 2017, 1:51:07 AM3/2/17
to testng...@googlegroups.com
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.

--

sheetal singh

unread,
Mar 2, 2017, 3:52:47 AM3/2/17
to testng...@googlegroups.com
Thanks Krishnan,

I tried using mvn command and it worked :)


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
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 testng-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages