Welcome to BhauAutomation!
Have you ever written an automation script but wondered whether the test actually passed or failed? This is where TestNG Assertions help.
What Are TestNG Assertions?
Assertions are used to check whether the actual result matches the expected result.
For example, when testing a login page, you want to verify whether the correct welcome message appears after login.
Common TestNG Assertions
assertEquals()– Checks whether two values are equal.assertTrue()– Checks whether a condition is true.assertFalse()– Checks whether a condition is false.assertNull()– Checks whether a value is null.assertNotNull()– Checks whether a value is not null.assertSame()– Checks whether two references point to the same object.
Practical Java Example
Let’s check whether a website displays the correct page title.
import org.testng.Assert;
import org.testng.annotations.Test;
public class AssertionTest {
@Test
public void verifyTitle() {
String actualTitle = "BhauAutomation";
String expectedTitle = "BhauAutomation";
Assert.assertEquals(actualTitle, expectedTitle);
System.out.println("Test Passed!");
}
}
Expected Output
Test Passed!
Simple Explanation
actualTitle– The result we received.expectedTitle– The result we expected.assertEquals()– Compares both values.- If both values match, the test passes.
- If they are different, the test fails.
Why Are Assertions Important?
- Verify actual test results.
- Identify application failures.
- Make automation tests more reliable.
- Avoid false test passes.
Conclusion
TestNG Assertions help you understand whether your automation test is actually successful. Always verify the expected result instead of simply running the test.
Keep Learning with BhauAutomation!