pytest-quickcheck


Namepytest-quickcheck JSON
Version 0.8.5 PyPI version JSON
download
home_pagehttps://bitbucket.org/pytest-dev/pytest-quickcheck
Summarypytest plugin to generate random data inspired by QuickCheck
upload_time2020-09-19 07:03:40
maintainer
docs_urlNone
authorTetsuya Morimoto
requires_python
licenseApache License 2.0
keywords test pytest quickcheck
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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.

.. _string module: http://docs.python.org/library/string.html

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`_

.. _PEP 3107 -- Function Annotations: http://www.python.org/dev/peps/pep-3107/

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.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



            

Raw data

            {
    "_id": null,
    "home_page": "https://bitbucket.org/pytest-dev/pytest-quickcheck",
    "name": "pytest-quickcheck",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "test,pytest,quickcheck",
    "author": "Tetsuya Morimoto",
    "author_email": "tetsuya.morimoto@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/61/bd/aea592dc53e30bc6c6f4372d878621d75167ba55febc967a739605f3602e/pytest-quickcheck-0.8.5.tar.gz",
    "platform": "linux",
    "description": "Requirements\n------------\n\n* Python 2.7 or 3.6 and later\n\nFeatures\n--------\n\n* Provide **pytest.mark.randomize** function for generating random test data\n\nInstallation\n============\n\n::\n\n    $ pip install pytest-quickcheck\n\nQuick Start\n===========\n\nJust pass the signature of function to *randomize* marker.\nThe signature is represented a tuple consist of argument name and its type.\n\n::\n\n    @pytest.mark.randomize(i1=int, i2=int, ncalls=1)\n    def test_generate_ints(i1, i2):\n        pass\n\nMore complex data structure::\n\n    @pytest.mark.randomize(\n        d1={'x': int, 'y': [str, (int, int)], 'z': {'x': str}}\n    )\n    def test_generate_dict(d1):\n        pass\n\nThe *randomize* marker is able to use with *parametrize* marker.\n\n::\n\n    @pytest.mark.parametrize(\"prime\", [2, 3, 5])\n    @pytest.mark.randomize(i1=int, f1=float, ncalls=1)\n    def test_gen_parametrize_with_randomize_int_float(prime, i1, f1):\n        pass\n\nUsing command line option ``--randomize`` restricts only the *randomize* test.\n\n::\n\n    $ py.test -v --randomize test_option.py \n    ==========================================================================================\n    test session starts\n    ==========================================================================================\n    test_option.py:5: test_normal SKIPPED\n    test_option.py:8: test_generate_ints[74-22] PASSED\n\nUsage\n=====\n\nThere some options for each data type::\n\n    $ py.test --markers\n    @pytest.mark.randomize(argname=type, **options): mark the test function with\n    random data generating any data type.\n      There are options for each data type: (see doc for details)\n      int: ['min_num', 'max_num']\n      float: ['min_num', 'max_num', 'positive']\n      str: ['encoding', 'fixed_length', 'min_length', 'max_length', 'str_attrs']\n      list_of, nonempty_list_of, dict_of: ['items', 'min_items', 'max_items']\n\n* common option\n\n  | **ncalls**: set the number of calls. Defaults to 3. (e.g. ncalls=5)\n  | **choices**: choose from given sequence. (e.g. choices=[3, 5, 7])\n\n* int\n\n  | **min_num**: lower limit for generating integer number. (e.g. min_num=0)\n  | **max_num**: upper limit for generating integer number. (e.g. max_num=10)\n\n* float\n\n  | **min_num**: lower limit for generating real number. (e.g. min_num=0.0)\n  | **max_num**: upper limit for generating real number. (e.g. max_num=1.0)\n  | **positive**: generate only positive real number if set to `True`.\n    Defaults to `False`. (e.g. positive=True)\n\n* str\n\n  | **encoding**: generate unicode string encoded given character code.\n    (e.g. encoding=\"utf-8\")  # for Python 2.x only\n  | **fixed_length**: generate fixed length string. (e.g. fixed_length=8)\n  | **max_length**: generate the string less than or equal to max length\n    (e.g. max_length=32)\n  | **str_attrs**: generate the string in given letters.\n    set a tuple consist of attribute names in the `string module`_.\n    (e.g. str_attrs=(\"digits\", \"punctuation\")\n\n* list_of, nonempty_list_of, dict_of\n\n  | **items**: number of items.\n  | **min_items**: lower limit on number of items.\n  | **max_items**: upper limit on number of items.\n\nProbably, `tests/test_plugin_basic.py` is useful for\nlearning how to use these options.\n\n.. _string module: http://docs.python.org/library/string.html\n\nGenerating Collections\n======================\n\nTo generate a variable length list of items::\n\n    from pytest import list_of\n\n    @pytest.mark.randomize(l=list_of(int))\n    def test_list_of(l):\n        pass\n\nYou can control its size with the ``items``, ``min_items`` and\n``max_items`` options, or use the ``nonempty_list_of`` shortcut.\n\n::\n\n    @pytest.mark.randomize(l=list_of(int, num_items=10))\n    def test_list_of_length(l):\n        assert len(l) == 10\n\n    @pytest.mark.randomize(l=list_of(int, min_items=10, max_items=100))\n    def test_list_of_minimum_length(l):\n        assert len(l) >= 10\n\n    from pytest import nonempty_list_of\n\n    @pytest.mark.randomize(l=nonempty_list_of(int)\n    def test_list_of_minimum_length(l):\n        assert len(l) >= 1\n\nOptions for data types work as usual::\n\n    @pytest.mark.randomize(l=list_of(str, num_items=10), choices=[\"a\", \"b\", \"c\"])\n    def test_list_of(l):\n        assert l[0] in [\"a\", \"b\", \"c\"]\n\n(Note what goes into the ``list_of()`` call and what goes outside.)\n\nYou can also generate a dict::\n\n    from pytest import dict_of\n    @pytest.mark.randomize(d=dict_of(str, int))\n    def test_list_of(l):\n        pass\n\n\nPython 3\n========\n\nFor Python 3, the signature of function is given as function annotation.\n\n::\n\n    @pytest.mark.randomize(min_num=0, max_num=2, ncalls=5)\n    def test_generate_int_anns(i1: int):\n        pass\n\nMixed representation is also OK, but it might not be useful. \n\n::\n\n    @pytest.mark.randomize(i1=int, fixed_length=8)\n    def test_generate_arg_anns_mixed(i1, s1: str):\n        pass\n\nSee also: `PEP 3107 -- Function Annotations`_\n\n.. _PEP 3107 -- Function Annotations: http://www.python.org/dev/peps/pep-3107/\n\nBackward Compatibility\n======================\n\nUnder 0.6 version, types were specified by strings containing the name\nof the type. It's still supported if you like.\n\n::\n\n    @pytest.mark.randomize((\"i1\", \"int\"), (\"i2\", \"int\"), ncalls=1)\n\nChangeLog\n=========\n\n0.8.5 (2020-09-19)\n------------------\n\n* fix a critical issue pytest cannot detect randomize marker\n* drop supporting pytest < 4.0.0\n* drop supporting python 3.5\n\n0.8.4 (2020-03-06)\n------------------\n\n* fix an issue related to pytest-4.x/5.x\n* drop supporting python 3.3 and 3.4\n\n0.8.3 (2017-05-27)\n------------------\n\n* fix an issue related to pytest-3.1.0\n* drop supporting python 2.6 and 3.2\n\n0.8.2 (2015-03-02)\n------------------\n\n* transfer the code repository to pytest-dev\n\n0.8.1 (2014-12-25)\n------------------\n\n* support min_length for str data type\n* removed distribute dependency\n* add pytest-flakes testing\n\n0.8 (2013-12-08)\n----------------\n\n* fix use the parameter length for string generator even if the set of\n  available characters is less than it (#2)\n\n* support new feature: Generating Collections from sonoflilit\n\n0.7 (2012-10-20)\n----------------\n\n* the types in the arguments are specified by the types themselves (#1)\n\n0.6 (2012-03-29)\n----------------\n* add generating data feature from function annotation\n\n0.5 (2012-03-18)\n----------------\n* first release\n\n\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "pytest plugin to generate random data inspired by QuickCheck",
    "version": "0.8.5",
    "split_keywords": [
        "test",
        "pytest",
        "quickcheck"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "60f1ba17fa9448c9173671d13e791b71",
                "sha256": "f0f3cd4346f6a22f6e2970fe6dfe74ce58df8e789806970dc8b5bd6bba9853a1"
            },
            "downloads": -1,
            "filename": "pytest_quickcheck-0.8.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "60f1ba17fa9448c9173671d13e791b71",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 11949,
            "upload_time": "2020-09-19T07:03:38",
            "upload_time_iso_8601": "2020-09-19T07:03:38.527598Z",
            "url": "https://files.pythonhosted.org/packages/a0/24/a20db45e84d17e175045959f55e951d71c5aff46739bf0946d1fa06b91c2/pytest_quickcheck-0.8.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "b3ee10bacb2afcb5e0688b2c4c0febda",
                "sha256": "2427808b54ccdec26a40cdba934a6c042fab9ebadb60d563a01f367bef87fe58"
            },
            "downloads": -1,
            "filename": "pytest-quickcheck-0.8.5.tar.gz",
            "has_sig": false,
            "md5_digest": "b3ee10bacb2afcb5e0688b2c4c0febda",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 19199,
            "upload_time": "2020-09-19T07:03:40",
            "upload_time_iso_8601": "2020-09-19T07:03:40.070789Z",
            "url": "https://files.pythonhosted.org/packages/61/bd/aea592dc53e30bc6c6f4372d878621d75167ba55febc967a739605f3602e/pytest-quickcheck-0.8.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-09-19 07:03:40",
    "github": false,
    "gitlab": false,
    "bitbucket": true,
    "bitbucket_user": null,
    "bitbucket_project": "pytest-dev",
    "lcname": "pytest-quickcheck"
}
        
Elapsed time: 0.13405s