当前位置:网站首页>Automated testing - unittest framework
Automated testing - unittest framework
2022-07-27 21:05:00 【Pinellia and cool】
Catalog
test runner( The test execution )
The execution order of use cases
Exception capture and error screenshot
Framework analysis
unittest yes python The unit test framework of , It mainly has the following functions :
- Provides use case organization and execution
- Provide rich comparison methods
- Provide rich logs
unittest There are four important concepts ,test fixture,test case,test suite,test runner.
test fixture( Test firmware )
Build and destroy a test case environment , It's just one. fixture, By covering setUp() and tearDown() Method to implement .
- setUp(): Preparation of test environment and data
- tearDown(): Clean up after the test case execution
test case( The test case )
A test case is a complete test process , Including the construction of preparation environment before testing (setUp)、 The code to implement the test process , And the restoration of the environment after testing (tearDown). A use case is a method , The name to test_ start .
test suite( test suite )
Verification of a function often requires multiple test cases , Multiple test cases can be assembled and executed together , This produces the test suite TestSuite The concept of . Must be Inherit ( object-oriented ) How to use .
test runner( The test execution )
Test execution is also a very important concept , stay unittest In the frame , adopt TextTestRunner Class provides the run() Method to execute test suite/test case.
Batch script execution
Build test suite
Suppose we have written test1.py And test2.py Two documents , So how can we execute these two files at the same time ?
addTest()
TestSuite Class addTest() Methods can assemble test methods from different test classes into test suites , however addTest() Only one test method in one class can be assembled into the test suite at a time .
import unittest
from src0716 import test1
from src0716 import test2
def createsuite():
#addTest
suite = unittest.TestSuite()
suite.addTest(test1.Baidu1("test_hao")) #addTest( Script name . Class name (" Method name "))
suite.addTest(test1.Baidu1("test_hbaidu"))
suite.addTest(test2.Baidu2("test_hao"))
suite.addTest(test2.Baidu2("test_baidusearch"))
return suite
if __name__=="__main__":
suite = createsuite()
runner = unittest.TextTestRunner(verbosity=2) #verbosity 0,1,2 The larger the number, the more detailed the background print
runner.run(suite)But there are two inconveniences in the above practice :
- You need to import all related py file , such as import test1, Each new script needs to be imported .
- addTest Only one test method can be added at a time , If one py In file 10 Test methods , If they are to be assembled into the test suite , It needs to be increased 10 Time .
makeSuit()
stay unittest The framework provides makeSuite() Methods ,makeSuite It can realize all the tests in the test case class case Test suite composed of TestSuite ,unittest call makeSuite When , Just pass in the test class name .
suite.addTest(unittest.makeSuite(test1.Baidu1))
suite.addTest(unittest.makeSuite(test2.Baidu2))TestLoader()
TestLoader Test suite for creating classes and modules , In general , use TestLoader().loadTestsFromTestCase(TestClass) To load the test class .
suite1 = unittest.TestLoader().loadTestsFromTestCase(test1.Baidu1)
suite2 = unittest.TestLoader().loadTestsFromTestCase(test2.Baidu2)
suite = unittest.TestSuite([suite1, suite2])however makeTest() and TestLoader() You still need to import a related file every time you add a script .
discover()
discover Is recursively to its subdirectory, starting from the specified directory , Find all test modules and return an object containing them TestSuite , Then load the only test file matching the pattern , That is to execute all test cases of test scripts in a folder ,discover The parameters are
unittest.defaultTestLoder.discover("../src",pattern="test*.py",top_level_dir=None) # Folder The file where the test case is located
The execution order of use cases
unittest By default, the order in which the framework loads test cases is based on ASCII The order of the codes , The order of numbers and letters is :0~9,A~Z,a~z .addTest() It is executed in the order of addition .
Ignore use case execution
Tag test cases that you don't want to run :@unittest.skip("skiping")
@unittest.skip("skipping")
def test_baidusearch(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").click()
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(u" test ")
driver.find_element_by_id("su").click()
driver.find_element_by_id("su").click()unittest Assertion
Assertion : Judge whether the actual results are consistent with the expected results
Common assertions :
| self.assertEqual(arg1,arg2,msg=" XXX") | Prediction expression arg1 And arg2 equal , Otherwise output XXX |
| self.assertNotEqual(arg1,arg2,msg="XXX") | Prediction expression arg1 And arg2 It's not equal , Otherwise output XXX |
| self.assertTrue(arg1,msg="XXX") | Prediction expression arg1 It's true , Otherwise output XXX |
| self.assertFalse(arg2,msg="XXX") | Prediction expression arg2 For false , Otherwise output XXX |
HTML Report generation
Run a test suite , There are hundreds of test cases , How to view the execution results of test cases centrally and clearly ? After the script is executed, it can generate HTML The report
HTMLTestRunner.py file , And put in python38/Lib Under the table of contents , Download address :http://tungwaiyip.info/software/HTMLTestRunner.html
Generate HTML Reporting steps :
1. Create a repository HTML Report folder :
curpath=sys.path[0]
if not os.path.exists(curpath+'/resultreport'):
os.makedirs(curpath+'/resultreport')
2. Solve the problem of duplicate naming ( Name it by time ):
now=time.strftime("%Y-%m-%d %M %S",time.localtime(time.time()))
filename=curpath+'/resultreport/'+now+'resultreport.html'
3. The output of the report :
Exception capture and error screenshot
Error screenshot API:get_screenshot_as_file()
Purpose : Keep the test site
Data driven
python Of unittest No built-in data-driven function . So if you use unittest, At the same time, I want to use data-driven , Then you can use it ddt To complete .ddt Installation
ddt Use
Guide pack :from ddt import ddt,unpack,data,file_date ; At the same time, use labels on classes @ddt
Data driven way :
@data(value) Pass one parameter at a time
@data(value1,value2....) Multiple parameters at a time , Need to use @unpack mapping
@file_data("json file ")
@data(* Method of parsing data (txt/csv file ))
边栏推荐
- Diffuse reflection of QT OpenGL light
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用deviance函数计算出模型的残差平方和
- 82. (cesium article) cesium points move on 3D models
- Do you know about data synchronization?
- 全局样式与图标
- User login switching case
- How to solve the problem that tp6 controller does not exist: app\controller\index
- LeetCode-136-只出现一次的数字
- Qt OPenGL 光的漫反射
- 坚持做一件事情
猜你喜欢

API Gateway介绍
![[dart] a programming language for cross end development](/img/e1/1167a322bb9f276f2e00fb12414d17.png)
[dart] a programming language for cross end development

全局样式与图标

二舅,为什么火了?

Hexagon_V65_Programmers_Reference_Manual(6)
![Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]](/img/ed/6f4da22e86b44935fc84e3b4901c48.png)
Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]

