pytest插件生成随机数据的灵感来自quickcheck
pytest-quickcheck的Python项目详细描述
要求
- python 2.7或3.3及更高版本
功能
- 提供用于生成随机测试数据的pytest.mark.randomize函数
安装
$ easy_install pytest-quickcheck # or $ pip install pytest-quickcheck
快速启动
只需将函数的签名传递给randomize标记。 签名由参数名及其类型组成的元组表示。
@pytest.mark.randomize(i1=int, i2=int, ncalls=1) def test_generate_ints(i1, i2): pass
更复杂的数据结构:
@pytest.mark.randomize( d1={'x': int, 'y': [str, (int, int)], 'z': {'x': str}} ) def test_generate_dict(d1): pass
randomize标记可以与parametrize标记一起使用。
@pytest.mark.parametrize("prime", [2, 3, 5]) @pytest.mark.randomize(i1=int, f1=float, ncalls=1) def test_gen_parametrize_with_randomize_int_float(prime, i1, f1): pass
使用命令行选项--randomize仅限制随机化测试。
$ py.test -v --randomize test_option.py ========================================================================================== test session starts ========================================================================================== test_option.py:5: test_normal SKIPPED test_option.py:8: test_generate_ints[74-22] PASSED
使用量
每个数据类型都有一些选项:
$ py.test --markers @pytest.mark.randomize(argname=type, **options): mark the test function with random data generating any data type. There are options for each data type: (see doc for details) int: ['min_num', 'max_num'] float: ['min_num', 'max_num', 'positive'] str: ['encoding', 'fixed_length', 'min_length', 'max_length', 'str_attrs'] list_of, nonempty_list_of, dict_of: ['items', 'min_items', 'max_items']
通用选项
ncalls:设置调用数。默认为3。(例如,ncalls=5)选项:从给定序列中选择。(例如,选项=[3,5,7])整数
min_num:生成整数的下限。(例如,最小值=0)max_num:生成整数的上限。(例如,max_num=10)浮动
min_num:生成实数的下限。(例如,最小值=0.0)max_num:生成实数的上限。(例如,max_num=1.0)positive:如果设置为true,则仅生成正实数。 默认为false。(例如,正=真)str
编码:生成给定字符代码编码的Unicode字符串。 (例如encoding=“utf-8”)仅适用于python 2.x固定长度:生成固定长度字符串。(例如,固定长度=8)max_length:生成小于或等于max length的字符串 (例如,最大长度=32)str\u attrs:生成给定字母的字符串。 设置由string module中的属性名组成的元组。 (例如str_attrs=(“数字”,“标点符号”)列表的,非空列表的,dict的
项目:项目数。min_items:项目数量的下限。max_items:项目数的上限。
可能,tests/test_plugin_basic.py对于 学习如何使用这些选项。
生成集合
生成项目的可变长度列表:
from pytest import list_of @pytest.mark.randomize(l=list_of(int)) def test_list_of(l): pass
您可以使用items、min_items和 max_items选项,或使用nonempty_list_of快捷方式。
@pytest.mark.randomize(l=list_of(int, num_items=10)) def test_list_of_length(l): assert len(l) == 10 @pytest.mark.randomize(l=list_of(int, min_items=10, max_items=100)) def test_list_of_minimum_length(l): assert len(l) >= 10 from pytest import nonempty_list_of @pytest.mark.randomize(l=nonempty_list_of(int) def test_list_of_minimum_length(l): assert len(l) >= 1
数据类型选项正常工作:
@pytest.mark.randomize(l=list_of(str, num_items=10), choices=["a", "b", "c"]) def test_list_of(l): assert l[0] in ["a", "b", "c"]
(注意进入list_of()调用的内容和外部的内容。)
您还可以生成dict:
from pytest import dict_of @pytest.mark.randomize(d=dict_of(str, int)) def test_list_of(l): pass
Python3
对于python 3,函数的签名作为函数注释给出。
@pytest.mark.randomize(min_num=0, max_num=2, ncalls=5) def test_generate_int_anns(i1: int): pass
混合表示也可以,但它可能不有用。
@pytest.mark.randomize(i1=int, fixed_length=8) def test_generate_arg_anns_mixed(i1, s1: str): pass
向后兼容性
在0.6版本下,类型由包含名称的字符串指定 类型的。如果你愿意的话,它仍然是支持的。
@pytest.mark.randomize(("i1", "int"), ("i2", "int"), ncalls=1)
变更日志
0.8.3(2017-05-27)
- 修复与pytest-3.1.0相关的问题
- 放弃支持Python2.6和3.2
0.8.2(2015-03-02)
- 将代码存储库转移到pytest dev
0.8.1(2014-12-25)
- 支持str数据类型的最小长度
- 已删除分发依赖项
- 添加pytest薄片测试
0.8(2013-12-08)
0.7(2012-10-20)
- 参数中的类型由类型本身指定(1)
0.6(2012-03-29)
- 添加从函数注释生成数据的功能
0.5(2012-03-18)
- 首次发布