Skip to main content

将 fauxfactory 集成到 pytest 中。

项目描述

pytest-fauxfactory
==================

.. image:: https://img.shields.io/pypi/v/pytest-fauxfactory.svg
:target: https: //pypi.python.org/pypi/pytest-fauxfactory

.. 图片:: https://img.shields.io/pypi/l/pytest-fauxfactory.svg
:target: https://pypi.python.org/ pypi/pytest-fauxfactory

.. 图片:: https://img.shields.io/pypi/pyversions/pytest-fauxfactory.svg
:target: https://pypi.python.org/pypi/pytest-fauxfactory

.. 图片:: https://travis-ci.org/omaciel/pytest-fauxfactory.svg?branch=master
:target: https://travis-ci.org/omaciel/pytest-fauxfactory

--------- ------

**pytest-fauxfactory** 是一个 **Pytest** 插件,可帮助您将随机数据传递给自动化测试,利用
`FauxFactory https://github.com/omaciel/fauxfactory`的力量。

特性
--------

pytest-fauxfactory 让您创建参数化的自动化测试,提供:

- 通过 **FauxFactory** 随机生成的字符串
- 允许您提供“可调用”方法来返回数据项的类型和数量供您的测试使用
- 允许您提供“生成器”方法来返回您的测试要使用的数据项的类型和数量

安装
------------


::

$ pip install pytest -fauxfactory


使用示例
--------------


生成随机字符串:faux_string
+++++++++++++++++++++++++++ +++++++++++++
假设您需要为测试生成一个随机字符串值(标识为 **author**)

.. code-block:: python

@pytest.mark.faux_string()
def test_generate_alpha_strings(author):
assert author

允许的类型可以生成的字符串有:

- alpha
- alphanumeric
- cjk
- html
- latin1
- numeric
- utf8
- 标点符号

使用不带任何参数的 `faux_string` 标记将为您的测试生成一个随机字符串。

::

test_generate_alpha_strings[faux_string_0] PASSED


假设您要为测试生成 **4** 随机 **alpha** 字符串(标识为 **book**):

.. code-block:: python

@pytest.mark.faux_string(4, 'alpha')
def test_generate_alpha_strings(book):
assert book.isalpha()


然后您将进行 4 个测试,每个测试都有不同的值:

::

test_generate_alpha_strings[faux_string_0] PASSED
test_generate_alpha_strings[faux_string_1] PASSED
test_generate_alpha_strings [faux_string_2] PASSED
test_generate_alpha_strings[faux_string_3] PASSED

现在,假设您还想确保所有字符串都正好有 43 个字符:

.. code-block:: python

@pytest.mark.faux_string(4, 'alpha', length=43 )
def test_generate_alpha_strings(value):
断言 len(value) == 43

此外,您可以通过传入长度列表来运行具有不同字符串长度的测试:

.. code-block:: python

@pytest.mark.faux_string(4, 'alpha', length=[5, 15])
def test_gen_alpha_string_with_variable_length( value):
"""生成一个长度为 5 或 15 的 `alpha` 字符串。"""
assert len(value) == 5 或 len(value) == 15

这将生成 4 个新测试

::

tests/test_faux_string .py::test_gen_alpha_string_with_variable_length[faux_string_0] 通过 [91%]
tests/test_faux_string.py::test_gen_alpha_string_with_variable_length[faux_string_1] PASSED [92%]
tests/test_faux_string.py::test_gen_alpha_string_with_variable_length[faux_string_2] PASSED [93%]
tests/test_faux_string.py::test_gen_alpha_string_with_variable_length[faux_string_3] PASSED

同样,你可以运行通过传入类型列表来使用不同的字符串类型:

.. code-block:: python

@pytest.mark.faux_string(4, ['alpha', 'alphanumeric'], length=[5, 10])
def test_gen_alpha_string_with_variable_types(value):
"""生成长度为 5 的 alpha 字符串,长度为 10 的字母数字字符串。"""
if len(value) == 5:
assert not contains_number(value)
else:
assert contains_number(value)

这将生成 4新测试

::

