Pytest Fixtures¶
Faker 包含一个用于 pytest 的 faker fixture。
def test_faker(faker):
assert isinstance(faker.name(), str)
默认情况下,faker fixture 返回一个会话范围(session-scoped)的 Faker 实例,供测试套件中的所有测试使用。此实例默认为 en-US 区域设置,在每次测试之前会使用种子值 0 重新设定种子,并清除 .unique 记住的生成值。
要更改默认区域设置,您可以在顶层 conftest.py 中定义一个会话范围的 autouse faker_session_locale fixture。要更改默认种子值,您可以定义一个会话范围的 autouse faker_seed fixture。例如,如果您想使用 it_IT 区域设置和种子值 12345,那么您只需在顶层 conftest.py 中包含以下内容即可
import pytest
@pytest.fixture(scope='session', autouse=True)
def faker_session_locale():
return ['it_IT']
@pytest.fixture(scope='session', autouse=True)
def faker_seed():
return 12345
如果您需要一个支持多种区域设置的实例,只需返回一个包含多个、唯一且有效区域设置的列表即可
import pytest
@pytest.fixture(scope='session', autouse=True)
def faker_session_locale():
return ['it_IT', 'ja_JP', 'en_US']
配置选项¶
如上所述,默认情况下会返回一个会话范围的 Faker 实例供您使用。这是为了防止在大多数用例中不必要的 Faker 实例化而设计的。当然,在一些不常见的用例中,这种方法可能不足够,这就是为什么 faker fixture 实际上是一个函数范围的 fixture,可以根据需要配置为不同的行为。
Important
配置 faker fixture 需要对 pytest 如何处理 fixture(更具体地说是作用域、共享、注入)有一些基本的了解。如果您不熟悉这个主题,强烈建议在继续之前阅读 pytest fixtures。
区域设置配置¶
如果一个 faker_locale fixture 对某个测试处于活动状态,faker fixture 将回退为该测试返回一个新的 Faker 实例(函数范围),因此如果您不喜欢使用会话范围的 Faker 实例,只需根据 pytest 处理 fixture 的方式,在适当的位置定义并激活一个 faker_locale fixture。
例如,如果您只需要为某些测试切换到不同的区域设置,您可以在子模块的 conftest.py 中或在测试文件本身中定义一个具有非会话范围的 autouse faker_locale fixture,如下所示
import pytest
@pytest.fixture(scope=any_non_session_scope, autouse=True)
def faker_locale():
return ['it_IT']
当在相关测试中使用 faker fixture 时,该 fixture 将自动使用新的 Faker 实例
def test_something(faker):
# The faker fixture here will return a new instance, not the session-scoped instance
pass
如果您想更明确地控制哪些测试应该使用新实例,或者需要更精细的控制,您可以去掉 autouse=True 并使用手动注入
import pytest
@pytest.fixture()
def faker_locale():
return ['it_IT']
def test_something(faker):
# The faker fixture will return the session-scoped instance
pass
def test_something_else(faker, faker_locale):
# The faker fixture will return a new instance, not the session-scoped instance
pass
种子配置¶
除了 faker_locale 之外,faker fixture 还会检查 faker_seed fixture 是否对某个测试处于活动状态。如果没有,则使用种子值 0,如果有,则使用返回值作为种子。这里需要记住的重要一点是:任何使用 faker fixture 的测试,无论它返回的是会话范围还是函数范围的 Faker 实例,都保证是一个已设定种子的实例。设定种子的操作独立于实例选择逻辑。
如上所述,定义一个 autouse 会话范围的 faker_seed fixture 将影响会话中的所有相关测试,但如果您想为特定的测试集使用某个种子,并且像 faker_locale 一样,您只需根据 pytest 处理 fixture 的方式,在适当的位置定义并激活一个 faker_seed fixture。例如,如果您在子模块的 conftest.py 中声明此内容,faker fixture 将为该子模块下的所有相关测试返回一个使用 12345 设定的种子实例。
import pytest
@pytest.fixture(scope=any_non_session_scope, autouse=True)
def faker_seed():
return 12345
如果您想更明确地控制哪些测试应该使用不同的种子,或者需要更精细的控制,您可以去掉 autouse=True 并使用手动注入,就像您对 faker_locale 所做的那样
import pytest
@pytest.fixture(scope=any_non_session_scope)
def faker_seed():
return 12345
def test_something(faker):
# The faker fixture will use the session seed value
pass
def test_something_else(faker, faker_seed):
# The faker fixture will use the seed value 12345
pass
如果您需要为每个测试设置多个不同的种子,或者需要在测试中途重新设定种子,您仍然可以像对任何 Faker 实例一样显式调用 seed_instance。这样做不会影响其他测试,因为 faker fixture 提供了种子保证。
# Assume the active seed value is 54321 for these tests
def test_something_first(faker):
# The faker fixture, at first, uses seed value 54321
do_thing_a()
# Explicit call to seed_instance
faker.seed_instance(12345)
# The faker fixture now uses seed value 12345
do_thing_b()
def test_something_second(faker):
# The faker fixture's seed value is still 54321, not 12345
pass