unittest 测试套件使用汇总篇
# coding=utf-8
import unittest
from inspect import isfunction def usage():
"""also unittest.__dict__"""
print(unittest.__all__) def stdout_writeText():
suite = unittest.TestSuite()
tests = unittest.TestLoader().loadTestsFromTestCase(TestHetero)
suite.addTests(tests)
with open('test_result.txt', 'w', encoding='utf-8') as file:
runner = unittest.TextTestRunner(stream=file, descriptions='这是第一次执行用例的测试报告!', verbosity=2)
runner.run(suite) def asciisort_members(instance_cls):
"""
instance_cls is class instance object,return : members method name list of class
for exp: ['test_001', 'test_002', 'test_Account', 'test_user'] """
self = instance_cls
return (list(filter(lambda m: not m.startswith("__")
and not m.endswith("__") and m.startswith("test_") and
callable(getattr(self, m)), dir(self)))) def unsort_functions(cls_name:object):
"""cls_name is TestClass Name,return test_* function suite"""
dict_items = list(
filter(lambda x: not x[0].startswith("__") and x[0].startswith("test_"), cls_name.__dict__.items()))
functions = [v for k, v in dict_items if isfunction(v)]
suite = unittest.TestSuite()
suite.addTests(functions)
return suite def raw_members(class_name: object):
""" get all unsort members test_* name list eg it also work well : list(filter(lambda x: not x.startswith("__")
and not x.endswith("__") and x.startswith("test_") ,TestHetero.__dict__.keys())) """ return list(filter(lambda x: not x.startswith("__") and not x.endswith("__") and x.startswith("test_") and isfunction(
eval(class_name.__name__ + ".%s" % x)) and callable(eval(class_name.__name__ + ".%s" % x)),
class_name.__dict__.keys())) def getCaseNameList(cls_name):
"""default ascii sort 0-9 A-Z,a-z ,return list """
return unittest.TestLoader().getTestCaseNames(cls_name) def ascii_makeSuite(cls_name):
"""also unittest.makeSuite(cls_name)"""
return unittest.loader.makeSuite(cls_name) def ascii_loaderClass(cls_name):
"""return suite according to className"""
return unittest.TestLoader().loadTestsFromTestCase(cls_name) def ascii_loaderMoudle(moudleName):
"""unittest.TestLoader().loadTestsFromModule(Hetero),Hetero.py is a module"""
return unittest.TestLoader().loadTestsFromModule(moudleName) def loader_method(fun_name: str):
""" unittest.TestLoader().loadTestsFromName("Hetero.TestHetero.test_user")"""
return unittest.TestLoader().loadTestsFromName(fun_name) def loader_methods(fun_namelist: list):
"""unittest.TestLoader().loadTestsFromNames(["Hetero.TestHetero.test_user","Hetero.TestHetero.test_001"])"""
return unittest.TestLoader().loadTestsFromNames(fun_namelist) def ascii_defaultloader(cls_name):
return unittest.defaultTestLoader.loadTestsFromTestCase(cls_name) def discovers(test_dir):
"""return suite"""
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
return discover def suite_addtests(iter_list):
"""[TestHetero("test_001"),TestHetero("test_002")]"""
suite = unittest.TestSuite()
suite.addTests(iter_list)
return suite def suite_addtest(func_method):
"""TestHetero("test_001") : format is className("method_name") """
suite = unittest.TestSuite()
suite.addTests(func_method)
return suite class TestHetero(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass def test_Account(self):
print("Account") def test_user(self):
print("user") def test_001(self):
print("001")
def test_002(self):
print("002") if __name__ == '__main__':
#方法一
suite=unittest.TestSuite()
suite.addTests([TestHetero("%s"%value) for value in raw_members(TestHetero)])
print(suite)
unittest.TextTestRunner(verbosity=2).run(suite)
#方法二
suite2=unsort_functions(TestHetero)
unittest.TextTestRunner(verbosity=2).run(suite2)
test_Account (__main__.TestHetero) ... ok
test_user (__main__.TestHetero) ... ok
test_001 (__main__.TestHetero) ... ok
test_002 (__main__.TestHetero) ... ok
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK
unittest 测试套件使用汇总篇的更多相关文章
- unittest测试套件
测试套件就是测试集,测试集是测试用例的集合. a.按用例顺序执行(addtest) 当addtest与unittest的测试规则冲突时,仍然按照ASCII码的顺序执行. import unittest ...
- Python+Selenium笔记(四):unittest的Test Suite(测试套件)
(一) Test Suite测试套件 一个测试套件是多个测试或测试用例的集合,是针对被测程序的对应的功能和模块创建的一组测试,一个测试套件内的测试用例将一起执行. 应用unittest的TestSui ...
- unittest单元测试框架之测试套件(三)
1.测试套件(注意:测试用例先添加先执行,后添加后执行,由此组织与设定测试用例的执行顺序) addTests:添加多个测试用例 addTest:添加单个测试用例 import unittest fro ...
- python+unittest框架第二天unittest之简单认识Test Suite:测试套件
今天了解下测试套件Test Suite,什么是测试套件,测试套件是由多个Test Case测试用例组成的,当然也可以由多个子测试套件组成. 接下来看下如果构建测试套件,构建测试套件的方法: 1.用un ...
- unittest详解(三) 测试套件(TestSuite)
在前面一章中示例了如何编写一个简单的测试,但有两个问题: 我们知道测试用例的执行顺序是根据测试用例名称顺序执行的,在不改变用例名称的情况下,我们怎么来控制用例执行的顺序呢? 一个测试文件,我们直接执行 ...
- Unittest框架之测试套件:TestSuite
前言 使用了unittest.main()方法执行当前模块里的测试用例. 除此之外,Unittest还可以通过测试套件构造测试用例集,再执行测试用例 将测试用例添加至TestSuite(测试套件) 方 ...
- 『心善渊』Selenium3.0基础 — 28、unittest中测试套件的使用
目录 1.测试套件的作用 2.使用测试套件 (1)入门示例 (2)根据不同的条件加载测试用例(了解) (3)常用方式(推荐) 1.测试套件的作用 在我们实际工作,使用unittest框架会有两个问题: ...
- Unittest方法 -- 测试套件
TestSuite 测试固件 一. import unittestclass F6(unittest.TestCase): def setUp(self): pass def tearDown(sel ...
- python实例编写(7)---测试报告与测试套件(多个py文件,1个py文件内多个用例)
一. 一个.py文件批量执行测试用例(一个.py文件下多个用例执行) 如果直接使用:unittest.main(),则按字母顺序执行, 对于前后之间又依赖关系的用例,需要按特定的顺序执行,则使用 s ...
随机推荐
- Docker镜像加速-配置阿里云镜像仓库
Docker默认远程仓库是https://hub.docker.com/ 比如我们下载一个大点的东西,龟速 由于是国外主机,类似Maven仓库,慢得一腿,经常延迟,破损: 所以我们一般都是配置国内镜像 ...
- AntDesign(React)学习-6 Menu展示数据
1.官方文档请查看https://ant.design/components/menu-cn/antPro自带的菜单功能很强大,但是太复杂了,感觉大部分功能都用不上,下面实现一个简单从后台动态获取菜单 ...
- webpack 之搭建本地服务器
搭建本地服务器 webpack提供了一个可选的本地开发服务器,这个本地服务器基于node.js搭建,内部使用express框架,可以实现 我们想要的让浏览器自动刷新显示我们修改后的结果 不过它是一个单 ...
- 还不错的Table样式和form表单样式
作为一个后台开发人员而言,拥有一套属于自己的前台样式是比较重要的,这里分享一下自己感觉还不错的样式,以后遇到好的,还会陆续添加 上图: 带鼠标滑动效果的table样式看起来比较清爽 样式 <he ...
- Java流,文件和I/O
java.io包中包含几乎所有可能永远需要在Java中执行输入和输出(I/ O)类.所有这些数据流代表一个输入源和输出目标. java.io包中的流支持多种数据,如基本类型,对象,本地化的字符等 流可 ...
- Codeforces Round #603 (Div. 2) A.Sweet Problem
#include <cstdio> #include <algorithm> using namespace std; int main() { int t; scanf(&q ...
- 题解【洛谷P1352】没有上司的舞会
题面 题解 树形\(\text{DP}\)入门题. 我们设\(dp[i][0/1]\)表示第\(i\)个节点选\(/\)不选的最大快乐指数. 状态转移方程: \(dp[i][0]=a[i]+\sum_ ...
- 为什么hashmap的容量永远要是2的次方
源码hashmap.java文件中有个函数叫tableSizeFor(),他的作用是,通过-1>>>n-1返回一个大于n的最小二次幂,n为map之前的容量,而函数返回值就是扩容的二次 ...
- Python之路Day01
一.Python简介 Python的历史 Python 2.4 - November 30, 2004, 同年目前最流行的WEB框架Django 诞生 In November 2014, it was ...
- [SDOI2016] 生成魔咒 - 后缀数组,平衡树,STL,时间倒流
[SDOI2016] 生成魔咒 Description 初态串为空,每次在末尾追加一个字符,动态维护本质不同的子串数. Solution 考虑时间倒流,并将串反转,则变为每次从开头删掉一个字符,即每次 ...