当前位置:网站首页>Test framework unittest test test suite, results output to file
Test framework unittest test test suite, results output to file
2022-07-25 16:27:00 【wangmcn】
test suite 、 The results are output to a file
Catalog
- 1、 test suite
- 1.1、 Mode one :unittest.main()
- 1.2、 Mode two :unittest.TestSuite()
- 1.3、 Mode three :unittest.defaultTestLoader()
- 2、 The results are output to a file
1、 test suite
1.1、 Mode one :unittest.main()
1、 establish test_Case1.py file
Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import unittest modular
import unittest
"""
test suite : Mode one :unittest.main()
"""
# Define test classes , The parent class is unittest.TestCase
class TestDemo(unittest.TestCase):
# Each test method is executed before running
def setUp(self):
print('setUp')
# Execute after each test method is run
def tearDown(self):
print('tearDown')
# All test methods should be based on test Start with
def test_case1(self):
print('test_case1')
def test_case3(self):
print('test_case3')
def test_case2(self):
print('test_case2')
if __name__ == '__main__':
unittest.main(verbosity=2)2、 perform test_Case1.py file , Running results :
unittest.main() Method will search all under this module test The initial test method , And automatically execute them .
The execution order is the naming order : Execute first test_case1, Re execution test_case2, Finally, execute test_case3.
1.2、 Mode two :unittest.TestSuite()
1、 establish test_Case2.py file
Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import unittest modular
import unittest
"""
test suite : Mode two :unittest.TestSuite()
"""
# Define test classes , The parent class is unittest.TestCase
class TestDemo(unittest.TestCase):
# Each test method is executed before running
def setUp(self):
print('setUp')
# Execute after each test method is run
def tearDown(self):
print('tearDown')
# All test methods should be based on test Start with
def test_case1(self):
print('test_case1')
def test_case2(self):
print('test_case2')
def test_case3(self):
print('test_case3')
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestDemo('test_case2'))
suite.addTest(TestDemo('test_case1'))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)2、 perform test_Case2.py file , Running results :
(1) Construct test set , Instantiate the test suite .
suite = unittest.TestSuite()(2) Load test cases into the test suite .
The execution sequence is installation loading sequence : Execute first test_case2, Re execution test_case1, Don't execute test_case3( Because it is not loaded ).
suite.addTest(TestDemo('test_case2'))
suite.addTest(TestDemo('test_case1'))(3) Execute test case , Instantiation TextTestRunner class .
runner = unittest.TextTestRunner(verbosity=2)(4) Use run() Method to run the test suite ( That is, run all the use cases in the test suite ).
runner.run(suite)1.3、 Mode three :unittest.defaultTestLoader()
1、 establish test_Case3.py file
Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import unittest modular
import unittest
"""
test suite : Mode three :unittest.defaultTestLoader()
"""
# Define test classes , The parent class is unittest.TestCase
class TestDemo(unittest.TestCase):
# Each test method is executed before running
def setUp(self):
print('setUp')
# Execute after each test method is run
def tearDown(self):
print('tearDown')
# All test methods should be based on test Start with
def test_case1(self):
print('test_case1')
def test_case3(self):
print('test_case3')
def test_case2(self):
print('test_case2')
if __name__ == '__main__':
test_dir = './'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_*3.py')
runner = unittest.TextTestRunner(verbosity=2)
runner.run(discover)2、 perform test_Case3.py file , Running results :
(1) Construct test set , Use defaultTestLoader Of discover Method to find the test case file .
The execution order is the naming order : Execute first test_case1, Re execution test_case2, Finally, execute test_case3.
test_dir = './'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_*3.py')(2) Execute test case , Instantiation TextTestRunner class .
runner = unittest.TextTestRunner(verbosity=2)(3) Use run() Method to run the test suite ( That is, run all the use cases in the test suite ).
runner.run(discover)2、 The results are output to a file
When executing a test case , Want to output the results to a file , You can use Python Of open() Function to operate on a file .
1、 establish OutputFile.py file
Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import unittest modular
import unittest
"""
The results are output to a file
"""
# Define test classes , The parent class is unittest.TestCase
class TestDemo(unittest.TestCase):
# Each test method is executed before running
def setUp(self):
print('setUp')
# Execute after each test method is run
def tearDown(self):
print('tearDown')
# All test methods should be based on test Start with
def test_case1(self):
print('test_case1')
def test_case2(self):
print('test_case2')
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestDemo('test_case1'))
suite.addTest(TestDemo('test_case2'))
# Perform the test , Output the result to OutputFile In file
with open('OutputFile.txt', 'a') as f:
runner = unittest.TextTestRunner(stream=f, verbosity=2)
runner.run(suite)2、 perform OutputFile.py file , Running results :
At the same time, it will generate OutputFile.txt file ( If the file exists, it does not need to be generated again ), As shown in the figure : Output the results to this file .
边栏推荐
- 狂神redis笔记12
- easyui下拉框,增加以及商品的上架,下架
- SAP Fiori 的附件处理(Attachment handling)
- Attachment handling of SAP Fiori
- Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
- 用递归进行数组求和
- 百度富文本编辑器UEditor单张图片上传跨域
- 从业务需求出发,开启IDC高效运维之路
- Lazy loading of pictures
- QT ListView 列表显示组件笔记
猜你喜欢

Breakthrough in core technology of the large humanoid Service Robot Walker x

【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码

QT ListView 列表显示组件笔记
![Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]](/img/03/54a2d82a17cd07374dc0aedacd7b11.png)
Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]

Today, I went to oppo for an interview and was asked numbly

如何安装govendor并打开项目

Solve win10 disk occupation of 100%

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具

新增批量删除

柏睿数据加入阿里云PolarDB开源数据库社区
随机推荐
Simple rotation map and hamster beating
测试框架-unittest-跳过测试
【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
Gap Locks(间隙锁)
High score technical document sharing of ink Sky Wheel - Database Security (48 in total)
Gap locks
狂神redis笔记12
Pagehelper.startpage is not effective
邮件的收发的展现逻辑之收件箱发件箱以及回复断链的问题
Today, I went to oppo for an interview and was asked numbly
MySQL table write lock
linux内核源码分析之页表缓存
权限管理-删除菜单(递归)
LVGL 7.11 tileview界面循环切换
MySQL implicit lock
Various useful forms of London Silver K-line chart
如何安装govendor并打开项目
2w字详解数据湖:概念、特征、架构与案例
使用Huggingface在矩池云快速加载预训练模型和数据集
吴恩达逻辑回归2