In this post, we are going to learn how to make a test suite in selenium python. The unittest framework is derived from JUnit for compatible execution of selenium with Python and the famous unit testing framework to perform unit testing. We can write one or more test cases by inheriting TestCase class available in the UnitTest module, to add a Test case we need to supply a testcase(handler) method to the derived class.
The most common assert method used in all test cases
- assertEqual(): This method is used to check for the expected output of the test case
- assertTrue() : This method is used to verify a condition
- assertRaises(): This method is used to verify that exception gets raised.
- How to Install selenium on windows Python
- How to install selenium in VSCode
- How to open chrome browser in selenium Python
- How to open Edge browser in selenium Python
- How to open Firefox browser in selenium Python
- Executable path has been deprecated in selenium
TestCase Initialization & De-Initialization method
- setup(): The entry point or execution before every test case, does not expect any argument. The steps performed in the setup() method.
- loads the test cases
- Create browser instance Chrome, Firefox, Mozilla
- File Handling opening or closing of the file, reading or appending the result of execution.
- teardown(): On completion of the test case cleaning is performed in the teardown() method
1. How to make a test suite in selenium python
To write a Testcase by using the Pyunit framework we have to follow these simple steps that are common for all test cases.
- Define a class derived from unittest.TestCase()
- We have defined a class SearchData that is inheriting from TestCase class
- Define test cases with the name prefixed with the word test
- It is good practice to distinguish the test method prefix with the word test which helps the Test runner between the test and other methods.
- Execute the test cases/test suites by having unittest.main()
1.1 Define a class and initialization
In this example, First, we have to import
import module unittest and defining a class SearchData that is inheriting from TestCase class. The setup() method is used to create an instance of the Chrome browser and initialize some properties and navigate to the main page.
1.2 De-Initialization method
Once the unit test execution is complete, We need to clean up values initialize at beginning of test cases using the setup() method. for this TestCase class provides method teardown() that the Test runner call after test execution is complete. In this example, once test execution ends, we no longer need the instance of Chrome. We have closed it in the tearDown() method.
1.3 Run from the command line
To run a test case from the command line we need to add to call the main method in our code. The code is used to run tests from the command line. After following all the steps above save the script file FirstTest.py and run this command from the command line “python FirstTest.py“
if __name__ == "__main__": unittest.main()
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import unittest class SearchData(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(ChromeDriverManager().install()) def test_SearchElemnt(self): self.driver.get("https://www.python.org") self.assertIn("Python", self.driver.title) self.SearEle = self.driver.find_element(By.NAME, "q") self.SearEle.clear() self.SearEle.send_keys("getting started with python") self.SearEle.send_keys(Keys.RETURN) print(self.driver.current_url) assert "The element is not find" not in self.driver.page_source def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()
---------------------------------------------------------------------- Ran 1 test in 8.239s OK