2015-09-22 37 views
1

我有一個pytest測試,像這樣:現在如何使用pytest夾具作爲參數化參數?

email_two = generate_email_two() 

@pytest.mark.parametrize('email', ['[email protected]', email_two]) 
def test_email_thing(self, email): 
     ... # Run some test with the email parameter 

,作爲重構的一部分,我已經感動行:

email_two = generate_email_two() 

到它自己的固定裝置(在conftest.py文件)因爲它被用在各種其他地方。然而,除了直接導入fixture函數,還有其他方法可以參考這個測試嗎?我知道funcargs通常是調用燈具的方式,但在這種情況下,當我想調用燈具時,我不在測試功能內。

+0

聽起來像一個'會話fixture'可以幫助:https://pytest.org/latest/example/special.html – dm295

+0

@ dm295我可以做這樣的事情......但是,這將夫婦夾具到它被調用的地方(我認爲)。在這個意義上說,夾具將自己附加到一個特定的類。雖然,或者,夾具可以附加到_every_類,或類似的東西,但這似乎是一個不好的解決方案。 – ismail

+1

在參數化中不能使用fixtue。 https://bitbucket.org/pytest-dev/pytest/issues/349/using-fixtures-in-pytestmarkparametrize –

回答

-1

我最終通過在測試函數中使用for循環來完成此操作,循環遍歷每個電子郵件。就測試結果而言,這並不是最好的方法,但它可以完成這項工作。

import pytest 
import fireblog.login as login 

pytestmark = pytest.mark.usefixtures("test_with_one_theme") 

class Test_groupfinder: 
    def test_success(self, persona_test_admin_login, pyramid_config): 
     emails = ['[email protected]', persona_test_admin_login['email']] 
     for email in emails: 
      res = login.groupfinder(email) 
      assert res == ['g:admin'] 

    def test_failure(self, pyramid_config): 
     fake_email = '[email protected]' 
     res = login.groupfinder(fake_email) 
     assert res == ['g:commenter']