Assertion and Verification in TestNG

August 19, 2016

Assertions are used to compare actual result with expected result. We can use this feature to give a verdict whether a test case is passed or failed. Say for Example after successful loading of a page we want to compare the title of the page with expected one.

In above code, Assert.assertEquals(Driver.getTitle(), “Qa Tech Hub | QATechHub”); – It compares Title of the page which we received from WebDriver and the expected one. This will pass or fail a test case depending upon whether Title matches with expected one or not.

Assert class is used to provide assertions in TestNG. Below is the list of assertions provided by TestNG:

Assert.assertEquals(actual, expected); – To compare two same type of Data. They can be two Strings or two integers etc.

Assert.assertFalse(condition); – This can be used as a negative assertion. This will pass a test case when a boolean condition in parameter returns a false.

Assert.assertTrue(condition); – This can be used as a positive assertion. This will pass a test case when a boolean condition is parameter returns a true.

Assert.assertNotEquals(actual1, actual2); – This can be used as a negative assertion. This will compare two same type of data, that can be String, Integer, etc.

Assert.assertNotNull(object); – This can be used as a negative assertion. If the parameter passed return a null then test case is considered as passed.

Assert.assertSame(actual, expected); – This assertion is used to compare two objects whether they are same or not. It will be considered as passed if they are same else will be failed.

Assert.assertNotSame(actual, expected); – This assertion is used to compare two objects whether they are same or not. It is used as a negative assertion. It will be considered as passed if they are same else will be failed.

So, this is a list of assertions which you can use to give a verdict to a test case. Use them wisely to achieve a good testing result.

For any Questions, Queries or comments feel free to write us at saurabh@qatechhub.com or support@qatechhub.com. Happy Learning 🙂

Saurabh

About the Author

Saurabh

Follow Saurabh:

Leave a Comment: