当前位置:网站首页>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 .
边栏推荐
猜你喜欢

伦敦银K线图的各种有用形态

Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier

easyui修改以及datagrid dialog form控件使用

MyBaits

一文理解分布式开发中的服务治理

Differences between cookies, cookies and sessions

Product upgrade observation station in June

Quickly deploy mqtt clusters on AWS using terraform

Paper notes: highly accurate protein structure prediction with alphafold (alphafold 2 & appendix)

柏睿数据加入阿里云PolarDB开源数据库社区
随机推荐
Fastadmin TP installation uses Baidu rich text editor ueeditor
EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
Record locks
High score technical document sharing of ink Sky Wheel - Database Security (48 in total)
Typescript learning 1 - data types
MySQL metadata lock (MDL)
论文笔记:Highly accurate protein structure prediction with AlphaFold (AlphaFold 2 & appendix)
Recursive menu query (recursion: check yourself)
MySQL之联表查询、常用函数、聚合函数
Two methods of importing sqllite table from MySQL
食品安全丨无处不在的冷冻食品,你真的了解吗?
权限管理-角色分配菜单
测试框架-unittest-命令行操作、断言方法
Permission management - delete menu (recursive)
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
Analysis and solution of data and clock mismatch delay in SPI transmission
LVGL 7.11 tileview界面循环切换
Promise期约
可验证随机函数 VRF
Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier