Transcripted Summary

I have a question for you. Up to this point, what have we tested? We have automated opening Chrome and the application, signing into the application, searching for a user, signing out, closing chrome, and closing the application. We have not verified our test script and are not sure if it truly passed or failed. While the console shows that the tests have passed,that's only because there was no error in our automation code. If I were to remove all code in the searchUser test method, then run it, this would still show as passing. That’s not right.

What Are TestNG Assertions

TestNG assertions verify if our test truly passed or failed. It’s a line of code that is placed in our test method to verify a condition.

TestNG Assertion Methods

There are many assertions for TestNG but most of them are overloaded versions of the following methods:

  • assertTrue - verifies a condition is true
  • assertFalse - verifies a condition is false
  • assertSame - verifies that two objects refer to the same object
  • assertNotSame - verifies that two objects do not refer to the same object
  • assertNotNull - verifies that an object is not null
  • assertEquals - verifies that two objects are equal

Generally, all of these TestNG assertions have the same three parameters: actual result, expected result, and a String. It’s the same with JUnit assertions which have an assertion class located in TestNG’s distribution.

JUnit & TestNG Assertions

JUnit has a class, junit.framework.Assert, that has similar overloaded methods. TestNG turned around and added the same JUnit class, org.testng.AssertJUnit, to its distribution. TestNG added the same class as JUnit to guarantee all assertions keep working if we migrate our test from JUnit to TestNG. TestNG also added another class called org.testng.Assert.

The main difference between JUnit’s class and TestNG’s class is the syntax. Their parameters are available in reverse order. For example, the assertEquals method for JUnit has a String as the first parameter followed by an expected result then an actual result. The same method for TestNG has actual result as the first parameter, expected result as the second parameter then String as the last parameter.

//JUnit Syntax
assertEquals("", expected, actual);

//TestNG Syntax
assertEquals(actual, expected, "");

Let’s go to Eclipse and I’ll show you the assertions. Go to the TestNG Library, maximize the TestNG jar file, select org.testng package, and we see both classes: Assert and AssertJUnit class.


Assert and AssertJUnit classes

Maximize Assert and there’s a lot of methods. Maximize AssertJUnit and we some of the same overloaded methods (you can see both of these classes in the Resources section below). The methods within our Assert class are considered hard asserts. In the next section, we will cover hard asserts.



Resources



Quiz:

Note: Chapter 6 has been divided into multiple sub-chapters. You'll find the Quiz on the last sub-chapter, Chapter 6.3



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR