Skip to main content

Requirements

  • Python 2.7 or 3.6 and later

Features

  • Provide pytest.mark.randomize function for generating random test data

Installation

$ pip install pytest-quickcheck

Quick Start

Just pass the signature of function to randomize marker. The signature is represented a tuple consist of argument name and its type.

@pytest.mark.randomize(i1=int, i2=int, ncalls=1)
def test_generate_ints(i1, i2):
    pass

More complex data structure:

@pytest.mark.randomize(
    d1={'x': int, 'y': [str, (int, int)], 'z': {'x': str}}
)
def test_generate_dict(d1):
    pass

The randomize marker is able to use with parametrize marker.

@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

Using command line option --randomize restricts only the randomize test.

$ 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

Usage

There some options for each data type:

$ 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']
  • common option

    ncalls: set the number of calls. Defaults to 3. (e.g. ncalls=5)
    choices: choose from given sequence. (e.g. choices=[3, 5, 7])
  • int

    min_num: lower limit for generating integer number. (e.g. min_num=0)
    max_num: upper limit for generating integer number. (e.g. max_num=10)
  • float

    min_num: lower limit for generating real number. (e.g. min_num=0.0)
    max_num: upper limit for generating real number. (e.g. max_num=1.0)
    positive: generate only positive real number if set to True. Defaults to False. (e.g. positive=True)
  • str

    encoding: generate unicode string encoded given character code. (e.g. encoding=”utf-8”) # for Python 2.x only
    fixed_length: generate fixed length string. (e.g. fixed_length=8)
    max_length: generate the string less than or equal to max length (e.g. max_length=32)
    str_attrs: generate the string in given letters. set a tuple consist of attribute names in the string module. (e.g. str_attrs=(“digits”, “punctuation”)
  • list_of, nonempty_list_of, dict_of

    items: number of items.
    min_items: lower limit on number of items.
    max_items: upper limit on number of items.

Probably, tests/test_plugin_basic.py is useful for learning how to use these options.

Generating Collections

To generate a variable length list of items:

from pytest import list_of

@pytest.mark.randomize(l=list_of(int))
def test_list_of(l):
    pass

You can control its size with the items, min_items and max_items options, or use the nonempty_list_of shortcut.

@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

Options for data types work as usual:

@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"]

(Note what goes into the list_of() call and what goes outside.)

You can also generate a dict:

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

Python 3

For Python 3, the signature of function is given as function annotation.

@pytest.mark.randomize(min_num=0, max_num=2, ncalls=5)
def test_generate_int_anns(i1: int):
    pass

Mixed representation is also OK, but it might not be useful.

@pytest.mark.randomize(i1=int, fixed_length=8)
def test_generate_arg_anns_mixed(i1, s1: str):
    pass

See also: PEP 3107 – Function Annotations

Backward Compatibility

Under 0.6 version, types were specified by strings containing the name of the type. It’s still supported if you like.

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

ChangeLog

0.8.6 (2020-11-15)

  • fix ignored ncalls parameter when a function annotation is used

  • change to be able to use the same argument in randomize marker and function annotation

0.8.5 (2020-09-19)

  • fix a critical issue pytest cannot detect randomize marker

  • drop supporting pytest < 4.0.0

  • drop supporting python 3.5

0.8.4 (2020-03-06)

  • fix an issue related to pytest-4.x/5.x

  • drop supporting python 3.3 and 3.4

0.8.3 (2017-05-27)

  • fix an issue related to pytest-3.1.0

  • drop supporting python 2.6 and 3.2

0.8.2 (2015-03-02)

  • transfer the code repository to pytest-dev

0.8.1 (2014-12-25)

  • support min_length for str data type

  • removed distribute dependency

  • add pytest-flakes testing

0.8 (2013-12-08)

  • fix use the parameter length for string generator even if the set of available characters is less than it (#2)

  • support new feature: Generating Collections from sonoflilit

0.7 (2012-10-20)

  • the types in the arguments are specified by the types themselves (#1)

0.6 (2012-03-29)

  • add generating data feature from function annotation

0.5 (2012-03-18)

  • first release

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-quickcheck-0.8.6.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_quickcheck-0.8.6-py2.py3-none-any.whl (12.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-quickcheck-0.8.6.tar.gz.

File metadata

  • Download URL: pytest-quickcheck-0.8.6.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.10

File hashes

Hashes for pytest-quickcheck-0.8.6.tar.gz
Algorithm Hash digest
SHA256 3ef9bde7ba1fe6470c5b61631440186d1254e276c67a527242d91451ab7994e5
MD5 3d67d498dddf6719b7ebb9f1cc69f7cc
BLAKE2b-256 53998522d4778d1db56d4f379b8664f797c5432fac0c19c1c6247ee5a8412de2

See more details on using hashes here.

File details

Details for the file pytest_quickcheck-0.8.6-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_quickcheck-0.8.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.10

File hashes

Hashes for pytest_quickcheck-0.8.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ebf2e41c4529f56ddf0bf190e4972d575033e0cb4f1e56c66b5653a07c92a27b
MD5 6439de70300aae75b572a394e42664d4
BLAKE2b-256 f0dc319fb8ea005decc43ee0f41f8aff33bbfe7cff737c6fdad765c30923565f

See more details on using hashes here.

Release history Release notifications | RSS feed

0.9.0

2 files

This release

0.8.6 This release

2 files

0.8.5

2 files

0.8.4

1 file

0.8.3

1 file

0.8.2

1 file

0.8.1

1 file

0.8

1 file

0.7

1 file

0.6

1 file

0.5

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page