When we are writing tests, the goal isn’t just to run the code but to make sure it’s doing what we expect. That’s where assertions come in – they let us confirm that our code behaves as it should. In TestNG, assertions play a key role, helping us verify if our expected outcomes match the actual results in the code. Let’s dive into how assertions work in TestNG, the types available, and why we should use them to make our tests as effective as possible.
Note: We have also attached a video at the end of this article. In case you are more comfortable with the video version, then please feel free to have a look
We will look at below points –
- What Are Assertions in TestNG?
- Why should we use Assertion?
- What are the different types of Assertions?
- When to Use Hard vs. Soft Assertions
- Commonly used Assert Methods
- Print Messages On Assertion failures
What Are Assertions in TestNG?
Assertions in TestNG are statements that check whether a particular condition holds true or false during test execution.
At its core, an assertion is like a checkpoint in a test. It’s a statement that either passes (meaning the code’s output matches our expectations) or fails (meaning it doesn’t).
Why should we use Assertion?
Assertions help us:
- Confirm Results: They validate if our code is working as it should.
- Automate Checks: With assertions, we don’t have to manually verify outputs every time we run tests.
- Save Debugging Time: Failed assertions tell us exactly what failed, making it easier to track down bugs.
- Boost Test Accuracy: Assertions make our tests more reliable, so we catch issues early.
What are the different types of Assertions?
There are two types of assertions in TestNG: hard assertion and soft assertion.
Hard Assertion
Hard assertions, or standard assertions, immediately halt the test execution if they fail. They are useful when we need the test to stop as soon as an expected condition is unmet.
Assert class is used for Hard Assertion in TestNG. It provides many methods, such as assertEquals
, assertTrue
, and others for hard assertions.
Here is one example
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test1() {
int actual = 5;
int expected = 3;
Assert.assertEquals(actual, expected);
System.out.println("Statement below the assert..");
}
}
In this example, we used assertEquals to compare the actual and expected values. As a result, the test case will fail because the actual value does not match the expected value. Additionally, any statements following the assertion will not be executed since a hard assertion stops the program when an assertion fails.
FAILED: test1
java.lang.AssertionError: expected [3] but found [5]
at org.testng.Assert.fail(Assert.java:99)
at org.testng.Assert.failNotEquals(Assert.java:1037)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
We can see that line 16 wasn’t printed in the console as the assertion failed and thus halted the execution at line 14.
Soft Assertion
Soft assertions, on the other hand, allow the test to continue running even if an assertion fails. It’ll log all the failed assertions, but the test will only be marked as failed at the end (if we want). This is helpful when we want to check several conditions in a single test.
Test marked as Passed
package Test;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class CodekruTest {
@Test
public void test1() {
int actual = 5;
int expected = 3;
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(actual, expected);
System.out.println("Statement below the Assertion..");
}
}
Output –
Statement below the Assertion..
PASSED: test1
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
Test marked as Failed
package Test;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class CodekruTest {
@Test
public void test1() {
int actual = 5;
int expected = 3;
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(actual, expected);
System.out.println("Statement below the Assertion..");
softAssert.assertAll();
}
}
Output –
Statement below the Assertion..
FAILED: test1
java.lang.AssertionError: The following asserts failed:
expected [3] but found [5]
at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:47)
at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:31)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
Some points to note
- In both scenarios, the statement after the assertion got printed in the console, indicating that the program continued executing even though the assertion had failed.
- Take a closer look at the code on the right-hand side; we’ve used “softAssert.assertAll()” at line 19. This line ensures that if any of the previous assertions fail, then the test case will be marked as failed. Without this line, the test case will be marked as passed even if some assertions fail, as was the case with the code on the left-hand side.
When to Use Hard vs. Soft Assertions
Hard Assertions
Use hard assertions when –
- A failed condition should stop the test execution immediately.
- The test depends on certain conditions being met before proceeding.
- Testing critical or fundamental aspects that must pass for subsequent assertions to be meaningful.
Soft Assertions
Use soft assertions when:
- You need to check multiple independent conditions within a single test.
- Failing conditions should not prevent the test from running to completion.
- The test output can provide comprehensive results of all failures for analysis.
Commonly used Assert Methods
1. assertEquals
The assertEquals
method checks if two values are equal. If the values don’t match, the assertion fails.
Example:
public class CodekruTest {
@Test
public void test1() {
int actual = 10;
int expected = 10;
Assert.assertEquals(actual, expected);
}
}
2. assertNotEquals
The assertNotEquals
method verifies that two values are not equal.
Example:
public class CodekruTest {
@Test
public void test1() {
int actual = 9;
int expected = 10;
Assert.assertNotEquals(actual, expected);
}
}
In this case, the test will be marked as passed because the actual and expected values do not match, and we are using the assertNotEquals method.
3. assertTrue
The assertTrue
method validates that a condition is true.
Example:
public class CodekruTest {
@Test
public void test1() {
int age = 21;
boolean isAdult = age > 18;
Assert.assertTrue(isAdult);
}
}
4. assertFalse
The assertFalse
method ensures that a condition is false.
Example:
public class CodekruTest {
@Test
public void test1() {
int age = 14;
boolean isAdult = age > 18;
Assert.assertFalse(isAdult);
}
}
5. assertNull
The assertNull
method checks that an object is null
.
Example:
public class CodekruTest {
@Test
public void test1() {
Object obj = null;
Assert.assertNull(obj);
}
}
6. assertNotNull
The assertNotNull
method verifies that an object is not null
.
Example:
public class CodekruTest {
@Test
public void test1() {
String website = "codekru";
Assert.assertNotNull(website);
}
}
Print Messages On Assertion failures
We can also print a message when an assertion fails to explain the reason for the failure. All assertions have an overloaded method that allows us to pass a message string as an argument, which will be printed in the console or report. This message will only appear if the assertion fails; if it passes, the message will not be displayed.
Let’s take the assertEquals method as an example. We will print the message whenever the expected value doesn’t match the final value.
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test1() {
int expected = 10;
int actual = 5;
Assert.assertEquals(actual,expected,"Expected doesn't match with actual value..");
}
}
Output –
FAILED: test1
java.lang.AssertionError: Expected doesn't match with actual value.. expected [10] but found [5]
at org.testng.Assert.fail(Assert.java:99)
at org.testng.Assert.failNotEquals(Assert.java:1037)
Here, we can see that “Expected doesn’t match with actual value..” is printed in the console.
Video Tutorial
We hope that you have liked this article. If you have any doubts or concerns, please write us in the comments or mail us at admin@codekru.com