2013-10-03 27 views
2

我想在一個類的skipif裝飾器中使用pytest fixture(scope = module),但是我收到一個錯誤,提示燈具沒有定義。這可能嗎?我可以在我的skipif邏輯條件下使用pytest夾具嗎?

conftest.py有一個模塊作用域稱爲'目標',返回一個CurrentTarget對象的夾具。 CurrentTarget對象具有isCommandSupported函數。 test_mytest.py有一個Test_MyTestClass類,它包含一打測試函數。 我想跳過所有Test_MyTestClass根據測試,如果燈具target.isCommandSupported所以我裝飾Test_MyTestClass與SKIPIF像:

@pytest.mark.skipif(not target.isCommandSupprted('commandA), reason=command not supported') 
class Test_MyTestClass: 
... 

我得到這個錯誤:NameError:名字「目標」沒有定義

如果我嘗試:

@pytest.mark.skipif(not pytest.config.getvalue('tgt').isCommandSupprted('commandA), reason=command not supported') 
class Test_MyTestClass: 
... 

我得到這個錯誤:AttributeError的: '功能' 對象有沒有屬性 'isCommandSupprted'

+0

有沒有更好的地方要問這個問題? –

+1

我在這裏看到Holger Krekel的答案,但他通常指向freenode聊天支持問題。 http://pytest.org/latest/contact.html – Anthon

+0

我偶然發現了相同的[問題](http://stackoverflow.com/questions/28179026/how-to-skip-a-pytest-using-an-external -fixture)。你有沒有找到解決這個問題的辦法? –

回答

0

您可以從conftest導入target像這樣:

from conftest import target 

然後,您可以在pytest.mark.skipif使用它作爲你在你的榜樣打算。

@pytest.mark.skipif(not target.isCommandSupported('commandA'), reason='command not supported') 
def Test_MyTestClass: 

如果您需要在多個測試中重複同樣的pytest.mark.skipif邏輯和希望避免的複製粘貼,簡單的裝飾將幫助:

check_unsupported = pytest.mark.skipif(not target.isCommandSupported('commandA'), 
             reason='command not supported') 

@check_unsupported 
def test_one(): 
    pass 

@check_unsupported 
def test_two(): 
    pass 
相關問題