Assertions in unittest python with selenium

The assertion is nothing but the checkpoint or you can say it as verification for the test case to evaluate some item on the execution.

If we do not provide any assertion inside a test case then there is no way to know whether a test case is failed or not.

I know, you are not thinking about 'why cannot I use if-else and print like "this test is failed", because printing may help user which checking the log but how will you tell to you client that I have seen it was printing'.

Assertion helps in report generation, based on the assertions the test execution report will be generated.

If the condition provided in the assertion passes then assertion will not have any effect on the test case but the unittest framework fails the test case if an assertion fails

The TestCase class provides several assert methods to check for and report failures, We will see a few of the frequently used assertions.


There are few assertions which will accept all the values and few assertions will accept only numeric values, by example or by names itself you can differentiate them

import unittest
class Test(unittest.TestCase):

	def test_run1(self):
		#assertEqual(actual, expected, errorMessage)
		self.assertEqual(1, 1, "1 is not equal to 1")
	def test_run2(self):
		#assertEqual(actual, expected, errorMessage)
		self.assertEqual("h", "p", "h is not equal to p")

if __name__ == "__main__":
	unittest.main()

assertin-unittest-selenium-python

assertEqual

assertEqual compares the first parameter with the second parameter if both matches the unittest will continue with the remaining execution but both the values are different then the unittest fails the test case.

Apart from these two values, assertEqual method accepts the third parameter for the informational purpose, the third parameter is nothing but the reason we give why the condition failed.

The informational message will be useful while analyzing the execution results, an informational message is an optional parameter. Even if we donot pass any informational message the assertEqual method works fine.

import unittest
from selenium import webdriver
class Test(unittest.TestCase):
	def testName(self):
		driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
		driver.get("https://bing.com")
		titleOfWebPage = driver.title
		# verify title is bing
		self.assertEqual("Bing", titleOfWebPage, "webpage title is not matching")
if __name__ == "__main__":
	unittest.main()

assertNotEqual

assertNotEqual method compares the given two parameters, if both parameters are not same then unittest passes the test case but if both parameters are same then unittest fails the test case.

We can also pass the informational message as the third parameter and it is an optional parameter.

def testName(self):
	driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
	driver.get("https://bing.com")
	titleOfWebPage = driver.title
	# verify title is bing
	self.assertNotEqual("Google", titleOfWebPage, "webpage title is not matching")

assertTrue

when we have only two-parameter we can use assertEqual method to check whether both are same or not, but if we have more than two parameters, comparing values with assertEqual method become more difficult

assertTrue method checks whether a given parameter is true or not, if the value is true then the test is passed otherwise test is failed.

assertTrue provides the ability to the user for writing relational operators, we can use language-specific relational operators to compare values inside the assertTrue method. When we use relational operators, they will result in either True or False.

def testName(self):
	driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
	driver.get("https://bing.com")
	titleOfWebPage = driver.title
	# verify title is bing
	self.assertTrue((titleOfWebPage!="Google") & (titleOfWebPage!="ChercherTech"), "webpage title is not matching")

assertFalse

assertFalse method compares whether given value or expression results in false or not

If the result or value is False then unittest passes the test case but if the result or value is True then unittest fails the test case.

You should try to avoid this method when you are comparing only two values, as assertEqual and assertNotEqual provides better error information.

assertIs & assertIsNot

assertIs method verifies whether the first parameter is the same as the second parameter

assertIsNot verifies whether the first parameter is not the same as the second parameter.

def testName(self):
	driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
	driver.get("https://bing.com")
	titleOfWebPage = driver.title
	# verify title is bing
	self.assertIs("Bing", titleOfWebPage, "webpage title is not matching")

assertIsNone

assertIsNone method verifies whether give values or expression results in None or not, if the result is none then python unittest will pass the test case otherwise fails the test case.

def testName(self):
	driver = None
	# verify the object is none or not
	self.assertIsNone(driver, "driver has not been initiated")

assertIsNotNone

assertIsNotNone method verifies whether given values is not None, if the value is None then the test case will be failed

assertIn & assertNotIn

assertIn method verifies whether the first element is present in the send element if the first element is present in the second element then test s passed otherwise test is failed.

assertNotIn method verifies whether the first element is not present in the second element or not if the first element is present then the test will be failed otherwise test is passed.

These two methods will be helpful when you want to verify the presence of a value in a list, tuple, set, and dictionary.

def testName(self):
	sampleSet = set(["Perl", "Python", "Java"])
	# check python is present
	self.assertIn("Python", sampleSet, "item is not present in set")
	# check ruby is not present
	self.assertNotIn("Ruby", sampleSet, "item is present in set")

assertIsInstance & assertNotIsInstance

assertIsInstance method verifies whether the given object is an instance of a class or not, if the object is given class type then the test is passed otherwise the test will be failed by unittest

assertNotIsInstance method verifies whether a given object is a type of class or not, if the object is not the type of class then the test will be passed.

def test_verify_type_of_object(self):
	driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
	driver.get("https://bing.com")
	# verify the type of the object
	self.assertIsInstance(driver, WebDriver, "driver is not instance of WebDriver")

assertListEqual

assertListEqual method compares the given two lists and also print the error message based on the difference between the element if any element is mismatching.

assertTupleEqual

assertTupleEqual method compares the given two Tuples, if the tuple are not same then unittest will fail the test case and also displays the error message based on the mismatched elements

assertSetEqual

assertSetEqual method compares whether two sets are the same or not, If not, an error message is constructed that lists the differences between the sets.

assertDictEqual

assertDictEqual method compares two Dictionaries in python

assertDictContainsSubset

Tests whether the key/value pairs in dictionary actual are a superset of those in expected. If not, an error message listing the missing keys and mismatched values are generated.

Relational comparison :

  • assertGreater verifies whether first values if greater than second value or not.
  • assertGreaterEqual verifies whether the first parameter is greater or equal to the second parameter.
  • assertLess verifies whether the first parameter is lesser than the second parameter or not
  • assertLessEqual verifies whether the first parameter is lesser or equal to the second parameter
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions