当前位置:网站首页>Unittest suite and runner
Unittest suite and runner
2022-07-27 06:56:00 【Si Xiaoyou】
1.Skip The use of decorators
''' Skip The use of decorators skip It means to skip unconditionally skipIf Indicates that the expression is true , Then skip , If not, continue to implement skipUnless And skipIf contrary expectedFailure Default use case execution will fail , And ignore it Unittest Acrobatic suit under '''
import unittest
class Demo(unittest.TestCase):
@unittest.skip(' Unconditionally skip the use case execution ')
def test_01(self):
print(1)
@unittest.skipIf(1 == 2,' This is a If The reason of ')
def test_02(self):
print(2)
# @unittest.skipUnless( 1== 2,' This is a Unless The reason of ')
def test_03(self):
print(3 / 0)
# @unittest.expectedFailure
def test_04(self):
print(4)
self.assertEqual(1,2,msg = ' Assertion failed ')
# if __name__ == '__main__':
# unittest.main()
2.Suite test suite ,
''' Suite test suite , It is specially used to manage all test case classes . It can be understood as a use case List, In physical form, it is the concept of a folder Use of kits , It must be rebuilding py File to call , If in UnitTest Pass under class main Function calls will not take effect . unittest.TestSuite Class suite The operation of must be based on the operation of the runner Test cases running in the suite , It is executed based on the order of addition , And UnitTest It doesn't matter to sort the use cases HTMLTestRunner Test report : In essence , In fact, it is a runner . The deployment environment : Direct download py file , Put it in python Under the installation path lib Just in the folder . Don't pass pip install The default of online downloading is that only 2.7 Version and below . If you want to support 3 Above version , Need to modify the source code . modify py Source code of the document : The first 94 That's ok , take import StringIO Modified into import io The first 539 That's ok , take self.outputBuffer = StringIO.StringIO() Modified into self.outputBuffer = io.StringIO() The first 642 That's ok , take if not rmap.has_key(cls): Modified into if not cls in rmap: The first 766 That's ok , take uo = o.decode('latin-1') Modified into uo = e The first 772 That's ok , take ue = e.decode('latin-1') Modified into ue = e The first 631 That's ok , take print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime) Modified into print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)) UnitTest Use... For all test cases test start . All test case files , It also needs to be test start . '''
import os
import unittest
# from HTMLTestRunner import HTMLTestRunner
from HTMLTestReportCN import HTMLTestRunner
# from class27.test_demo import UnitDemo, UnitDemo02
# from class28.test_demo import Demo
1. Create a test suite
suite = unittest.TestSuite()
2. Add test cases to the suite
# Add a single test case based on the name of the use case
# suite.addTest(UnitDemo('test_02'))
# suite.addTest(Demo('test_03'))
3. Batch add use cases
# Add based on the use case name , With list form
# cases = [Demo('test_01'),UnitDemo('test_03')]
# suite.addTests(cases)
4. By adding a complete class Enter the kit
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Demo))
5. Add based on the file name
# What is added is under the current path py file , Otherwise, an error will be reported
# cases = [unittest.TestLoader().loadTestsFromNames('unit_demo.UnitDemo')]
# suite.addTests(cases)
# names = ['unit_demo.UnitDemo','unit_demo.UnitDemo02'] # py The name of the document +class The name of
# suite.addTests(unittest.TestLoader().loadTestsFromNames(names))
6. Add test cases in batch
# It is easy to use in continuous integration , Batch management test cases are also very useful
# Define the acquisition path of the use case
path = '../class28'
discover = unittest.defaultTestLoader.discover(start_dir=path,pattern='test*.py')
7. Run the suite through the runner
# run = unittest.TextTestRunner(verbosity=2) #verbosity The parameter indicates the detail of the displayed console information , It is divided into 0、1、2 Level three .
# run.run(suite)
# run.run(discover)
3.HTMLTestRunner Generate test reports
1. Configure the relevant contents of the test report
report_dir = './report/' # Save the path
report_title = ' Test report of Phyllostachys pubescens ' # Report name
report_description = ' This is the description part of the test report ' # Test report description
report_file = report_dir + 'report.html' # File of test report : It is recommended that the name of the test report be named in the form of timestamp
# Test executors
report_tester = ' Si Xiaoyou '
2. Whether the path to save the report exists , If it doesn't exist, create a
if not os.path.exists(report_dir):
os.mkdir(report_dir)
3. Generate test reports , In fact, it is to write the contents of a file
with open(report_file,'wb') as file:
runner = HTMLTestRunner(stream = file,title = report_title,description=report_description,verbosity=2,tester = report_tester)
runner.run(discover)
边栏推荐
- Multimodal database | star ring technology multimode database argodb "one database for multiple purposes", building a high-performance Lake warehouse integrated platform
- Problems related to compilation and training of Darknet yolov3 and Yolo fast using CUDA environment of rtx30 Series graphics card on win10 platform
- 关于卡尔曼滤波的协方差如何影响deepsort的跟踪效果的考虑
- Memo @restcontrolleradvice and exception interception class example
- 程序、进程、线程、协程以及单线程、多线程基本概念
- Alibaba cloud SMS authentication third-party interface (fast use)
- 【11】 Binary code: "holding two roller handcuffs, crying out for hot hot hot"?
- Boostrap
- 关于过快S验证码的一些问题
- C语言怎么学?这篇文章给你完整答案
猜你喜欢

Fix the problem that the paging data is not displayed when searching the easycvr device management list page

EasyCVR平台播放设备录像时,拖动时间轴播放无效是什么原因?

Alibaba cloud SMS authentication third-party interface (fast use)

win10 添加虚拟网卡,配置op路由

What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!

MySQL的基本语句(1)—增删改查

px4源码编译之 建立自己的程序模块

1. Install redis in CentOS 7

Install redis under Windows

About the new features of ES6
随机推荐
Is it feasible to fix the vulnerability with one click? Sunflower to tell you that one click fix vulnerability is feasible? Sunflower to tell you that one click fix vulnerability is feasible? Sunflowe
Where to connect with user-defined functions leads to slow queries
Code random notes_ Hash_ 242 effective letter heterotopic words
Use -wall to clear code hidden dangers
A cross domain problem of golang
EasyCVR设备管理列表页面搜索时,分页数据不显示的问题修复
Memo @restcontrolleradvice and exception interception class example
Project training experience 2
deepsort源码解读(五)
Disk management and file system
Future, futuretask and completable future are often asked in interviews
New features of ES6 (2)
GoLand 编写go程序
关于ES6的新特性
事件捕获方式和冒泡方式—它们的区别是什么?
Build cloud native operating environment
FTX US推出FTX Stocks,向主流金融行业迈进
deepsort源码解读(六)
FTX.US推出股票和ETF交易服务,让交易更透明
ESXI虚拟机启动,模块“MonitorLoop”打开电源失败