<自动化测试>之<使用unittest Python测试框架进行参数化测试>
最近在看视频时,虫师简单提到了简化自动化测试脚本用例中的代码量,而python中本身的参数化方法用来测试很糟糕,他在实际操作中使用了parameterized参数化...
有兴趣就查了下使用的方法,来分享给大家,使用Python测试框架进行参数化测试 下载安装https://github.com/wolever/parameterized或PIP install: $ pip install parameterized
parameterized
了修正对于一切nose参数化测试,py.test参数化测试,单元测试参数化测试。
# test_math.py
from nose.tools import assert_equal
from parameterized import parameterized import unittest
import math @parameterized([
(2, 2, 4),
(2, 3, 8),
(1, 9, 1),
(0, 9, 0),
])
def test_pow(base, exponent, expected):
assert_equal(math.pow(base, exponent), expected) class TestMathUnitTest(unittest.TestCase):
@parameterized.expand([
("negative", -1.5, -2.0),
("integer", 1, 1.0),
("large fraction", 1.6, 1),
])
def test_floor(self, name, input, expected):
assert_equal(math.floor(input), expected)
在 nose (and nose2)下运行: $ nosetests -v test_math.py
test_math.test_pow(2, 2, 4) ... ok
test_math.test_pow(2, 3, 8) ... ok
test_math.test_pow(1, 9, 1) ... ok
test_math.test_pow(0, 9, 0) ... ok
test_floor_0_negative (test_math.TestMathUnitTest) ... ok
test_floor_1_integer (test_math.TestMathUnitTest) ... ok
test_floor_2_large_fraction (test_math.TestMathUnitTest) ... ok ----------------------------------------------------------------------
Ran 7 tests in 0.002s OK
As the package name suggests, nose is best supported and will be used for all further examples. With py.test (version 2.0 and above): $ py.test -v test_math.py
============================== test session starts ==============================
platform darwin -- Python 2.7.2 -- py-1.4.30 -- pytest-2.7.1
collected 7 items test_math.py::test_pow::[0] PASSED
test_math.py::test_pow::[1] PASSED
test_math.py::test_pow::[2] PASSED
test_math.py::test_pow::[3] PASSED
test_math.py::TestMathUnitTest::test_floor_0_negative
test_math.py::TestMathUnitTest::test_floor_1_integer
test_math.py::TestMathUnitTest::test_floor_2_large_fraction =========================== 7 passed in 0.10 seconds ============================
With unittest (and unittest2): $ python -m unittest -v test_math
test_floor_0_negative (test_math.TestMathUnitTest) ... ok
test_floor_1_integer (test_math.TestMathUnitTest) ... ok
test_floor_2_large_fraction (test_math.TestMathUnitTest) ... ok ----------------------------------------------------------------------
Ran 3 tests in 0.000s OK
(note: because unittest does not support test decorators, only tests created with @parameterized.expand will be executed)
在@parameterized与@parameterized.expand装饰接受列表或可迭代的元组或param(...),或调用它返回一个列表或可迭代, 下面是比较全的使用方法示例:
from parameterized import parameterized, param # A list of tuples
@parameterized([
(2, 3, 5),
(3, 5, 8),
])
def test_add(a, b, expected):
assert_equal(a + b, expected) # A list of params
@parameterized([
param("", 10),
param("", 16, base=16),
])
def test_int(str_val, expected, base=10):
assert_equal(int(str_val, base=base), expected) # An iterable of params
@parameterized(
param.explicit(*json.loads(line))
for line in open("testcases.jsons")
)
def test_from_json_file(...):
... # A callable which returns a list of tuples
def load_test_cases():
return [
("test1", ),
("test2", ),
]
@parameterized(load_test_cases)
def test_from_function(name):
...
请注意,在使用迭代器或生成器时,在开始测试运行之前,所有项目都将被加载到内存中(我们明确地做到这一点,以确保生成器在多进程或多线程测试环境中精确地耗尽一次) 。
@parameterized
装饰可用于测试类的方法,和独立的功能:
from parameterized import parameterized class AddTest(object):
@parameterized([
(2, 3, 5),
])
def test_add(self, a, b, expected):
assert_equal(a + b, expected) @parameterized([
(2, 3, 5),
])
def test_add(a, b, expected):
assert_equal(a + b, expected)
并且@parameterized.expand
可以用于在不能使用测试生成器的情况下生成测试方法(例如,当测试类是子类时unittest.TestCase
):
import unittest
from parameterized import parameterized class AddTestCase(unittest.TestCase):
@parameterized.expand([
("2 and 3", 2, 3, 5),
("3 and 5", 2, 3, 5),
])
def test_add(self, _, a, b, expected):
assert_equal(a + b, expected)
会创建测试用例:
$ nosetests example.py
test_add_0_2_and_3 (example.AddTestCase) ... ok
test_add_1_3_and_5 (example.AddTestCase) ... ok ----------------------------------------------------------------------
Ran 2 tests in 0.001s OK
请注意,@parameterized.expand
通过在测试类上创建新方法。如果第一个参数是一个字符串,该字符串将被添加到方法名称的末尾。例如,上面的测试用例会生成方法 test_add_0_2_and_3
和test_add_1_3_and_5
。
生成的测试用例的名称@parameterized.expand
可以使用testcase_func_name
keyword参数自定义。该值应该是这三个参数的函数:testcase_func
,param_num
,和params
,应该返回测试用例的名字。 testcase_func
将被测试的功能,param_num
将参数列表中的测试用例参数的索引,和param
(一个实例param
)将被使用的参数。
import unittest
from parameterized import parameterized def custom_name_func(testcase_func, param_num, param):
return "%s_%s" %(
testcase_func.__name__,
parameterized.to_safe_name("_".join(str(x) for x in param.args)),
) class AddTestCase(unittest.TestCase):
@parameterized.expand([
(2, 3, 5),
(2, 3, 5),
], testcase_func_name=custom_name_func)
def test_add(self, a, b, expected):
assert_equal(a + b, expected)
创建测试用例:
$ nosetests example.py
test_add_1_2_3 (example.AddTestCase) ... ok
test_add_2_3_5 (example.AddTestCase) ... ok ----------------------------------------------------------------------
Ran 2 tests in 0.001s OK
该param(...)
助手类存储一个特定的测试情况的参数。它可以用于将关键字参数传递给测试用例:
from parameterized import parameterized, param @parameterized([
param("", 10),
param("", 16, base=16),
])
def test_int(str_val, expected, base=10):
assert_equal(int(str_val, base=base), expected)
如果测试用例有一个docstring,则该测试用例的参数将追加到docstring的第一行。这个行为可以用doc_func
参数控制:
from parameterized import parameterized @parameterized([
(1, 2, 3),
(4, 5, 9),
])
def test_add(a, b, expected):
""" Test addition. """
assert_equal(a + b, expected) def my_doc_func(func, num, param):
return "%s: %s with %s" %(num, func.__name__, param) @parameterized([
(5, 4, 1),
(9, 6, 3),
], doc_func=my_doc_func)
def test_subtraction(a, b, expected):
assert_equal(a - b, expected)
$ nosetests example.py
Test addition. [with a=1, b=2, expected=3] ... ok
Test addition. [with a=4, b=5, expected=9] ... ok
0: test_subtraction with param(*(5, 4, 1)) ... ok
1: test_subtraction with param(*(9, 6, 3)) ... ok ----------------------------------------------------------------------
Ran 4 tests in 0.001s OK
仔细学习可以查看在github上有详尽的使用方法
from wolever & thanks!!!
<自动化测试>之<使用unittest Python测试框架进行参数化测试>的更多相关文章
- 彻底弄清c标准库中string.h里的常用函数用法
在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...
- 走进C标准库(8)——"string.h"中函数的实现相关字符串操作函数
我的strcat: char *strcat(char *dest,char *src) { char * reval = dest; while(*dest) dest++; while(*src) ...
- 走进C标准库(3)——"stdio.h"中的getc和ungetc
接前文. 再来看看getc和ungetc的实现.在看这两个函数的实现之前,我们先来想一想这两个函数分别需要做的工作. int getc(FILE *stream) 说明:函数getc从stream指向 ...
- 走进C标准库(2)——"stdio.h"中的fopen函数
其他的库文件看起来没有什么实现层面的知识可以探究的,所以,直接来看stdio.h. 1.茶余饭后的杂谈,有趣的历史 在过去的几十年中,独立于设备的输入输出模型得到了飞速的发展,标准C从这个改善的模型中 ...
- 走进C标准库(1)——assert.h,ctype.h
默默觉得原来的阅读笔记的名字太土了,改了个名字,叫做走进C标准库. 自己就是菜鸟一只,第一次具体看C标准库,文章参杂了对<the standard C library>的阅读和对源码的一些 ...
- C 非标准库(conio.h)
所谓的 C 标准库(C standard library),是指在 ISO C 或者 POSIX 标准中定义的: POSIX is a superset(超集) of the standard C l ...
- 走进C标准库(4)——"stdio.h"中的putc
花了点时间把园子弄得好看了点,现在继续. 函数名: putc 功 能: 输出一字符到指定流中 用 法: int putc(int ch, FILE *stream); #define _putc_ ...
- 走进C标准库(5)——"stdio.h"中的其他部分函数
函数介绍来自:http://ganquan.info/standard-c/ 函数名: freopen 功 能: 替换一个流 用 法: FILE *freopen(char *filename, ...
- 走进C标准库(6)——"string.h"中函数的实现memchr
我写的memchr: void *memchr(const void *buf, char ch, unsigned count){ unsigned ; while(*(buf++) != ch & ...
- 走进C标准库(7)——"string.h"中函数的实现memcmp,memcpy,memmove,memset
我的memcmp: int memcmp(void *buf1, void *buf2, unsigned int count){ int reval; while(count && ...
随机推荐
- keil c51 不能使用:Go to Definition of....的解决方法 STC51
keil c51 不能使用:Go to Definition of....的解决方法 达到的目标如下图所示: 解决方法为 :在工程栏右键单击进入Manage Components ,然后点确定,前提是 ...
- AT2000 Leftmost Ball(计数dp+组合数学)
传送门 解题思路 设\(f[i][j]\)表示填了\(i\)个白色,\(j\)种彩色的方案数,那么显然\(j<=i\).考虑这个的转移,首先可以填一个白色,就是\(f[i][j]=f[i-1][ ...
- [HDU3117]Fibonacci Numbers
题目:Fibonacci Numbers 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 分析: 1)后四位可以用矩阵快速幂解决.$T= \left ...
- 转载:IDEA配置SVN及使用
转自:https://blog.csdn.net/zwj1030711290/article/details/80687365 1.安装svn客户端 之前用myEcplise只需要插件,现在IDEA需 ...
- hdu 5396 Expression
考虑到此题麻烦了某hust大神&体现出了自己数学能力的欠缺 虽然最近一直比较忙 还是把这题的题解写下来吧 首先看完数据范围后 应该有不少人会反应到是$n^3$的DP 以$F[i][j]$表示从 ...
- Workflow:Workflow 目录
ylbtech-Workflow:Workflow 目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:ht ...
- Jmeter 5.1 从excel读取数据执行接口自动化
思路:数据在excel文件中进行维护,然后转换成csv格式,jme中读取数据执行: 1.将接口各数据在excel文件中进行维护,然后存为csv格式,我的数据如下: 2.jmeter脚本,配置csv文件 ...
- 用 Flask 来写个轻博客 (30) — 使用 Flask-Admin 增强文章管理功能
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 实现文章管理功能 实现效果 前文列表 用 Flask 来写个 ...
- smartGit的使用
合理管理自己的代码,对程序员来说是一件很重要的事,今天我也走上了github托管之路 要在Git上托管代码,首先你要要github官网创建一个代码仓库,用来放你的代码. 一,要托管到github,那你 ...
- 使用systemctl管理服务
系统服务,开机不需要登录就能运行的程序(相当于开机自启)/usr/lib/systemd/system 用户服务,需要登录后才能运行的程序/usr/lib/systemd/user 目录下又存在两种类 ...