use*_*361 6 python integration-testing pytest
我是新手,所以如果问题不够具体,请不要介意。
我想知道如何将单元测试合并到 pytest 中的单个集成测试中。此外,我想在单个测试会话中重复集成测试几次。请让我知道是否有办法在 pytest 中做到这一点。
场景:我有两个单元测试名称 test_start_call 和 test_end_call 由 pytest 按该顺序调用。
现在我想重复这个过程几次,所以我这样做了:
对于范围内的 i(0,c):pytest.main(some command)
它工作正常,它将开始测试会话并根据需要多次拆除测试会话,在每个测试会话中进行一次调用。
但是我想在一个测试会话中打几个电话,到目前为止,自从过去两天以来,我还没有找到任何方法来做到这一点。我尝试查看 xdist,但我不想并行启动新进程。集成测试应该在单个测试会话中根据需要多次连续执行单元测试(开始调用和结束调用)。
我被困住了。所以任何帮助都会很棒。谢谢!
查看https://docs.pytest.org/en/latest/parametrize.html
然后将 mult 标记添加到每个测试中,并在钩子pytest_generate_tests中使用它,以提供多个测试固定值将在 --collect-only --mult 3 中可见。以这种方式使用标记将把多个测试机制限制为仅标记的测试。
# conftest
def pytest_addoptions(parser):
parser.addoption('--mult', default=0, help="run many tests")
def pytest_generate_tests(metafunc):
count = int(metafunc.config.getoption('--mult'))
if count and metafunc.get_closest_marker('mult'):
if 'mult' not in metafunc.fixturenames:
metafunc.fixturenames.append('mult')
metafunc.parametrize("mult", range(count))
# testfoo
@pytest.mark.mult
def test_start_call():
...
Run Code Online (Sandbox Code Playgroud)