简体   繁体   English

为什么pytest可以不导入而引用fixture?

[英]Why pytest can reference fixture without import?

Hi I am new to python I found that if we use pytest, fixtures that defined in conftest.py can be directly referenced without needing import in test module. 嗨,我是python的新手,我发现如果我们使用pytest,则可以直接引用conftest.py中定义的fixture,而无需在测试模块中导入 Similarly, if we are using pytest-mock, a fixture called mocker can be referenced any where in the test modules as long as the test is triggered by pytest, no need to do import either. 作为测试由pytest触发同样,如果我们使用pytest-模拟,一次装夹叫亵慢只要可以参考在测试模块的任何地方,没有必要做任何进口 I think there must be one or multiple python language features that enable such kind of thing. 我认为必须有一种或多种python语言功能可以启用这种功能。 What are they? 这些是什么? If your answers can assume that I am from Java/C++ background that will be great! 如果您的答案可以假定我来自Java / C ++背景,那就太好了!

the pytest fixture system registers fixtures by name, and then looks those names up in the fixture registry, not the actual python module you are currently in pytest夹具系统按名称注册夹具,然后在夹具注册表中查找这些名称,而不是您当前所在的实际python模块

so you dont need to import them in each test module, and plugins for pytest can provide them 因此您无需在每个测试模块中导入它们,并且pytest插件可以提供它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM