如何使用 pytest fixture 作为参数化参数?

How can I use a pytest fixture as a parametrize argument?

我有一个像这样的 pytest 测试:

email_two = generate_email_two()

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

现在,作为重构的一部分,我移动了行:

email_two = generate_email_two()

放入它自己的夹具中(在 conftest.py 文件中),因为它在其他各种地方使用。但是,除了直接导入 fixture 函数之外,还有什么方法可以在这个测试中引用它吗?我知道 funcargs 通常是调用夹具的方式,但在这种情况下,当我想调用夹具时,我不在测试函数中。

我最终通过在测试函数中使用 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 = ['id5489746@mockmyid.com', 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 = 'some_fake_address@example.com'
        res = login.groupfinder(fake_email)
        assert res == ['g:commenter']