python - 如何仅将 conftest.py 中的夹具应用到内部文件夹
问题描述
我有一个位于 conftest.py 中的夹具。
@pytest.fixture(scope='module', autouse=True)
def my_fixture():
"""
Some useful code
"""
结构如下:
tests
|
|--first_folder
| |--__init__.py
| |--test_first_1.py
| |--test_first_2.py
|
|--second_folder
| |--__init__.py
| |--test_second_1.py
|
|--__init__.py
|--conftest.py
|--test_common_1.py
我希望该夹具仅在内部文件夹测试脚本中自动使用:在test_first_1.py、test_first_2.py、test_second_1.py中,但不在 test_common_1.py中。
我可以在每个内部文件夹中使用该夹具创建 conftest,但我不想复制代码
有什么方法可以将 conftest 中的夹具应用到内部文件夹中的测试脚本并在公共文件夹测试脚本中忽略它?
解决方案
一种可能的解决方案是,您不想更改文件夹的结构,即使用request
夹具中的对象来检查测试中使用的标记,因此如果设置了特定标记,则可以执行任何操作:
@pytest.fixture(scope='module', autouse=True)
def my_fixture(request):
"""
Some useful code
"""
if 'noautofixt' in request.keywords:
return
# more code
然后将您的测试标记如下:
@pytest.mark.noautofixt
def test_no_running_my_fixture():
pass
推荐阅读
- javascript - 在 React 中遍历虚拟 DOM
- javascript - 如何在 react native 中编写普通的 javascript 外部函数?
- c++ - 在析构函数中的唯一指针上调用重置的 C++ 语义
- identityserver4 - 使用角色将 Identity Server 4 与 API 服务共同托管
- asp.net - 按钮单击 ASP.NET 后无需等待操作完成即可发送电子邮件
- python - 如何将python类'str'转换为没有左引号和右引号的类'tuple'
- java - Netbeans 10 及更高版本中 Ant java webapplication 的奇怪行为
- php - 如何在 Windows 上安装带有 php 7.4.9 的 mongodb 和带有 Apache 2.4.46 的 WAMP
- python - ImportError:无法从“scipy._lib”导入名称“_ccallback_c”
- reactjs - 试图注册两个同名的视图