LeetCode-136-只出现一次的数字

CPDA | how to have data analysis thinking?

如何对话CIO/CTO
![[numpy] broadcast mechanism](/img/1f/8d61ac7b35a82067bc0b77426590eb.png)
[numpy] broadcast mechanism
随机推荐
Opencv implements image clipping and scaling
Hexagon_ V65_ Programmers_ Reference_ Manual(6)
LeetCode每日一练 —— CM11 链表分割
DJI push code (one code for one use, updated on July 26, 2022)
QT link MSSQL
Rk3399 platform development series explanation (process part) 15.36, understanding process and collaboration process
Hexagon_V65_Programmers_Reference_Manual(8)
ELK太重?试试KFC日志采集
IPv4/IPv6、DHCP、网关、路由
认识网络模型网络模型概述
opencv实现图片裁剪和缩放
自定义学习率
Things about stack migration
Knowledge management system promotes the development of enterprise informatization
R语言使用epiDisplay包的power.for.2p函数进行效用分析 ( 效能分析、Power analysis)、给定两个样本的比例值(proportions)、样本量计算效用值
R语言使用dplyr包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组均值(mean)并合并生成的统计数据到原数据集中
API Gateway介绍
Kingbasees heterogeneous database migration guide (2. Overview)
Leetcode-136-a number that appears only once
Sscanf caused the address to be out of bounds
