当前位置:网站首页>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 ))
边栏推荐
- DJI push code (one code for one use, updated on July 26, 2022)
- Things about stack migration
- 自定义学习率
- Uncaught SyntaxError: redeclaration of let page
- R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数、均值和中位数、使用list函数指定函数列表(使用.符号和~符号指定函数语法purr)
- Brief description of tenant and multi tenant concepts in cloud management platform
- Where is the program?
- Hexagon_V65_Programmers_Reference_Manual(6)
- opencv实现图片裁剪和缩放
- 原生对象、内置对象、宿主对象的区别
猜你喜欢

Things about stack migration

82.(cesium篇)cesium点在3d模型上运动

北京/上海/广州/深圳DAMA-CDGA/CDGP数据治理认证报名条件

品牌列表案例

LeetCode每日一练 —— CM11 链表分割

Read Plato & nbsp; Eplato of farm and the reasons for its high premium

Airiot Q & A issue 6 | how to use the secondary development engine?

Recommend a powerful search tool listary

程序中的地址如何转换?

NPDP|什么样的产品经理可以被称为优秀?
随机推荐
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
Brief description of tenant and multi tenant concepts in cloud management platform
CPDA | how to have data analysis thinking?
LeetCode每日一练 —— 链表中倒数第 k 个结点
[program life] "stage summary" - unwilling to be ordinary
Leetcode-136-a number that appears only once
sql编码bug
Kingbasees heterogeneous database migration guide (2. Overview)
Uncaught SyntaxError: redeclaration of let page
VI working mode (3 kinds) and mode switching (conversion)
IPv4/IPv6、DHCP、网关、路由
R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数、均值和中位数、使用list函数指定函数列表(使用.符号和~符号指定函数语法purr)
DJI push code (one code for one use, updated on July 26, 2022)
How to check the Bluetooth version of Bluetooth headset
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用deviance函数计算出模型的残差平方和
User login switching case
Recommend a powerful search tool listary
Understand the encapsulation and de encapsulation of network model data
知识管理系统推动企业信息化发展
如何解决tp6控制器不存在:app\controller\Index
