TestNG Assertions
TestNG Assertions will discuss about the Assertions available in the testNG in detail. When you write a test script there should be a verification point in order to mark that test script is passed or failed or any other state. To achieve this in testNG we will user Assertions. Assertion helps you to check or verify the success of conditions in your test. If the conditions don’t satisfy, it will stop the
test execution of the said test and mark it as failing.
TestNG supports assertion of a test using the Assert class which is part of the testNG library. The following table describes few of the methods and their usage that are available with the Assert class in testng:
Frequently Used Assertions:
Assertion | Description |
---|---|
Assert.assertTrue(condition, message) | It will check the condition is true or not. If the condition is false then it fails and prints the message. Here the message is optional. |
Assert.assertFalse(condition, message) | It will check the condition is false or not.If the condition is true then it fails and prints the message. Here the message is optional. |
Assert.assertNotNull(object, message) | It will check the object is NOT Null or not. If the object is null then it fails and prints the message. Here the message is optional. |
Assert.assertNull(object, message) | It will check the object is Null or not. If the object is NOT Null then it fails and prints the message. Here the message is optional. |
Assert.assertEquals(actual, expected,message) | It will check the actual and expected or Equal or not. If both are NOT Equal then it fails and prints the message. Here the message is optional. |
Assert.assertNotEquals(actual, expected,message) | It will check the actual and expected or NOT Equal or not. If both are Equal then it fails and prints the message. Here the message is optional. |
In the above table we have seen some of the assertions which are very frequently used. If the condition meets as expected then the assert passed otherwise it will fails the test and stops the execution of that test.
Below is the sample program where all the assertions will PASS:
import org.testng.Assert; import org.testng.annotations.Test; public class PassedAssertions { @Test public void testOne() { Assert.assertTrue(true, "Assert Failed as the actual condition is False"); } @Test public void testTwo() { Assert.assertFalse(false, "Assert Failed as the actual condition is True"); } @Test public void testThree() { String s = "Krishna"; Assert.assertNotNull(s,"Assert Failed as the String is Null"); } @Test public void testFour() { String s = null; Assert.assertNull(s, "Assert Failed as the String is NOT NULL"); } @Test public void testFive() { String s1 = "Krishna"; String s2 = "Krishna"; Assert.assertEquals(s1, s2, "Assert Failed as the two string are NOT EQUAL"); } @Test public void testSix() { String s1 = "Krishna"; String s2 = "Sakinala"; Assert.assertNotEquals(s1, s2, "Assert Failed as the two string are EQUAL"); } }
Below is the output for the above Program:
Below is the sample program where all the assertions will FAIL:
import org.testng.Assert; import org.testng.annotations.Test; public class FailedAssertions { @Test public void testOne() { Assert.assertTrue(false, "Assert Failed as the actual condition is False"); } @Test public void testTwo() { Assert.assertFalse(true, "Assert Failed as the actual condition is True"); } @Test public void testThree() { String s = null; Assert.assertNotNull(s,"Assert Failed as the String is Null"); } @Test public void testFour() { String s = "Krishna"; Assert.assertNull(s, "Assert Failed as the String is NOT NULL"); } @Test public void testFive() { String s1 = "Krishna"; String s2 = "Sakinala"; Assert.assertEquals(s1, s2, "Assert Failed as the two string are NOT EQUAL"); } @Test public void testSix() { String s1 = "Krishna"; String s2 = "Krishna"; Assert.assertNotEquals(s1, s2, "Assert Failed as the two string are EQUAL"); } }
Below is the output for the above Program:
This way we will use the Assertions in testNG to know the status of the tests which are executed.
Please watch the you tube video for better understanding.