Assertion and Verification in TestNG

August 19, 2016

A test is considered successful (passed) if it is completed without throwing an exception or if it threw an exception that was expected.

Test methods are typically be made of calls that can throw an exception, or of various assertions (using the Java “assert” keyword).  An “assert” failing will trigger an AssertionErrorException, which in turn will mark the method has failed.

Assertions are used to compare actual result with the 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 Automation Tools Trainings and Tutorials | QA Tech Hub”); – It compares title of the page which we will receive from getTitle() method and the expected one. T

his 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 values. If both the values are equal then test case will be passed else it will mark the test case as fail.

This method is overloaded to pass various types of parameters – String, int, boolean, char, byte, short, long etc.

Assert.assertNotEquals(actual1, actual2); – This can be used as a negative assertion. If both the values are not equal the test case will be passed else it will mark the test case as fail.

This method is overloaded to pass various types of parameters – String, int, boolean, char, byte, short, long etc.

Assert.assertTrue(condition); – Using this assertion method test case will pass a when a boolean condition passed as a parameter returns a true.

Assert.assertFalse(condition); – Using this assertion method test case will pass a when a boolean condition passed as a parameter returns a false.

Assert.assertNull(object); – Using this assertion method test case will pass when the value of object does not equal null then test case is considered as passed.

Assert.assertNotNull(object); – Using this assertion method test case will pass when the value of object does not equal 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 Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra:

Leave a Comment: