2015-12-30 65 views
1

您好我使用pytest,并在文件夹中有以下2个py文件。Pytest从不同的测试用例文件中订购

test_abc.py如下:如下

class MyTest(unittest.TestCase): 
    @classmethod 
    def setup_class(cls): 
     cls.a = 10 

    @classmethod 
    def teardown_class(cls): 
     cls.a = 20 

    @pytest.mark.run(order=2) 
    def test_method1(self): 
     logging.warning('order2 in test_abc') 
     assert (10,self.a) # fail for demo purposes 

    @pytest.mark.run(order=1) 
    def test_method2(self): 
     logging.warning('order1 in test_abc') 
     assert 0, self.db # fail for demo purposes 

test_sample2.py,

class MyTest1(unittest.TestCase): 
    @classmethod 
    def setup_class(cls): 
     cls.a = 10 

    @classmethod 
    def teardown_class(cls): 
     cls.a = 20 

    @pytest.mark.run(order=2) 
    def test_mtd1(self): 
     logging.warning('order2 in test_samp') 
     assert (10,self.a) # fail for demo purposes 

    @pytest.mark.run(order=1) 
    def test_mtd2(self): 
     logging.warning('order1 in test_samp') 
     assert 0, self.db # fail for demo purposes 

现在我运行使用命令:

py.test --tb=long --junit-xml=results.xml --html=results.html -vv 

这里会发生什么事是从两个test_method2测试用例文件首先运行(因为它已经作为order1给出),然后test_method1从两个文件运行(si NCE已经给如订单2)

所以我在这里注意到的是订购是全面测试运行而不是针对单个类/文件

有没有什么办法来解决这个问题?现在我使用订购号码为所有像第一个文件我给(1,2)然后在下一个文件我给(3,4),它工作正常。

但是我不想在所有测试课上只在少数需要它的地方订购。是否有任何钩子说pytest只看到特定文件的订单?

回答

2

我假设你正在使用的pytest排序插件 - 如果在测试中只有特定的领域需要排序,你可以使用相对顺序:

@pytest.mark.run(after='test_second') 
def test_third(): 
    assert True 

def test_second(): 
    assert True 

@pytest.mark.run(before='test_second') 
def test_first(): 
    assert True 

参考:(http://pytest-ordering.readthedocs.org/en/develop/#relative-to-other-tests