pytest ordered plugin不能与多个文件组合使用

时间:2016-03-10 06:59:25

标签: python unit-testing pytest pytest-django

您好我正在使用“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开始order = 1,首先执行file2,然后在两个文件中开始执行order = 2

有没有办法指定只在目前正在执行的文件中说明订单?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。在这里我开始使用 pytest-order 而不是 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

答案 1 :(得分:0)

即使文件中有两个类,我也看到了这个问题,并且我们已经为这两个类中的函数定义了命令。

示例:

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命名文件,以便按顺序执行。 但我个人觉得第一种方式更好。