ducati1212
ducati1212

Reputation: 201

Pass custom objects to TestNG result listener

I have a TestNG result listener that I reference from my Suite file

   <!-- Result Listener -->
<listeners>
    <listener class-name="com.test.automation.testng.ResultListener" methods="generateReport"/>
</listeners>

I use that result listener to write to a DB and store all my test results in mysql. This works great except now I want to to add in some custom information such as timing data that I only have access to in my test. Not the timing of the actual test so I can't just use start and end time.

Is there a way to pass in a custom object to my result listener or the suite result object so I can then use that in my listener to write more info to the DB.

Upvotes: 2

Views: 1466

Answers (2)

niharika_neo
niharika_neo

Reputation: 8531

You can use

Reporter.getCurrentTestResult().setAttribute(name,value)

in your tests and then use the result object to get the attribute.

Upvotes: 3

Shamik
Shamik

Reputation: 1609

something like this can be used as a hack -

Reporter.log("Time:"+time); // in the test

then in the listener

public void onTestFailure(ITestResult arg0) {
          Reporter.getOutput(arg0); // this will fetch that log and you can parse it and make it meaningful

}

Of course this will fetch all the logs as a List and you have to work on it.

Upvotes: 0

Related Questions