2015-12-30 34 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