Python+Selenium(2)-unittest unit testing framework

        unittest is a unit testing framework, a unit testing framework for Python programming. Sometimes called "PyUnit", it is the Python language version of Junit. Learn here, Junit is a unit testing framework for the Java language. Java also has a very useful unit testing framework called TestNG. This series only learns Python, so you only need unittest to be a unit testing framework in Python.
       Unittest supports test automation, sharing initialization and closing exit codes in test cases. The smallest unit in unittest is test, which is a test case. To understand the unittest unit testing framework, first understand the following important concepts.
Test fixture (test fixture)
      A test fixture consists of two parts, the preparation part before executing the test code and the cleanup code after the test ends. These two parts are generally represented by the functions setUp() and tearDown(). Here is an example. For example, to test the scenario of Baidu search selenium, our test firmware can be written like this. In setUp(), write script code such as opening the browser, maximizing the browser, and opening the Baidu homepage; write the end in tearDown(). Code to quit and close your browser after searching.
Test case (test case)
       The smallest unit managed in unittest is a test case, a test case, including the test firmware, and the function or method of the specific test business. In a test case, the test firmware may not be written, but there is at least one function that starts with test. unittest will automatically recognize that the function that starts with test is the test code. If the function you write does not start with test, unittest will not execute the script in this function. Remember, all test functions must start with test, remember Live is in lowercase.
test  suite
       Very simply, it is a collection of many test cases, called a test suite, and a test suite can manage multiple test cases at will. If the test case is compared to a single student, the test suite is like a class concept.
Test  runner (test runner)
       A test runner is a component used to execute load test cases, execute the test cases, and provide test output. The test runner can load test cases or test suites to perform test tasks.

Let's take an example to practice the use of test fixture and test case, and learn the simple usage of unittest:
1. Create a new testbaidu.py file
2. Import the unittest module
3. The current test class inherits unittest.TestCase, which is equivalent to the current use of unittest to create A test case is created, which can be directly identified by unittest.
4. Write setUP(), mainly to open the browser and open the site
5. Write a test_search() use case to write the search code
6. Write tearDown(), mainly to exit the browser

The relevant script code is as follows:

[python]  view plain copy  
  1. # coding=utf-8  
  2. import time  
  3. import unittest  
  4. from selenium import webdriver  
  5.   
  6.   
  7. class BaiduSearch(unittest.TestCase):  
  8.   
  9.     def setUp(self):  
  10.         """ 
  11.         测试固件的setUp()的代码,主要是测试的前提准备工作 
  12.         :return: 
  13.         """  
  14.         self.driver = webdriver.Chrome()  
  15.         self.driver.maximize_window()  
  16.         self.driver.implicitly_wait(8)  
  17.         self.driver.get("https://www.baidu.com")  
  18.   
  19.     def tearDown(self):  
  20.         """ 
  21.         测试结束后的操作,这里基本上都是关闭浏览器 
  22.         :return: 
  23.         """  
  24.         self.driver.quit()  
  25.   
  26.     def test_baidu_search(self):  
  27.         """ 
  28.         这里一定要test开头,把测试逻辑代码封装到一个test开头的方法里。 
  29.         :return: 
  30.         """  
  31.         self.driver.find_element_by_id('kw').send_keys('selenium')  
  32.         time.sleep(1)  
  33.         try:  
  34.             assert 'selenium' in self.driver.title  
  35.             print ('Test Pass.')  
  36.         except Exception as e:  
  37.             print ('Test Fail.', format(e))  
  38.   
  39. if __name__ == '__main__':  
  40.     unittest.main()  
解释:

        最后结尾处的unittest.main(),添加这个是支持在cmd,里面,cd到这个脚本文件所在的目录,然后python 脚本名.py执行,如果不添加这一段,是无法执行cmd里面运行脚本的,在PyCharm中,不添加最后一段,也可以通过,右键 Run "unittest xxx",来达到执行效果。

如需更多了解和交流,请加QQ群:49044146

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326009487&siteId=291194637