Is this the correct use of conftest.py?
是的.固定装置是conftest.py的潜在和常见用途.该
您将定义的灯具将在您的测试套件中的所有测试中共享.但是,在根conftest.py中定义fixture可能没用,如果所有测试都没有使用这些灯具,它会减慢测试速度.
Does it have other uses?
是的,它确实.
>夹具:为测试使用的静态数据定义夹具.除非另有说明,否则可以通过套件中的所有测试访问此数据.这可能是数据以及将传递给所有测试的模块的帮助程序.
>外部插件加载:conftest.py用于导入外部插件或模块.通过定义以下全局变量,pytest将加载模块并使其可用于其测试.插件通常是项目中定义的文件或测试中可能需要的其他模块.您还可以按照说明here加载一组预定义插件.
pytest_plugins =“someapp.someplugin”
>挂钩:您可以指定挂钩,例如设置和拆卸方法等等,以改善您的测试.有关一组可用的挂钩,请阅读here.示例:
def pytest_runtest_setup(item):
""" called before ``pytest_runtest_call(item). """
#do some stuff`
>测试根路径:这是一个隐藏的功能.通过在根路径中定义conftest.py,您将获得pytest识别应用程序模块而无需指定PYTHONPATH.在后台,py.test通过包含从根路径找到的所有子模块来修改sys.path.
Can I have more than one conftest.py file?
是的,你可以,强烈建议你的测试结构有点复杂. conftest.py文件有目录范围.因此,创建有针对性的固定装置和助手是很好的做法.
When would I want to do that? Examples will be appreciated.
有几种情况可以适合:
为特定测试组创建一组工具或挂钩.
根/ MOD / conftest.py
def pytest_runtest_setup(item):
print("I am mod")
#do some stuff
test root/mod2/test.py will NOT produce "I am mod"
为某些测试加载一组灯具但不为其他测试加载.
根/ MOD / conftest.py
@pytest.fixture()
def fixture():
return "some stuff"
根/ MOD2 / conftest.py
@pytest.fixture()
def fixture():
return "some other stuff"
根/ MOD2 / test.py
def test(fixture):
print(fixture)
将打印“其他一些东西”.
覆盖从根conftest.py继承的挂钩.
根/ MOD / conftest.py
def pytest_runtest_setup(item):
print("I am mod")
#do some stuff
根/ conftest.py
def pytest_runtest_setup(item):
print("I am root")
#do some stuff
通过在root / mod中运行任何测试,只打印“我是mod”.
您可以阅读有关conftest.py here的更多信息.
编辑:
What if I need plain-old helper functions to be called from a number
of tests in different modules – will they be available to me if I put
them in a conftest.py? Or should I simply put them in a helpers.py
module and import and use it in my test modules?
您可以使用conftest.py来定义帮助程序.但是,你应该遵循惯例.辅助工具至少可以在pytest中用作固定装置.例如,在我的测试中,我有一个模拟redis帮助器,我以这种方式注入我的测试.
根/辅助/ redis的/ redis.py
@pytest.fixture
def mock_redis():
return MockRedis()
根/测试/材料/ conftest.py
pytest_plugin="helper.redis.redis"
根/测试/材料/ test.py
def test(mock_redis):
print(mock_redis.get('stuff'))
这将是一个测试模块,您可以在测试中自由导入.请注意,如果您的模块redis包含更多测试,则可以将redis.py命名为conftest.py.但是,由于含糊不清,不鼓励这种做法.
如果你想使用conftest.py,你可以简单地把这个帮助器放在你的root conftest.py中,并在需要时注入它.
根/测试/ conftest.py
@pytest.fixture
def mock_redis():
return MockRedis()
根/测试/材料/ test.py
def test(mock_redis):
print(mock_redis.get(stuff))
你可以做的另一件事是编写一个可安装的插件.在这种情况下,您的助手可以在任何地方编写,但需要定义一个入口点,以便安装在您和其他潜在的测试框架中.见this.
如果你不想使用灯具,你当然可以定义一个简单的帮手,只需在任何需要的地方使用普通的旧导入.
根/测试器/辅助/ redis.py
class MockRedis():
# stuff
根/测试/材料/ test.py
from helper.redis import MockRedis
def test():
print(MockRedis().get(stuff))
但是,在这里您可能会遇到路径问题,因为模块不在测试的子文件夹中.您应该能够通过向助手添加__init__.py来克服此问题(未经测试)
根/测试器/辅助/ __ init__.py
from .redis import MockRedis
或者只是将辅助模块添加到PYTHONPATH.