The purpose of learning unittest is to use it to write web automation test cases, so we will combine unittest with Selenium to write a web automation test example.
Let's take Baidu search as an example and create the file test_baidu.py
# _*_ coding:utf-8 _*_ """ name:zhangxingzai date:2023/2/25 """ import unittest from time import sleep from selenium import webdriver from import By class TestBaidu(): def setUp(self): = () self.baidu_url = '' def test_search_key_unittest(self): (self.baidu_url) .find_element(, 'kw').send_keys('unittest') .find_element(, 'su').click() sleep(2) title = (title, 'unittest_baidu search') def test_search_key_selenium(self): (self.baidu_url) .find_element(, 'kw').send_keys('selenium') .find_element(, 'su').click() sleep(2) title = (title, 'selenium_baidu_search') def tearDown(self): () if __name__ == '__main__':
Without going into too much detail about the code above, it's all about creating test classes and methods with unittest. The code in the methods is Selenium scripts. However, there are some problems with the code here, let's improve them one by one.
First of all, observing the code you can see that the steps in the two test cases are the same, the only difference is that the keywords searched and the results asserted are different. After learning python modularization, so here the operation steps are encapsulated into a method.
The code is as follows:
# _*_ coding:utf-8 _*_ """ name:zhangxingzai date:2023/2/25 """ import unittest from time import sleep from selenium import webdriver from import By class TestBaidu(): def setUp(self): = () self.baidu_url = '' # Encapsulate Baidu's home page access and search process into a baidu_search() method def baidu_search(self, search_key): (self.baidu_url) .find_element(, 'kw').send_keys(search_key) .find_element(, 'su').click() sleep(2) def test_search_key_unittest(self): search_key = 'unittest' self.baidu_search(search_key) (, search_key + 'Baidu Search') def test_search_key_selenium(self): search_key = "selenium" self.baidu_search(search_key) (, search_key + "Baidu Search") def tearDown(self): () if __name__ == '__main__':
Here we encapsulate the process of accessing and searching the Baidu homepage into a baidu_search() method, and define the search_key parameter as the search key, and perform different content searches based on the received keywords. The baidu_search() method will not be executed as a test case, because according to unittest's rules for finding and executing test cases, it will only treat methods starting with "test" as test cases.
Another question worth discussing is whether the assertions of the test cases should be written in the wrapped methods? As you can see from the previous code, the assertion points for the tests are the same. Here it is preferred to write the assertions inside each test case. This is because very often even if the steps are the same, the assertion points are not exactly the same. From a design point of view, it is also clearer to write the assertions in each test case.
In addition, we also found that each test case has to start and close the browser once, which is very time-consuming, so how to reduce the number of times the browser is started and closed? This can be solved by utilizing the setUpClass/tearDownClass learned earlier.
# _*_ coding:utf-8 _*_ """ name:zhangxingzai date:2023/2/25 """ import unittest from time import sleep from selenium import webdriver from import By class TestBaidu(): @classmethod def setUpClass(cls): = () cls.baidu_url = '' def baidu_search(self, search_key): (self.baidu_url) .find_element(, 'kw').send_keys(search_key) .find_element(, 'su').click() sleep(2) def test_search_key_unittest(self): search_key = 'unittest' self.baidu_search(search_key) (, search_key + '_Baidu Search') def test_search_key_selenium(self): search_key = 'selenium' self.baidu_search(search_key) (, search_key + '_Baidu Search') @classmethod def tearDownClass(cls): () if __name__ == '__main__':
Pre-modification:
Modified:
You can see that the modification saves as much as 16 seconds. Here, although we define the driver driver as , it is still used in each test case as .
() is called to close the browser when all the test cases in the entire test class have been run.
When there are multiple test cases in a test class, this approach will greatly reduce the execution time of the test cases.
This way we get a simple automated test script for Baidu search, the reader can try other scenarios based on this example.
to this article on selenium unittest web automation sample code is introduced to this article, more related selenium unittest web automation content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!