What are assertions in Selenium with python?


There are assertions in Selenium which are verification or checkpoints for the test case. In the absence of an assertion, there is no option of determining if a test case has failed or not.

Sometimes, we may use the conditional statements like if – else and so on and then print the result of pass/ fail in the console. But that can only solve the problem of checking logs and not for actual reporting.

Thus assertion is used for generating test execution reports. In case, our test case passes all the test steps, the assertions do not impact the test cases in any way, however if the test case fails, it is reported.

A test case can contain numerous methods of assertions. Some of them can accept values of all data types and some will only have numeric values. The different types of assertions are listed below −

  • assertEqual – This assertion has two parameters. A comparison is done between the first and second parameter. In case both of them match, the test case is considered a pass; else the test case is failed.

The third parameter of assertEqual is an optional one. It is mostly used for informational purposes for result analysis.

Syntax

assertEqual("Tutorialspoint", "Tutorialspoint")
  • assertNotEqual – This assertion can have more than two parameters. A comparison is done between the first and second parameter. In case both of them do not match, the test case is considered a pass; else the test case is failed.

The third parameter of assertEqual is an optional one. It is mostly used for informational purposes for result analysis.

Syntax

assertNotEqual("Tutorials", "Tutorialspoint")
  • assertTrue – This assertion can handle more than two parameters. A comparison is done between the first and second parameter. In case both of them do match, the test case is considered a pass; else the test case is failed.

The assertTrue assertion allows the use of relational operators for comparison. In these situations, the result is in Boolean (True or False). The final parameter of this assertion can contain informational messages used for result analysis.

Syntax

assertTrue((hd="Tutorialspoint") OR (hd="Selenium"),"Matching header")
  • assertFalse – This assertion can handle more than two parameters. A comparison is done between the first and second parameter. In case both of them do not match, the test case is considered a pass; else the test case is failed.

The assertTrue assertion allows the use of relational operators for comparison. In these situations, the result is in Boolean (True or False). The final parameter of this assertion can contain informational messages used for result analysis.

Syntax

assertFalse(2>3,"2 is not greater than 3")
  • assertIs – This assertion can handle two parameters. A comparison is done between the first and second parameter. In case both of them match, the test case is considered a pass; else the test case is failed.

The third parameter of assertEqual is an optional one. It is mostly used for informational purposes for result analysis.

Syntax

assertIs(Tutorialspoint", "Tutorialspoint", "Both are equal")
  • assertIsNot – This assertion can handle two parameters. A comparison is done between the first and second parameter. In case both of them do not match, the test case is considered a pass; else the test case is failed. The final parameter of this assertion can contain informational messages used for result analysis.

The final parameter of this assertion can contain informational messages used for result analysis.

Syntax

assertIsNot(Tutorialspoint", "Tutorials", "Both are not equal")
  • assertIsNone – This assertion can handle a parameter. It is used to check if the value provided is none. If the result is equal to none, the test case is considered a pass; else the test case is failed.

The final parameter of this assertion can contain informational messages used for result analysis.

Syntax

assertIsNone( result, "The result is equal to none")
  • assertIsNotNone – This assertion can handle a parameter. It is used to check if the value provided is none. If the result is not equal to none, the test case is considered a pass; else the test case is failed.

The final parameter of this assertion can contain informational messages used for result analysis.

Syntax

assertIsNotNone( r, "The result is not equal to none")
  • assertIn – This assertion has two parameters. It is used to check if the first parameter exists in the second parameter. If the item is present in the second element, the test case is considered a pass; else the test case is failed. The third parameter of assertIs is an optional one. It is mostly used for informational purposes for result analysis.

This type of assertion is mostly used in set, list, tuple and dictionary.

Syntax

s = set(["PLSQL", "Selenium", "Jmeter"])
assertIn("Selenium", s, " Selenium is present in set s")
  • assertNotIn – This assertion has two parameters. It is used to check if the first parameter exists in the second parameter. If the item is not present in the second element, the test case is considered a pass; else the test case is failed.

The third parameter of assertIs is an optional one. It is mostly used for informational purposes for result analysis.

This type of assertion is mostly used in set, list, tuple and dictionary.

Syntax

s = set(["PLSQL", "Selenium", "Jmeter"])
assertIn("Oracle", s, " Oracle is not present in set s")
  • assertIsInstance – This assertion has two parameters. It is used to check if the given object( in the first parameter) is an instance of the class( in the second parameter). If yes, the test case is considered a pass; else the test case is failed.

The third parameter of assertIs is an optional one. It is mostly used for informational purposes for result analysis.

Syntax

Cl1 c = new Cl1()
assertIsInstance(c, Cl1, " c is an instance of Cl1")
  • assertIsNotInstance – This assertion has two parameters. It is used to check if the given object( in the first parameter) is an instance of the class( in the second parameter). If no, the test case is considered a pass; else the test case is failed.

The third parameter of assertIs is an optional one. It is mostly used for informational purposes for result analysis.

Syntax

Cl1 c = new Cl1()
assertIsInstance(d, Cl1, " d is not an instance of Cl1")
  • assertListEqual – This assertion has two parameters. It is used to check if the two lists mentioned in the parameter are similar or not. If there is any missing or not similar element, it is printed as an error message.

  • assertTupleEqual – This assertion has two parameters. It is used to check if the two tuples mentioned in the parameter are similar or not. If there is any missing or not similar element, it is printed as an error message.

  • assertSetEqual – This assertion has two parameters. It is used to check if the two sets mentioned in the parameter are similar or not. If there is any missing or not similar element, it is printed as an error message.

  • assertDictEqual – This assertion has two parameters. It is used to check if the two dictionaries mentioned in the parameter are similar or not. If there is any missing or not similar element, it is printed as an error message.

Updated on: 29-Jul-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements