Python pytest中的测试用例执行顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17571438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:33:41  来源:igfitidea点击:

Test case execution order in pytest

pythonpytest

提问by Kocka

I am using pytest. I have two files in a directory. In one of the files there is a long running test case that generates some output. In the other file there is a test case that reads that output. How can I ensure the proper execution order of the two test cases? Is there any alternative other than puting the test cases in the same file in the proper order?

我正在使用 pytest。我在一个目录中有两个文件。在其中一个文件中,有一个长时间运行的测试用例会生成一些输出。在另一个文件中有一个读取该输出的测试用例。如何确保两个测试用例的正确执行顺序?除了以正确的顺序将测试用例放在同一个文件中之外,还有其他选择吗?

采纳答案by Frank T

In general you can configure the behavior of basically any part of pytest using its well-specified hooks.

通常,您可以使用其明确指定的 hooks配置 pytest 基本上任何部分的行为。

In your case, you want the "pytest_collection_modifyitems" hook, which lets you re-order collected tests in place.

在您的情况下,您需要“pytest_collection_modifyitems”挂钩,它可以让您重新排序收集的测试到位。

That said, it does seem like ordering your tests should be easier -- this is Python after all! So I wrote a plugin for ordering tests: "pytest-ordering". Check out the docsor install it from pypi. Right now I recommend using @pytest.mark.first and @pytest.mark.second, or one of the @pytest.mark.order# markers, but I have some ideas about more useful APIs. Suggestions welcome :)

也就是说,订购测试似乎应该更容易——毕竟这是 Python!所以我写了一个用于排序测试的插件:"pytest-ordering"。查看文档或从pypi安装它。现在我建议使用@pytest.mark.first 和@pytest.mark.second,或@pytest.mark.order# 标记之一,但我对更有用的API 有一些想法。欢迎提出建议:)

回答by asterio gonzalez

Maybe you can consider using dependencypytest plugin, where you can set the test dependencies easily:

也许你可以考虑使用dependencypytest plugin,你可以在其中轻松设置测试依赖项:

@pytest.mark.dependency()
def test_long():
    pass

@pytest.mark.dependency(depends=['test_long'])
def test_short():
    pass

This way test_shortwill only execute if test_longis success and force the execution sequenceas well.

这种方式test_short只会在test_long成功时执行并强制执行顺序

回答by funky-future

There's also a plugin pytest-orderingthat seems to meet your requirements.

还有一个插件pytest-ordering似乎可以满足您的要求。

回答by Ali Zwd

Try this:

尝试这个:

@pytest.fixture(xxx)
def test_A():
    pass
    yield
    pass

@pytest.mark.usefixtures('test_A')
def test_B():
    pass

回答by zhihao he

main.py:

主要.py:

import functools
import pytest
from demo import test_foo,test_hi

def check_depends(depends):
    try:
        for dep in depends:
            dep()
    except Exception as e:
        return dep
    else:
        return True

def pytest_depend(depends):
    def pytest_depend_decorator(func):
        stat = check_depends(depends)
        if stat is True:
            return func
        else:
            return pytest.mark.skip(True, reason="%s[skip] --> %s[Failed]" % (func.__name__, stat.__name__))(func)
    return pytest_depend_decorator


@pytest_depend([test_foo,test_hi])
def test_bar():
    pass

@pytest_depend([test_foo,test_hi])
def test_bar2():
    pass

demo.py:

演示.py:

def test_hi():
    pass
def test_foo():
    assert False


platform linux -- Python 3.5.2, pytest-3.8.2, py-1.6.0, pluggy-0.7.1 -- /usr/bin/python3

linux平台——Python 3.5.2、pytest-3.8.2、py-1.6.0、pluggy-0.7.1——/usr/bin/python3

pytest -vrsx ./plugin.py

pytest -vrsx ./plugin.py

回答by Amol Kumar

Make Sure you have installed pytest-ordering package. To confirm go to Pycharm settings>>Project Interpreter >> and look for pytest-ordering : If it is not available install it. Pycharm settings>>Project Interpreter >> Click on + button and search pytest-ordering install it. Voila!! It will definitely work.

确保你已经安装了 pytest-ordering 包。要确认转到 Pycharm 设置>>项目解释器 >> 并查找 pytest-ordering :如果不可用,请安装它。Pycharm设置>>项目解释器>>点击+按钮并搜索pytest-ordering安装它。瞧!!它肯定会起作用。

回答by Ravichandran K

Make use of the '--randomly-dont-reorganize'option or '-p no:randomly'available in pytest-randomlyplugin, this will just run your test in the same order as you mentioned in your module.

使用pytest-randomly插件中可用的“--randomly-dont-reorganize”选项或“-p no:randomly”,这将按照您在模块中提到的相同顺序运行您的测试。

Module:

模块:

import pytest

def test_three():
    assert True

def test_four():
    assert True

def test_two():
    assert True

def test_one():
    assert True

Execution:

执行:

(tmp.w95BqE188N) rkalaiselvan@dev-rkalaiselvan:~/$ py.test --randomly-dont-reorganize test_dumm.py
======================================================================== test session starts ========================================================================
platform linux2 -- Python 2.7.12, pytest-3.10.1, py-1.5.4, pluggy-0.7.1 -- /tmp/tmp.w95BqE188N/bin/python2
cachedir: .pytest_cache
Using --randomly-seed=1566829391
rootdir: /home/rkalaiselvan, inifile: pytest.ini
plugins: randomly-1.2.3, timeout-1.3.1, cov-2.6.0, mock-1.10.0, ordering-0.6
collected 4 items

test_dumm.py::test_three PASSED
test_dumm.py::test_four PASSED
test_dumm.py::test_two PASSED
test_dumm.py::test_one PASSED

(tmp.w95BqE188N) rkalaiselvan@dev-rkalaiselvan:~/$ py.test -p no:randomly test_dumm.py
======================================================================== test session starts ========================================================================
platform linux2 -- Python 2.7.12, pytest-3.10.1, py-1.5.4, pluggy-0.7.1 -- /tmp/tmp.w95BqE188N/bin/python2
cachedir: .pytest_cache
Using --randomly-seed=1566829391
rootdir: /home/rkalaiselvan, inifile: pytest.ini
plugins: randomly-1.2.3, timeout-1.3.1, cov-2.6.0, mock-1.10.0, ordering-0.6
collected 4 items

test_dumm.py::test_three PASSED
test_dumm.py::test_four PASSED
test_dumm.py::test_two PASSED
test_dumm.py::test_one PASSED