pytest 有序插件不适用于多个文件的组合

pytest ordered plugin doesn't work with combination of multiple files

您好,我正在使用“http://pytest-ordering.readthedocs.org/en/develop/”,当我使用如下装饰器时,订单工作正常,

import pytest

@pytest.mark.run(order=3)
def test_three():
    assert True

@pytest.mark.run(order=1)
def test_four():
    assert True

@pytest.mark.run(order=2)
def test_two():
    assert True

现在假设我有两个文件 test_example1.py 和第二个文件 test_example2.py

在这种情况下,如果我使用此顺序,则首先执行 file1 和 file2 中的 order=1,然后开始在两个文件中执行 order=2

有没有办法指定只在当前正在执行的文件中说顺序检查?

即使一个文件中有两个 类 并且我们已经为两个 类.

中的函数定义了顺序,我也会看到这个问题

示例:

class Test_abc:

    @pytest.mark.run(order=1)
    def test_func_a(self):
        print 'class 1 test 1'
    @pytest.mark.run(order=1)
    def test_func_b(self):
        print 'class 1 test 2'
    @pytest.mark.run(order=1)
    def test_func_c(self):
        print 'class 1 test 3'

class Test_bcd:

    @pytest.mark.run(order=1)
    def test_func_ab(self):
        print 'class 2 test 1'
    @pytest.mark.run(order=1)
    def test_func_bc(self):
        print 'class 2 test 2'
    @pytest.mark.run(order=1)
    def test_func_cd(self):
        print 'class 2 test 3'

输出如下:

class 1 test 1
class 2 test 1
class 1 test 2
class 2 test 1
class 1 test 3
class 2 test 1

我做了类似的事情来解决这个问题

@pytest.mark.run(order=1)
class Test_abc:
    def test_func_a(self):
        print 'class 1 test 1'
    def test_func_b(self):
        print 'class 1 test 2'
    def test_func_c(self):
        print 'class 1 test 3'


@pytest.mark.run(order=2)
class Test_bcd:

    def test_func_ab(self):
        print 'class 2 test 1'

    def test_func_bc(self):
        print 'class 2 test 2'

    def test_func_cd(self):
        print 'class 2 test 3'

输出:

class 1 test 1
class 1 test 2
class 1 test 3
class 2 test 1
class 2 test 2
class 2 test 3

所以,对于你的问题,我认为你可以尝试这样的事情。我知道这不是您问题的完美答案,但您可以即兴使用此解决方案。 我尝试对文件(和文件名)执行此操作。似乎 pytest 编译器被编程为按顺序获取数字或字母。因此,您可能需要将文件命名为 test_1_foo.pytest_2_bar.pytest_a.pytest_b.py 以按顺序执行它们。 不过个人感觉第一种更好

我遇到了同样的问题。这里我开始使用pytest-order instead of pytest-ordering因为pytest-ordering已经不再维护

将所有标记从 run 更改为 order,例如从 @pytest.mark.run(order=1) 更改为 @pytest.mark.order(1)

现在使用以下命令执行测试:

pytest -v --order-scope=module

测试现在将在每个文件中单独排序。

参考https://pytest-dev.github.io/pytest-order/dev/configuration.html#order-scope