How to capture information for skipped tests

52 views
Skip to first unread message

SPS

unread,
Aug 7, 2016, 7:53:14 AM8/7/16
to testng-users
Hello all,
I need some help on how to capture information when test skips.I want to capture dataRow for my reporting purpose when  testB skips on failure of testA. Now it reports as null for testB in  my listener. If somebody can help me resolve this, that will be great

Thanks

public classA {


public classA(){
  dataRow = 1;
}


@Test
public void testA(){
   Reporter.getcurrentTestResult().setAttribute("datrow",datarow)
}

@Test { dependson ="testA"}
public void testB(){
   Reporter.getcurrentTestResult().setAttribute("datrow",datarow)
}



}

⇜Krishnan Mahadevan⇝

unread,
Aug 7, 2016, 9:58:59 AM8/7/16
to testng...@googlegroups.com
Sneha,

You should be able to get this done by plugging in an implementation for org.testng.ITestListener (TestNG Listener) to your test.

You can learn more about listeners by referring to this blog post of mine :



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.

SPS

unread,
Aug 7, 2016, 4:18:01 PM8/7/16
to testng-users
Yeah, I try to gather information in listener. the problem is if testA fails then testB skips. TestResult object for testB has datarow as null. we want data row as 1. We use factory method as datarow indicates the data lines

⇜Krishnan Mahadevan⇝

unread,
Aug 7, 2016, 10:53:18 PM8/7/16
to testng...@googlegroups.com
Sneha,
Since you mention factories, you can do something like below :

Define an interface which will expose the data that is passed to your test class instance.
Have your test class implement this interface so that it can be invoked from outside.
In your onSkipped() implementation of your listener you can now invoke this interface method to get this data.
Here's a sample implementation which shows what I am talking about.

public interface RowInfo {
int getRowId();
}
@Listeners (InfoOnSkippedTests.TestDetailsListener.class)
public class InfoOnSkippedTests implements RowInfo {
private int rowId;

@Factory (dataProvider = "dp")
public InfoOnSkippedTests(int rowId) {
this.rowId = rowId;
}

@DataProvider (name = "dp")
public static Object[][] getData() {
return new Object[][] {
{1},
{2}
};
}

@Test
public void testMethod1() {
System.err.println("Hello from testMethod1()");
throw new RuntimeException("Failing testMethod1()");
}

@Test (dependsOnMethods = "testMethod1")
public void testMethod2() {
System.err.println("This is never going to be executed");

}

@Override
public int getRowId() {
return rowId;
}

public static class TestDetailsListener extends TestListenerAdapter {

@Override
public void onTestSkipped(ITestResult result) {
Object instance = result.getMethod().getInstance();
if (! (instance instanceof RowInfo)) {
return;
}
int rowId = ((RowInfo) instance).getRowId();
System.err.println("Test Method " + result.getMethod().getConstructorOrMethod().getName()
            + "() skipped for rowId: " + rowId);
}
}
}

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/

--

SPS

unread,
Aug 11, 2016, 5:47:51 AM8/11/16
to testng-users
Thanks Krishnan. It works.

On Sunday, August 7, 2016 at 6:53:14 AM UTC-5, SPS wrote:
Reply all
Reply to author
Forward
0 new messages