当前位置:网站首页>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 ))
边栏推荐
- [Numpy] 广播机制(Broadcast)
- 好开不贵,舒适安全!深度体验比亚迪宋Pro DM-i
- Automatic test solution based on ATX
- Tips for file upload to bypass WAF
- Global styles and icons
- 飞信卒于2022:中国移动一手好牌被打烂,5亿用户成“僵尸”
- R语言使用epiDisplay包的power.for.2p函数进行效用分析 ( 效能分析、Power analysis)、给定两个样本的比例值(proportions)、样本量计算效用值
- One article to understand pychar shortcut key
- A lock faster than read-write lock. Don't get to know it quickly
- Programmer growth Chapter 18: project launch
猜你喜欢

How to make personalized recommendations instantly accessible? Cloud native database gaussdb (for redis) to help

最新版web漏洞扫描工具AppScan\AWVS\Xray安装及使用教程

How to translate the address in the program?

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

js闭包知识

Introduction to source insight 4.0

CPDA|如何拥有数据分析思维?

LeetCode-209-长度最小的子数组

二舅,为什么火了?

sql编码bug
随机推荐
你了解数据同步吗?
go --- air自动重新编译
好开不贵,舒适安全!深度体验比亚迪宋Pro DM-i
如何让个性化推荐即刻触达?云原生数据库GaussDB(for Redis)来助力
LeetCode每日一练 —— 21. 合并两个有序链表
认识传输介质网络通信的介质
How to realize document collaboration?
LeetCode每日一练 —— 206. 反转链表
记一次restTemplate.getForEntity携带headers失败,restTemplate. exchange
用户登录切换案例
Opencv implements image clipping and scaling
LeetCode-136-只出现一次的数字
Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)
Riding lantern case
Automatic test solution based on ATX
Hexagon_ V65_ Programmers_ Reference_ Manual(5)
Hexagon_ V65_ Programmers_ Reference_ Manual(7)
LeetCode每日一练 —— 203. 移除链表元素
QT OpenGL makes objects move under care to form animation
SRE相关问题答疑
