据我所知,您可以参数化测试以使用不同的参数集重复测试。另外我知道测试文件中的不同测试可以使用-n
并行运行,但我想并行执行同一组测试。在pytest中有可能吗?
例如:
import pytest
@pytest.fixture()
def user_number(worker_id):
return "user number : %s" %worker_id
def test_add(user_number):
print("Adding 1+1 and returning the result and user number: {}".format(user_number))
return 1+1
def test_subtract():
print("subtracting 2-1 and returning the result and user number: {}".format(user_number))
return 2-1
如果我运行以下命令: py.test -n 3 -s -v parallel_users.py
在结果中,test_add()和test_subtract()并行运行,如下所示:
[gw1] PASSED parallel_users.py::test_subtract
[gw0] PASSED parallel_users.py::test_add
如何让test_add()和test_subtract()运行两次,如下所示:
[gw1] PASSED parallel_users.py::test_add, test_subtract
[gw0] PASSED parallel_users.py::test_add, test_subtract
答案 0 :(得分:2)
要重复执行测试,请在conftest.py
:
def pytest_collection_modifyitems(items):
numrepeats = 2
items.extend(items * (numrepeats - 1))
这将复制为执行numrepeats
次收集的每个测试。示例运行:
$ pytest test_spam.py -v -n3
============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-3.4.2, py-1.5.3, pluggy-0.6.0 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /private/tmp, inifile:
plugins: xdist-1.22.2, forked-0.2, dependency-0.3.2, cov-2.5.1
[gw0] darwin Python 3.6.4 cwd: /private/tmp
[gw1] darwin Python 3.6.4 cwd: /private/tmp
[gw2] darwin Python 3.6.4 cwd: /private/tmp
[gw0] Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) -- [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
[gw1] Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) -- [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
[gw2] Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) -- [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
gw0 [4] / gw1 [4] / gw2 [4]
scheduling tests via LoadScheduling
test_spam.py::test_add
test_spam.py::test_subtract
test_spam.py::test_add
[gw0] [ 25%] PASSED test_spam.py::test_add
[gw1] [ 50%] PASSED test_spam.py::test_subtract
[gw2] [ 50%] PASSED test_spam.py::test_add
test_spam.py::test_subtract
[gw0] [ 50%] PASSED test_spam.py::test_subtract
=========================== 4 passed in 0.63 seconds ===========================
如果要使其可配置,请添加自定义cli参数:
import pytest
def pytest_addoption(parser):
parser.addoption('--numrepeats', action='store', type=int, default=1)
def pytest_collection_modifyitems(items):
numrepeats = pytest.config.getoption('--numrepeats')
items.extend(items * (numrepeats - 1))
现在,您可以使用--numrepeats
来调用您的测试,例如pytest --numrepeats 5
。
至于每个流程的批处理测试(问题的第二部分),pytest-xdist
尚不支持,请参阅this issue以及与之关联的所有内容。最近,添加了一些基本的支持,例如在单个模块或类中在单独的进程中执行测试:
--dist=distmode set mode for distributing tests to exec environments.
each: send each test to all available environments.
load: load balance by sending any pending test to any
available environment. loadscope: load balance by
sending pending groups of tests in the same scope to
any available environment. loadfile: load balance by
sending test grouped by file to any available
environment. (default) no: run tests inprocess, don't
distribute.
但是,如果您想根据某些自定义条件对测试进行负载均衡,除了编写自己的调度程序之外别无他法。
答案 1 :(得分:0)
实现此目的的一种最简单方法是欺骗py.test以假设使用pytest.mark.parametrization
进行多种测试组合。在这里,我只是使用虚拟参数count
来欺骗py.test,有多个测试变体。这是最简单的方法,如果您有更多的测试条件,可以使用pytest_generate_tests(metafunc):
来微调您的需求。
import pytest
@pytest.fixture()
def user_number(worker_id):
return "user number : %s" %worker_id
@pytest.mark.parametrize("count", [1, 2,3])
def test_add(user_number, count):
print("Adding 1+1 and returning the result and user number: {}".format(user_number))
return 1+1
@pytest.mark.parametrize("count", [1, 2 ,3])
def test_subtract(count):
print("subtracting 2-1 and returning the result and user number: {}".format(user_number))
return 2-1
$ py.test test.py -n 2 -s -vv
======================================== test session starts ========================================
platform darwin -- Python 2.7.13, pytest-2.9.2, py-1.5.3, pluggy-0.3.1 -- bin/python2.7
cachedir: .cache
rootdir: , inifile:
plugins: xdist-1.17.1, repeat-0.4.1, cov-1.8.1
[gw0] darwin Python 2.7.13 cwd:
[gw1] darwin Python 2.7.13 cwd:
[gw1] Python 2.7.13 (default, Dec 17 2016, 23:03:43) -- [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
[gw0] Python 2.7.13 (default, Dec 17 2016, 23:03:43) -- [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
gw0 [6] / gw1 [6]
scheduling tests via LoadScheduling
test.py::test_add[1]
test.py::test_add[2]
[gw1] PASSED test.py::test_add[2]
[gw0] PASSED test.py::test_add[1]
test.py::test_subtract[1]
[gw1] PASSED test.py::test_subtract[1]
test.py::test_add[3]
[gw0] PASSED test.py::test_add[3]
test.py::test_subtract[3]
test.py::test_subtract[2]
[gw0] PASSED test.py::test_subtract[3]
[gw1] PASSED test.py::test_subtract[2]