Using Reporter.log() in @AfterMethod

2,511 views
Skip to first unread message

Sébastien Bouffard

unread,
Jan 25, 2012, 2:26:32 PM1/25/12
to testng-users
Hello

I just noticed that when I use Reporter.log() in a method that is
annotated with @AfterMethod, the output will be written only in the
file "reporter-output.html".

It will not be written to the html report file of the test itself thus
it won't show up when clicking on the "Show all outputs" link for a
specific method. For the output to show there, you have to call
Reporter.log() from the test method itself, before @AfterMethod is
called.

Is this by design?

Cédric Beust ♔

unread,
Jan 25, 2012, 2:44:02 PM1/25/12
to testng...@googlegroups.com
Hi Sébastien,

Kind of. The logs are indexed by methods, and since the (current) reports only display the test methods in the main view, anything logged in a method that's not a test method won't appear there.

I agree it would make sense to add the logs for some configuration methods.

-- 
Cédric




2012/1/25 Sébastien Bouffard <sebwe...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


MaC

unread,
Jan 26, 2012, 11:38:29 AM1/26/12
to testng-users
Actually if @afterMethod failes the logs go to the report.

This is also a kind of show stopper for us right now, sine we are
using smth like this:

@AfterMethod(alwaysRun = true)
protected void afterTest(final ITestResult result,
final ITestContext context) throws Exception {
Reporter.log("IN AFTERMETHOD");
if (!result.isSuccess() || result.getThrowable() != null) {

Reporter.log("TC X FAILED, see additional info in file Y");

} else {
Reporter.log("TC X PASSED");
}
}

But if there was no error nothing will be reported :(

I guess there is no workaround for this as of now?


THX and BR;

MaC



On 25 Jan., 20:44, Cédric Beust ♔ <ced...@beust.com> wrote:
> Hi Sébastien,
>
> Kind of. The logs are indexed by methods, and since the (current) reports
> only display the test methods in the main view, anything logged in a method
> that's not a test method won't appear there.
>
> I agree it would make sense to add the logs for some configuration methods.
>
> --
> Cédric
>
> 2012/1/25 Sébastien Bouffard <sebweb1...@gmail.com>

Jostar

unread,
May 9, 2012, 9:49:38 AM5/9/12
to testng...@googlegroups.com

Hi Cédric

The fact that i can't carry weight on the method-report in the @BeforeMethod
and @AfterMethod bothers me too...

Strange enough, the "Method" object that can be passed to any method
annotated with BeforeMtehod or AfterMethod is allready holding the effective
test-method.

So it seems the "m_currentTestResult" in the Reporter is perhaps just not
the right fact to use as an log-index! ;-)

Greets,
Arno
--
View this message in context: http://old.nabble.com/Using-Reporter.log%28%29-in-%40AfterMethod-tp33203292p33763635.html
Sent from the testng-users mailing list archive at Nabble.com.

Abdullah Mohiuddin

unread,
Jul 6, 2015, 2:02:05 PM7/6/15
to testng...@googlegroups.com, ced...@beust.com
Hi Cedric,

I am using testng-6.9.4.jar and I still see that Reporter.log() logs are showing in old reports but not in the new reports.  Has this been addressed yet or is it going to remain this way?  I really wanted testsOnly to be false or be able to be configured in xml Suite as parameter.
Is there any such option for me that I could use?  or is there a work around to get new reports show logs from non-test methods as well?

Thanks,
--Abdullah.

Abdullah Mohiuddin

unread,
Jul 6, 2015, 2:30:39 PM7/6/15
to testng...@googlegroups.com, ced...@beust.com
Just to elaborate on this, I am not seeing any NullPointerException (NPE) but the generated reports only show logs from test methods only and none from BeforeMethod, BeforeClass, AfterMethod, AfterClass, (and possible DataProvider methos as well, haven't tried that).  Tried this using 6.8.21 as well as 6.9.4 versions of testng jars.
Appreciate it if you can shed some light on how to achieve this or if it needs a fix, when it will be fixed?

--Abdullah.

Shawn McCarthy

unread,
Jul 6, 2015, 7:51:48 PM7/6/15
to testng...@googlegroups.com, ced...@beust.com
Reporter.log will not work correctly in AfterMethod or BeforeMethod. To get around this, I use onTestSuccess and onTestFailure, and onTestStart (from ITestListener - http://testng.org/javadoc/org/testng/ITestListener.html ). Reporter.log will work (as far as I have seen) from these methods.

Ibrahim Abedrabbo

unread,
Jan 18, 2017, 8:11:53 AM1/18/17
to testng-users
I Found a way to use Reporter.log("s") in @AfterMethod annotated method. 

@AfterMethod
public void reportTest(ITestResult result) {
 
Reporter.setCurrentTestResult(result);
 
Reporter.log("Message");
}


The method will receive ITestResult runtime object for the test that has just finished.

sonal baghel

unread,
May 18, 2017, 1:27:16 AM5/18/17
to testng-users
Thanks  Ibrahim , I tried it and working fine 
Reply all
Reply to author
Forward
0 new messages