We talked about a series of selenium articles earlier. You can read them carefully and I believe you will gain some results. If you have any questions, welcome to me_ an_ an.
brief introduction
unittest is a Python unit test framework, similar to the JUnit framework
What is unit testing?
Unit testing refers to the work of checking and verifying the smallest testable unit in software in isolation from other parts of the program. The smallest testable unit here usually refers to functions or classes, which are generally done by development. It is divided according to the testing stages, namely unit testing, integration testing, system testing and acceptance testing.
Why do unit tests?
1. Flexible organization ui automation / interface test cases
2. Make use cases execute efficiently
3. Assertion: it is convenient to verify the results of test cases
4. Integrated html test report
Test case methods in unittest start with test, otherwise they will not be recognized by unittest. The execution order is based on (0-9, A-Z,a-z), such as test01,testaa.
give an example
1. Import unittest module
2. Create a test class and inherit unittest.TestCase()
3. Define the test method. The method name must be test_ start
4. Call unittest.main() method to run test cases. unittest.main() method will search all test case methods starting with test under the module and execute them automatically
5. Identification of the execution result of each use case, OK OR FALSE
from selenium import webdriver def login(): fox = webdriver.Firefox() fox.implicitly_wait(5) fox.get('https://baidu.com') if_rame = fox.find_elements_by_tag_name('iframe')[0] fox.switch_to.frame(if_rame) fox.find_element_by_id('switcher_plogin').click() fox.find_element_by_id('u').send_keys('123456') fox.find_element_by_id('p').send_keys('123456') fox.find_element_by_id('login_button').click()
This is an example of QQ space. The above is written in the user-defined function. Let's write it into unittest. The above example needs to change login.py slightly:
from selenium import webdriver import unittest class LoginTest(unittest.TestCase): def test_login(): fox = webdriver.Firefox() fox.implicitly_wait(5) fox.get('https://qzone.qq.com/') if_rame = fox.find_elements_by_tag_name('iframe')[0] fox.switch_to.frame(if_rame) fox.find_element_by_id('switcher_plogin').click() fox.find_element_by_id('u').send_keys("Qing'an") fox.find_element_by_id('p').send_keys("Please be safe") fox.find_element_by_id('login_button').click() if __name__=='__main__': unittest.main()
Let's take a look at the pre and post of unittest.
from Login import T_test import unittest from selenium import webdriver class LoginTest(unittest.TestCase,T_test): def setUp(self) -> None: # After testing a use case, close the browser once, and then continue with the second use case self.fox = webdriver.Firefox() self.fox.implicitly_wait(5) print("Use case execution start") print("Each use case needs to be executed once") def tearDown(self) -> None: self.fox.quit() print("End of case execution") @classmethod def setUpClass(cls): # Global execution once print("This is all test Preconditions for"+'\n') @classmethod def tearDownClass(cls): # Global execution once print("This is all test Post condition of"+'\n') # test data def test_login_01(self): fox = webdriver.Firefox() fox.implicitly_wait(5) fox.get('https://qzone.qq.com/') if_rame = fox.find_elements_by_tag_name('iframe')[0] fox.switch_to.frame(if_rame) fox.find_element_by_id('switcher_plogin').click() fox.find_element_by_id('u').send_keys("Qing'an") fox.find_element_by_id('p').send_keys("Please be safe") fox.find_element_by_id('login_button').click() def test_login_02(self): def test_login_01(): fox = webdriver.Firefox() fox.implicitly_wait(5) fox.get('https://qzone.qq.com/') if_rame = fox.find_elements_by_tag_name('iframe')[0] fox.switch_to.frame(if_rame) fox.find_element_by_id('switcher_plogin').click() fox.find_element_by_id('u').send_keys("Qing'an") fox.find_element_by_id('p').send_keys("Please be safe") fox.find_element_by_id('login_button').click() if __name__=='__main__': unittest.main()
The front and rear positions here are just one. Choose different front and rear positions in different situations. The above two types are pre and post. They need not be added every time unittest is written.
Test set
1. Create test set: suite=unittest.TestSuite()
2. Add the specified test case in the test set:
Method 1: add a single test case method
Addtest (class name (method name)):
Method 2: add multiple test case methods, and there is a list of method names
addTest([class name (method name 1), class name (method name 2)...]):
Method 3: add all test cases
unittest.TestLoader()
Loadtestsfromtestcase (test class name): add a test class
Loadtestsfrommodule (module name): add a module
3. Execute test set
1. Create an actuator: runner= unittest.TextTestRunner() or runner=HTMLTestRunner
2. Execute the test set: runner.run(suite)
Let's look at the first one first: we won't paste all the code here. The code is the same as the above, but it's changed here and written in unittest_login.py:
if __name__=='__main__': # Create a test suite suite = unittest.TestSuite() # Write the test cases to be executed case1 = LoginTest('test01') # Add to component suite.addTest(case1) # Execute test cases running = unittest.TextTestRunner() running.run(suite)
Let's look at the second:
if __name__=='__main__': # Create a test suite suite = unittest.TestSuite() # Add all test methods under LoginTest loa = unittest.TestLoader() suite.addTest(loa.loadTestsFromTestCase(LoginTest)) # Execute test cases running = unittest.TextTestRunner() running.run(suite)
Assertion of unittest
There are many kinds of assertions, which will not be introduced one by one here. If you can move out and introduce them later, here is an example. We should learn to draw inferences from one instance:
Take assertEqual as an example. If the judgment is a==b, what can we do? Judge whether the title is accurate or whether the value of an element on the jump page has the value you want:
For example, test01 in the above code:
def test01(self): self.login('https://qzone.qq.com/','123456','123465') self.ele = self.fox.find_element_by_id('title_2').text self.assertEqual(self.ele, 'Account password login')
Or get the title of the web page, make an assertion, and customize the assertion information:
def test01(self): self.login('https://qzone.qq.com/','123456','123465') self.ele = self.fox.title self.assertEqual(self.ele, 'QQ space-Share life and keep a sense of',msg='Non conformity')
I judge whether the text values of the elements are equal. It can be seen that I typed the wrong text
Generate HTML test report
Finally, let's generate a report. Although there are few data, durability is good:
Parameters:
1. The report verbosity parameter can control the output of execution results. 0 is a simple report, 1 is a general report, and 2 is a detailed report 2. The report stream parameter can output the report to a file: you can use HTMLTestRunner to output html reports
if __name__=='__main__': # Create a test suite suite = unittest.TestSuite() # Add all test methods under LoginTest loa = unittest.TestLoader() suite.addTest(loa.loadTestsFromTestCase(LoginTest)) # Execute test cases running = unittest.TextTestRunner(verbosity=2) running.run(suite)
To output an HTML report, you need to import a package:
1. Guide Package: from HTMLTestRunner import HTMLTestRunner
2. Create an actuator: runner=HTMLTestRunner(stream,tester,description,title)
Parameter: stream -- report file; Tester -- tester, description - report description information, title - report title
import HTMLTestRunner import unittest from uni_test import LoginTest if __name__=='__main__': # Create a test suite suite = unittest.TestSuite() # Add all test methods under LoginTest loa = unittest.TestLoader() suite.addTest(loa.loadTestsFromTestCase(LoginTest)) # Execute test cases running = HTMLTestRunner.HTMLTestRunner(stream=open('123.html', 'wb'), tester='Qinghuan has nothing else to do', description='Simple report', title='test test') running.run(suite)
Note here that this part of the code needs to be written separately, that is, a use needs to be created_ Unittest.py file is used to perform the previous operations and cannot be written under the test case. Otherwise, the report cannot be output. As far as bloggers know, unittest can run even without writing this part of code. python only executes the content in unittest, regardless of this part.