跳转到主要内容

pytest插件,用于生成受QuickCheck启发的随机数据

项目描述

要求

  • Python 2.7或3.7及以上

功能

  • 提供 pytest.mark.randomize 函数以生成随机测试数据

安装

$ 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 限制仅对 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)
    choices:从给定序列中选择。(例如:choices=[3, 5, 7])
  • int

    min_num:生成整数数字的下限。(例如:min_num=0)
    max_num:生成整数数字的上限。(例如:max_num=10)
  • float

    min_num:生成实数的下限。(例如:min_num=0.0)
    max_num:生成实数的上限。(例如:max_num=1.0)
    positive:如果设置为 True,则仅生成正实数。默认为 False。(例如:positive=True)
  • str

    encoding:根据给定的字符编码生成Unicode字符串。(例如:encoding="utf-8")# 仅适用于Python 2.x
    fixed_length: 生成固定长度的字符串。 (例如 fixed_length=8)
    max_length: 生成小于或等于最大长度的字符串 (例如 max_length=32)
    str_attrs: 使用给定的字母生成字符串。设置一个由属性名称组成的元组,在 string 模块 中。 (例如 str_attrs=("digits", "punctuation")
  • list_of, nonempty_list_of, dict_of

    items: 项目数量。
    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

您可以使用 itemsmin_itemsmax_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() 调用中包含什么,以及什么在外部。)

您还可以生成一个字典

from pytest import dict_of
@pytest.mark.randomize(d=dict_of(str, int))
def test_list_of(l):
    pass

Python 3

对于 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

另请参阅:PEP 3107 – 函数注解

向后兼容性

在 0.6 版本之前,类型是通过包含类型名称的字符串指定的。如果您喜欢,仍然支持。

@pytest.mark.randomize(("i1", "int"), ("i2", "int"), ncalls=1)

更改日志

0.9.0 (2022-11-06)

  • 支持 pytest > 6.0

  • 停止支持 Python 3.6

0.8.6 (2020-11-15)

  • 修复在使用函数注解时忽略 ncalls 参数的问题

  • 修改为能够在随机化标记和函数注解中使用相同的参数

0.8.5 (2020-09-19)

  • 修复了一个关键问题:pytest 无法检测到随机化标记

  • 停止支持 pytest < 4.0.0

  • 停止支持 Python 3.5

0.8.4 (2020-03-06)

  • 修复与 pytest-4.x/5.x 相关的问题

  • 停止支持 Python 3.3 和 3.4

0.8.3 (2017-05-27)

  • 修复与 pytest-3.1.0 相关的问题

  • 停止支持 Python 2.6 和 3.2

0.8.2 (2015-03-02)

  • 将代码仓库转移到 pytest-dev

0.8.1 (2014-12-25)

  • 支持 str 数据类型的 min_length

  • 移除 distribute 依赖

  • 添加 pytest-flakes 测试

0.8 (2013-12-08)

  • 修复即使可用的字符集小于参数长度,也使用参数长度进行字符串生成的问题 (#2)

  • 支持从 sonoflilit 生成集合的新功能

0.7 (2012-10-20)

  • 参数中的类型由类型本身指定 (#1)

0.6 (2012-03-29)

  • 添加从函数注解生成数据的新功能

0.5 (2012-03-18)

  • 第一个版本

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

pytest-quickcheck-0.9.0.tar.gz (19.6 kB 查看哈希值)

上传时间 源代码

构建分发

pytest_quickcheck-0.9.0-py3-none-any.whl (12.2 kB 查看哈希值)

上传于 Python 3

由以下支持