Set testNg result(Pass/Fail) explicitly at the end of test script execution

6,903 views
Skip to first unread message

Gajendra Jain

unread,
Nov 11, 2013, 6:11:59 AM11/11/13
to seleniu...@googlegroups.com
Hi,

I am using TestNG framework for Selenium Webdriver. 

I am not using any assertion in my framework for verification. Instead i am using  if else condition for verification. 
Based on Condition I am logging the statement using Reporter.log()

Ex: 1. For Pass, I am prefixing the statement with Pass::
        Reporter.Log("Pass:: Login succesful")

2. For Fail, I am prefixing the statement with Pass::
        Reporter.Log("Fail:: Login Failed").

At the end of execution, my test will be passed always. but based on the reporter log analysis we are verifying if the test is really passed or failed.

Can i explicitly set the Test result(Pass/Fail) at the end of each test execution? if yes, kindly let me know how could i achieve this in TestNg?

Regards,
Gajendra

Oscar Rieken

unread,
Nov 11, 2013, 8:01:59 AM11/11/13
to seleniu...@googlegroups.com
Can you give an example of what a test looks like. 

You should never have a test suite that always passes. If you have failures it should fail thats the whole point of having tests. And if you have to check a log to see if they pass or fail manually every time... what is the point of having automation?



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAEQ_fx%3DFh42c-YQVe80pt%2Bsyp1EKQzLaHPMXgpTHa9iuyHtCZw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Gajendra Jain

unread,
Nov 12, 2013, 12:39:36 AM11/12/13
to seleniu...@googlegroups.com
I totally agree with you.

Thats i am having a static variable testStatus, and I making them pass or fail based on the my verification. and the end i.e @afterclass i have to verify the testStatus. If it is fail, then i should make that test as failed.

I want to implement the above thing using TestNg

EX: something like below statement to fail. I don't how to create an object of TestResult and make them pass or fail.

                     testResult.setTestResult(ITestResult.FAILURE);

Regards,
Gajendra


Krishnan Mahadevan

unread,
Nov 12, 2013, 1:24:30 AM11/12/13
to Selenium Users
Gajendra

You can gain access to the current @Test method's ITestResult via Reporter.getCurrentTestResult().setStatus(ITestResult.FAILURE)

That being said, why would you not want to use asserts ?
Your if..else blocks are essentially doing the same here as well.

If you are not comfortable using Asserts, then you might want to throw Exceptions which would force TestNG to fail a test. I dont know what would be your use case wherein you would want to do this.



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/


Gajendra Jain

unread,
Nov 12, 2013, 6:29:32 AM11/12/13
to seleniu...@googlegroups.com
Thanks Krishnan,

i used  Reporter.getCurrentTestResult().setStatus(ITestResult.FAILURE) in my @Test, but still the Test result is PASSED. Could you please let me know what is wrong in this?


public class CustomReportTest {
    @Test
    public void testCustomReportTest() throws InterruptedException {
        
        Reporter.getCurrentTestResult().setStatus(ITestResult.FAILURE);

    }
}

Result:

[TestNG] Running:
  C:\Users\A045534\AppData\Local\Temp\testng-eclipse-339467447\testng-customsuite.xml

PASSED: testCustomReportTest

I wanted to make the test Result as 

FAILED: testCustomReportTest 


The reason for not using asserts are, if assert fails, the test stops there itself. 
but i want to continue till the end of the execution and based on status(maintained by my test script), i should be able to  set the status of the test PASS or FAIL.

Regards,
Gajendra



Krishnan Mahadevan

unread,
Nov 12, 2013, 9:39:54 AM11/12/13
to seleniu...@googlegroups.com
If you would like the test to run to completion before being marked passed/failed you should be using SoftAssert in TestNG. 

For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAEQ_fx%3D4vvY7G6x0JMJ8P6mcJobnBRrgoLcTqohzqeWHGLuLgw%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.


--

Kaushik S

unread,
Jul 1, 2017, 3:12:02 PM7/1/17
to Selenium Users
I'm sorry if I'm over simplifying, but Assert.assertTrue(false) will work I guess.
Reply all
Reply to author
Forward
0 new messages