The assertion is the process of verifying the actual state of the application with the ideal state of the application to find bugs. 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.
Why cannot I use if-else and print like "this test is failed"? because printing may help the user who is checking the log but will you tell your 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 tests 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 that 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()

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 do not 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 method compares the given two parameters, if both parameters are not the same then unittest passes the test case but if both parameters are the 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")
when we have only two-parameter we can use assertEqual method to check whether both are the 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 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.
def testName(self):
driver = webdriver.Chrome(executable_path=r'D:/PATH/chromedriver.exe')
driver.get("https://bing.com")
titleOfWebPage = driver.title
# verify title is bing
self.assertIs("Bing", titleOfWebPage, "webpage title is not matching")
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 method verifies whether the given values is not None, if the value is None then the test case will be failed
assertIn method verifies whether the first element is present in the send element if the first element is present in the second element then the test is 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 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 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 method compares the given two Tuples, if the tuple is not same then unittest will fail the test case and also displays the error message based on the mismatched elements
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 method compares two Dictionaries in python
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 is generated.
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.