Methods | Description | |
---|---|---|
1 | assertEquals (String actual, String expected); |
|
2 | assertEquals (String actual, String expected, String message); |
|
3 | assertEquals (boolean actual, boolean expected); |
|
4 | assertEquals (java.util.Collection actual, java.util.Collection expected, java.lang.String message); |
|
5 | assertTrue(< condition>); |
|
6 | assertTrue(< condition>, message); |
|
7 | assertFalse(< condition>); |
|
8 | assertFalse(< condition>, message); |
|
There are two types of Assertion:
We already discussed it earlier.
It is a custom assert mechanism supported by TestNG’s < org.testng.asserts.Softassert> package.
We use it when a test has to continue execution even after an assertion fails in the sequence.
The First Scenario:
package com.javabykiran;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertEx {
SoftAssert softAssert = new SoftAssert();
@Test
public void jbkTestCase_One() {
System.out.println(1);
softAssert.assertEquals(2, 3); // fail
System.out.println(2);
softAssert.assertEquals(4, 3); // fail
System.out.println(3);
softAssert.assertEquals(5, 5); // pass
System.out.println(4);
softAssert.assertAll();
}
@Test
public void jbkTestCase_Two() {
System.out.println(11);
softAssert.assertEquals(22, 33); // fail
System.out.println(22);
softAssert.assertEquals(33, 44); // fail
System.out.println(33);
softAssert.assertEquals(22, 22); // fail
System.out.println(44);
softAssert.assertAll();
}
}
Output:
1
2
3
4
11
22
33
44
FAILED: jbkTestCase_One
java.lang.AssertionError: The following asserts failed:
expected [3] but found [2],
expected [3] but found [4]
FAILED: jbkTestCase_Two
java.lang.AssertionError: The following asserts failed:
expected [3] but found [2],
expected [3] but found [4],
expected [33] but found [22],
expected [44] but found [33]
===============================================
Default test
Tests run: 2, Failures: 2, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 2, Skips: 0
See problem in above output. Everything executed then combine results we see Now we will see second approach where it will properly execute and that will be a correct way of using soft assert.
We will create a separate object for each test case.
The Second Scenario:
package com.javabykiran;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertEx {
@Test
public void jbkTestCase_One() {
SoftAssert softAssert1 = new SoftAssert();
System.out.println(1);
softAssert1.assertEquals(2, 3); // fail
System.out.println(2);
softAssert1.assertEquals(4, 3); // fail
System.out.println(3);
softAssert1.assertEquals(5, 5); // pass
System.out.println(4);
softAssert1.assertAll();
}
@Test
public void jbkTestCase_Two() {
SoftAssert softAssert2 = new SoftAssert();
System.out.println(11);
softAssert2.assertEquals(22, 33); // fail
System.out.println(22);
softAssert2.assertEquals(33, 44); // fail
System.out.println(33);
softAssert2.assertEquals(22, 22); // fail
System.out.println(44);
softAssert2.assertAll();
}
@Test
public void jbkTestCase_Three() {
SoftAssert softAssert3 = new SoftAssert();
System.out.println(11);
softAssert3.assertEquals(34, 34);
}
}
Output:
1
2
3
4
11
11
22
33
44
PASSED: jbkTestCase_Three
FAILED: jbkTestCase_One
java.lang.AssertionError: The following asserts failed:
expected [3] but found [2],
expected [3] but found [4]
at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
FAILED: jbkTestCase_Two
java.lang.AssertionError: The following asserts failed:
expected [33] but found [22],
expected [44] but found [33]
at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
==================================================================
Default test
Tests run: 3, Failures: 2, Skips: 0
==================================================================
==================================================================
Default suite
Total tests run: 3, Failures: 2, Skips: 0
==================================================================