Reputation: 73
How can I continue the test execution in TestNG if an assertion fails? How can I report the failure in the HTML report of TestNG?
When I run the following code, the lines after assertion are executed but in the reports the assertion failure is not listed:
@Test
public void googleSearch(){
driver.get("http://www.google.co.in/");
System.out.println(" ---------- Start -------------");
try {
Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");
} catch (Throwable t) {
System.out.println("Error");
}
System.out.println(" ---------- End -------------");
}
Upvotes: 2
Views: 11668
Reputation: 879
Since you have caught the error, this will not fail. I recreated the situation as following. This did not fail. But a Throwable is thrown. Error
is printed.
public class TNG {
WebDriver driver;
@Test
public void googleSearch(){
System.setProperty("webdriver.chrome.driver", "path to web driver");
driver = new ChromeDriver();
driver.get("http://www.google.co.in/");
System.out.println(" ---------- Start -------------");
try {
Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");
} catch (Throwable t) {
System.out.println("Error");
}
System.out.println(" ---------- End -------------");
}
@Test
public void anotherTest(){
System.out.println("another test");
}
}
Following is the result from the execution of the test.
another test
Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 43423
Only local connections are allowed.
Jul 20, 2017 5:55:33 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
---------- Start -------------
Error
---------- End -------------
PASSED: anotherTest
PASSED: googleSearch
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
And if you want to continue test execution even though some tests failed , you can simply write methods with @Test
annotation like the anotherTest() method that I have created. But you can not guarantee the order of the test execution.
In here above anotherTest() has been executed first.
Upvotes: 0
Reputation: 641
I suggest to use soft assertions, which are provided in TestNG natively
package automation.tests;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
public class MyTest {
private Assertion hardAssert = new Assertion();
private SoftAssert softAssert = new SoftAssert();
}
@Test
public void testForSoftAssertionFailure() {
softAssert.assertTrue(false);
softAssert.assertEquals(1, 2);
softAssert.assertAll();
}
Upvotes: 1
Reputation: 47
You can use this code in catch block: -
org.testng.Assert.fail("expected and actual result do not match");
Lets take example of following code : -
String expectedtitle="xyz";
String actualtitle="xywz";
try {
Assert.assertEquals(expectedtitle, actualtitle);
} catch(Throwable t) {
org.testng.Assert.fail("expected and actual result do not match");
}
Upvotes: 3
Reputation: 2734
Agreed, continuing the test after a failure is not best practice. Use after*Methods() for stuff.
Specifically for logging of test (configuration) start and end however, don't do it in the test class at all - create a listener, and do the logging from the listener.
You could extend TestListenerAdapter and implement all methods that it dictates:
http://testng.org/javadoc/org/testng/TestListenerAdapter.html
public class YourTestResultMonitor extends TestListenerAdapter {
*snip*
}
Upvotes: 1
Reputation: 158
Rather than catching assertion errors, in your test design preferably try to avoid the need to continue tests after they fail.
In your example:
@BeforeMethod
public void openPage() {
driver.get("http://www.google.co.in/");
System.out.println(" ---------- Start -------------");
}
@Test
public void googleSearchThis(){
Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");
}
@Test
public void googleSearchThat(){
// assert for other things you want to test on the same page
}
@AfterMethod
public void closePage() {
System.out.println(" ---------- End -------------");
}
Upvotes: 0