Reputation: 57
I am new to Selenium, using testNG framework. Below is the code I am trying to understand
@Test
public void softAssertionTest() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(1, 1,"Error 1");
softAssert.assertEquals("a","b","Error ab");
System.out.println("Line after assertions");
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method1"+ verificationErrors);
}
}
@Test
public void softAssertionTest2() {
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(3, 4,"Error 3,4");
softAssert.assertEquals("c","d","Error cd");
System.out.println("Line after assertions method 2");
}
@AfterTest
public void afterTest(){
softAssert.assertAll();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}
}
Q1) I read it is not a good practice to do assertions in configuration methods. Rather do them in each test method. Hence I modify the code as below
@Test
public void softAssertionTest() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(1, 1,"Error 1");
softAssert.assertEquals("a","b","Error ab");
System.out.println("Line after assertions");
doSoftAssert();
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method1"+ verificationErrors);
}
}
@Test
public void softAssertionTest2() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(3, 4,"Error 3,4");
softAssert.assertEquals("c","d","Error cd");
System.out.println("Line after assertions method 2");
doSoftAssert();
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method2"+ verificationErrors);
}
}
public void doSoftAssert(){
softAssert.assertAll();
/*String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}*/
}
When I run this, I see calling softAssert.assertAll()
from softAssertionTest2()
executes the assertions in softAssertionTest1()
as well, duplicating the asserts. How can I have assertions being asserted only once?
Q2) Is appending the errors to verificationErrorString
the right way or is it advisable to add these errors in a HashMap
?
Upvotes: 0
Views: 737
Reputation: 14746
How can I have assertions being asserted only once?
Every test should instantiate its own SoftAssert
object and it should not be shared across multiple @Test
methods.
Is appending the errors to
verificationErrorString
the right way or is it advisable to add these errors in aHashMap
?
You should never wrap your assertions with a try..catch
block because it can cause you to gobble exceptions and it adds a lot of boiler plate code to your tests.
If your intent is basically to capture the verification messages, then you should be looking at sub-classing org.testng.asserts.SoftAssert
where you:
org.testng.asserts.Assertion#onAssertSuccess
(if you want to capture passed assertions) and org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError)
(if you want to capture failed assertions)Upvotes: 1