Assertions in TestNG



Methods Description
1 assertEquals (String actual, String expected);
  1. It accepts two string arguments.
  2. It’ll check whether both are equal, if not it’ll fail the test.
2 assertEquals (String actual, String expected, String message);
  1. It accepts three string arguments.
  2. It’ll test whether both are same, if not it’ll fail the test.
  3. Also, it’ll throw a message that we provide.
3 assertEquals (boolean actual, boolean expected);
  1. It assumes two boolean arguments.
  2. It tests them for equality, if not it’ll fail the test.
4 assertEquals (java.util.Collection actual, java.util.Collection expected, java.lang.String message);
  1. It accepts two collection type objects.
  2. It checks whether they hold same elements and with the same order.
  3. It’ll fail the test if the above condition doesn’t meet.
  4. Also, you’ll see the message appear in the report.
5 assertTrue(< condition>);
  1. It accepts one boolean argument.
  2. It tests that the given condition is true.
  3. If it fails, then an < AssertionError> would occur.
6 assertTrue(< condition>, message);
  1. It assumes one boolean argument and a message.
  2. It asserts that the given condition is true.
  3. If it fails, then an < AssertionError> would occur with the message you passed.
7 assertFalse(< condition>);
  1. It accepts one boolean argument.
  2. It checks that the given condition is false.
  3. If it doesn’t pass, then an < AssertionError> would occur.
8 assertFalse(< condition>, message);
  1. It assumes one boolean argument and a message.
  2. It asserts that the given condition is false.
  3. If it fails, then an < AssertionError> would occur with the message you passed.

There are two types of Assertion:

Hard Assertion:

We already discussed it earlier.

Soft Assertion:

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:


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: