我是新手,所以如果问题不够具体,请不要介意。
我想知道如何将单元测试组合成 pytest 中的单个集成测试。 此外,我想在单个测试 session 中重复集成测试几次。如果有办法在 pytest 中执行此操作,请告诉我。
场景:
我有两个单元测试名称 test_start_call
和 test_end_call
由 pytest 按此顺序调用。
现在我想重复这个过程几次,所以我这样做了:
for i in range(0,c):
pytest.main(some command)
它工作正常,它将启动测试 session 并根据需要多次终止测试 session ,每个测试 session 中进行一次调用。
但我想在一个测试 session 中进行多次调用,自从过去两天以来我还没有找到任何方法来执行此操作。我尝试查看 xdist 但我不想并行启动新进程。集成测试应在单个测试 session 中连续执行单元测试(开始调用和结束调用)多次。
我卡住了。所以任何帮助都会很棒。谢谢!
最佳答案
评论 https://docs.pytest.org/en/latest/parametrize.html
然后向每个测试添加 mult 标记并在钩子(Hook) pytest_generate_tests 中使用它以提供多个测试 fixture 值将在 --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():
...
关于python - 如何使用 pytest 编写集成测试以及如何重复集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29021361/