当前位置:网站首页>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)
边栏推荐
- 工控用Web组态软件比组态软件更高效
- 银行业客户体验管理现状与优化策略分析
- Ftx.us launched stock and ETF trading services to make trading more transparent
- Inventory of the world's six most technologically competent smart contract audit companies in 2022
- Event capture and bubbling - what is the difference between them?
- Introduction to the official functions of easyrecovery14 data recovery software
- Tips - completely delete the files on the USB flash drive
- torch加载自定义模型的问题
- DataScience:数据生成之在原始数据上添加小量噪声(可自定义噪声)进而实现构造新数据(dataframe格式数据存储案例)
- CentOS上使用Docker安装和部署Redis
猜你喜欢

Esxi virtual machine starts, and the module "monitorloop" fails to power on

CASS11.0.0.4 for AutoCAD2010-2023免狗使用方法

Sunflower popularizes Science in an all-round way to avoid loopholes for your remote control equipment in time

ES6新特性(入门)

Inventory of the world's six most technologically competent smart contract audit companies in 2022

AI:业余时间打比赛—挣它个小小目标—【阿里安全×ICDM 2022】大规模电商图上的风险商品检测比赛

Express框架

【11】 Binary code: "holding two roller handcuffs, crying out for hot hot hot"?

向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解

事件捕获方式和冒泡方式—它们的区别是什么?
随机推荐
Code random notes_ Hash_ 242 effective letter heterotopic words
According to SQL, you must know and learn SQL (MySQL)
IoTDB 的C# 客户端发布 0.13.0.7
Ancient art - make good use of long tail keywords
Shell编程的规范和变量
How to avoid loopholes? Sunflower remote explains the safe use methods in different scenarios
2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!
PSI|CSI和ROC|AUC和KS -备忘录
CASS11.0.0.4 for AutoCAD2010-2023免狗使用方法
Soul continues to make efforts to list its social channels in Hong Kong. Why is "soul style social" popular?
Redis' idea under windows is not connected
Where to connect with user-defined functions leads to slow queries
网站服务器被攻击怎么办?向日葵提示防范漏洞是关键
向日葵教大家如何防范拒绝服务攻击漏洞?
keras-ocr实例测试
Do it yourself container
gin-vue-admin 使用docker容器中的数据库
FTX US推出FTX Stocks,向主流金融行业迈进
FTX Foundation funded 15million to help covid-19 clinical trials, which will affect global public health
ES6 new features (getting started)