tests/test_faux_string.py::test_gen_alpha_string_with_variable_types[faux_string_0] 通过 [96%]
测试/test_faux_string.py::test_gen_alpha_string_with_variable_types[faux_string_1] PASSED [97%]
tests/test_faux_string.py::test_gen_alpha_string_with_variable_types[faux_string_2] PASSED [98%]
tests/test_faux_string.py::test_gen_alpha_string_with_variable_types[faux_string_auxcall


函数
+ PASSED ++++++++++++++++++++++++++++++++++++++
现在假设您有一个自定义函数,可以生成任何类型的值而不是使用的默认类型
`faux_string`。使用 `fauxfactory.gen_integer` 例如:

.. code-block:: python

import fauxfactory
import pytest

@pytest.mark.faux_callable(4, fauxfactory.gen_integer)
def test_callable_generate_integer(value):
"""返回生成整数的测试函数" ""
assert isinstance(value, int)


这将生成 4 个新测试

::

tests/test_pytest_fauxfactory.py::test_generate_integers[faux_callable_0] PASSED
tests/test_pytest_fauxfactory.py::test_generate_integers[faux_callable_1] PASSED
tests/test_pytest_fauxfactory.py::test_generate_integers[ faux_callable_2] 通过
tests/test_pytest_fauxfactory.py::test_generate_integers[faux_callable_3] PASSED


faux_callable 也可以将参数传递给可调用函数:

.. code-block:: python

import fauxfactory
import pytest

@pytest.mark.faux_callable(4, fauxfactory.gen_integer, min_value=0 , max_value
=100)
def test_callable_generate_integers(value):
"""Test function that return generated integer with kwargs"""
assert isinstance(value, int)
assert 0 <= value <= 100

这将生成 4 个新测试

::

tests/ test_pytest_fauxfactory.py::test_generate_integers[faux_callable_0] 通过
tests/test_pytest_fauxfactory.py::test_generate_integers[faux_callable_1] PASSED
tests/test_pytest_fauxfactory.py::test_generate_integers[faux_callable_2] PASSED
tests/test_pytest_fauxfactory.py::test_generate_integers[faux_callable_3] PASSED


当然生成的值可以是任何类型!例如,让我们将值生成为 alpha 字符串的元组:

.. code-block:: python

import fauxfactory
import pytest

def generate_alpha_strings(number=1, length=10):
"""返回生成的 alpha 字符串元组的函数" ""
return tuple(fauxfactory.gen_alpha(length=length) for _ in range(number))

@pytest.mark.faux_callable(5, generate_alpha_strings, number=3,
def test_callable_generate_from_custom_function(value):
"""测试返回生成字符串元组的通用函数"""
assert isinstance(value, tuple)
assert len(value) == 3
# 解压
位置、组织、cv =
str_alpha in ( location, organization, cv):
assert len(str_alpha) == 12
assert location != organization
assert location != cv

这将生成 5 个新测试

::

tests/test_pytest_fauxfactory.py::test_generate_from_custom_function[faux_callable_0] PASSED
tests/test_pytest_fauxfactory.py ::test_generate_from_custom_function[faux_callable_1] 已通过
tests/test_pytest_fauxfactory.py::test_generate_from_custom_function[faux_callable_2] PASSED
tests/test_pytest_fauxfactory.py::test_generate_from_custom_function[faux_callable_3] PASSED
tests/test_pytest_fauxfactory.py::test_generate_from_custom_function[faux_callable_4] PASSED


现在让我们从返回字典的自定义函数生成值

: . code-block:: python

import fauxfactory
import pytest

def generate_person():
"""生成随机人员记录。"""
return {
'name': fauxfactory.gen_alpha(length=12),
'age': fauxfactory.gen_integer (最小值=12,最大值=100)
}

@pytest.mark.faux_callable(3, generate_person)
def test_callable_generate_person(value):
"""Test generic function that return a dict"""
assert isinstance(value, dict)
assert 'name' in value
assert 'age' in value
assert len(value['name']) == 12
assert 12 <= value['age'] <= 100

这将生成 5 个新测试

::

tests/test_pytest_fauxfactory.py::test_generate_person[faux_callable_0] PASSED
tests/test_pytest_fauxfactory.py ::test_generate_person[faux_callable_1] 通过
测试/test_pytest_fauxfactory.py::test_generate_person[faux_callable_2] 通过


使用生成器:faux_generator
++++++++++++++++++++++++++++++++
现在我们想要生成具有
任何类型值的测试,而不是使用可调用函数来自生成器函数或生成器表达式。
为此,我们可以使用“faux_generator”标记:


.. code-block:: python

def alpha_strings_generator(items=1, length=10):
"""Generate alpha string value at each iteration."""
for _ in range (items):
yield fauxfactory.gen_alpha(length=length)


@pytest.mark.faux_generator(alpha_strings_generator(items=3, length=12))
def test_generator_alpha_strings(value):
"""Test function generator with kwargs."""




::

tests/test_pytest_fauxfactory.py::test_generator_alpha_strings[faux_generator_0] PASSED
tests/test_pytest_fauxfactory.py::test_generator_alpha_strings[faux_generator_1] PASSED
tests/test_pytest_fauxfactory.py::test_generator_alpha_strings[faux_generator_2] PASSED

我们也可以使用生成器表达式:

..code- block:: python

list_of_integers = [fauxfactory.gen_integer(min_value=0) for _ in range(4)]


@pytest.mark.faux_generator(int_val for int_val in list_of_integers)
def test_generator_expression(value):
"""测试生成器表达式。" ""
assert isinstance(value, int)
assert value >= 0

这将生成 4 个测试

::

测试/test_pytest_fauxfactory.py::test_generator_expression[faux_generator_0] 通过
测试/test_pytest_fauxfactory.py::test_generator_expression[faux_generator_1] 通过测试
/test_pytest_fauxfactory.py::test_generator_expression[faux_generator_2] 通过
测试/test_pytest_fauxfactory.py::test_generator_expression[faux_generator] 通过


返回值可以是任何类型:


.. code-block:: python

def foo_generator():
"""返回不同的值:首先,一个字符串 'foo';第二次迭代,一个
整数列表。"""
yield ' foo'
产量 [1, 2, 3]


@pytest.mark。faux_generator(foo_generator())
def test_generator_foo_generator(value):
"""测试不同类型的值。"""
if isinstance(value, list):
assert value == [1, 2, 3]
else:
assert value == 'foo'


这将生成 2 个测试

::

tests/test_pytest_fauxfactory。 py::test_generator_foo_generator[faux_generator_0] PASSED
tests/test_pytest_fauxfactory.py::test_generator_foo_generator[faux_generator_1] PASSED

我们也可以结合以上所有的生成器:

.. code-block:: python

@pytest.mark.faux_generator(
alpha_strings_generator(items=3, length=12),
(int_val for int_val in list_of_integers),
foo_generator()
)
def test_generator_combined(value):
"""测试组合生成器。"""
if isinstance(value, list):
assert value == [1, 2, 3]
elif isinstance(value, int):
assert value >= 0
else:
assert value.isalpha()

这将生成 9 个测试

::

tests/test_pytest_fauxfactory.py::test_generator_combined[faux_generator_0] PASSED
tests/test_pytest_fauxfactory.py::test_generator_combined[faux_generator_1] PASSED
tests/test_pytest_fauxfactory.py::test_generator_combined[faux_generator_2] PASSED
tests/test_pytest_fauxfactory.py: test_generator_combined[faux_generator_3] 通过
测试/test_pytest_fauxfactory.py::test_generator_combined[faux_generator_4] PASSED
测试/test_pytest_fauxfactory.py::test_generator_combined[faux_generator_5] PASSED 测试/test_pytest_fauxfactory.py::test_generator_combined[faux_generator_6] PASSED测试 /
test_pytest_fauxfactory
测试.py::test_generator_com
test_pytest_fauxfactory.py::test_generator_combined[faux_generator_8] PASSED


文档
-------------

文档正在编写中,但我们很乐意从社区获得帮助!

作者
=======

pytest-fauxfactory 由 Og Maciel 和各种
贡献者编写和维护。

开发主管
----------------

- Og Maciel `@omaciel <https://github.com/omaciel/>`_

贡献者
------------

- Djebran Lezzoum `@ldjebran https://github.com/ldjebran`_
- Jacob Callahan `@JacobCallahan https://github.com/JacobCallahan`_
- Milan Falešník `@mfalesni https://github.com/mfalesni`_


.. :changelog:

Release History
========= ======

1.1.0 (2017-12-06)
------------------

- 将 `gen_string` 标记重命名为具有扩展功能的 `faux_string`
- 添加了新的`faux_callable` 和 `faux_generator` 标记
- 更新了 README 文件以包含可用示例

1.0.1 (2017-10-18)
------------------

- 作为 Pytest 首次发布暴露 `gen_string` 标记的插件
- 可以用新标记装饰测试以生成随机字符串作为参数

1.0.0 (2015-05-20)
------------------

- 首次发布。


项目详情


下载文件

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

源分布

pytest-fauxfactory-1.1.1.tar.gz (23.4 kB 查看哈希

已上传 source

内置分布

pytest_fauxfactory-1.1.1-py3-none-any.whl (11.5 kB 查看哈希

已上传 py3