简体   繁体   English

为什么 pytest 在仅运行一部分测试时不加载 conftest.py?

[英]Why doesn't pytest load conftest.py when running only a subset of tests?

Here is my API test directory layout:这是我的 API 测试目录布局:

api_tests
├── conftest.py
└── query
    └── me_test.py

Contents of conftest.py: conftest.py 的内容:

print("CONFTEST LOADED")

Contents of me_test.py: me_test.py 的内容:

"""Tests the "me" query"""

def test_me():
    assert True

If I simply run pytest , everything works:如果我只是运行pytest ,一切正常:

================================================= test session starts =================================================
platform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
rootdir: /home/hubro/myproject, configfile: pytest.ini
collecting ... CONFTEST LOADED
collected 3 items                                                                                                     

api_tests/query/me_test.py .                                                                                    [ 33%]
lib/myproject/utils_test.py .                                                                                   [ 66%]
lib/myproject/schema/types/scalars_test.py .                                                                    

Notice "CONFTEST LOADED" is printed.注意打印“CONFTEST LOADED”。 Great!伟大的! However, this test run also picked up all my unit tests, which I don't want.但是,此测试运行还包含了我不想要的所有单元测试。 I want to separate my test runs into unit tests and API tests, I don't want to run them all in one go.我想将我的测试运行分为单元测试和 API 测试,我不想一次性运行它们。

However, if I simply run pytest api_tests/ :但是,如果我只是运行pytest api_tests/

================================================= test session starts =================================================
platform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
rootdir: /home/hubro/myproject, configfile: pytest.ini
collected 1 item                                                                                                      

api_tests/query/me_test.py .                                                                                    [100%]

================================================== 1 passed in 0.00s ==================================================

Now the right tests are run, but the conftest.py file wasn't loaded... How come?现在运行了正确的测试,但是没有加载 conftest.py 文件......怎么会?


I am using Pytest 6.1.0 on Python 3.8.我在 Python 3.8 上使用 Pytest 6.1.0。


EDIT: Alright, I found an acceptable workaround.编辑:好的,我找到了一个可以接受的解决方法。 I can override INI file options through the command line with the -o option.我可以通过命令行使用-o选项覆盖 INI 文件选项。 This works:这有效:

poetry run pytest -o "testpaths=api_tests"

However, I would very much like an answer to the original question so I'm not going to delete it.但是,我非常想要原始问题的答案,因此我不会将其删除。

The conftest plugin will be registered in both invocations, the only difference being the registration stage. conftest插件将在两次调用中注册,唯一的区别是注册阶段。 If in doubt, add the --traceconfig argument to list the registered plugins in order of their registration:如果有疑问,请添加--traceconfig参数以按注册顺序列出已注册的插件:

$ pytest --traceconfig
PLUGIN registered: <_pytest.config.PytestPluginManager object at 0x7f23033ff100>
PLUGIN registered: <_pytest.config.Config object at 0x7f2302d184c0>
...
=================================== test session starts ===================================
...
PLUGIN registered: <module 'conftest' from 'path/to/conftest.py'>
...

In the first invocation, the conftest.py won't be found immediately since it's down the test root path, so it will be loaded while pytest discovers the tests.在第一次调用中, conftest.py不会立即被找到,因为它位于测试根路径下,所以它会在pytest发现测试时加载。 In the second invocation, conftest.py is located in test root, so it will be loaded even before the test session starts (after the plugins passed via -p arg and registered via setuptools entrypoint are loaded).在第二次调用中, conftest.py位于测试根目录中,因此它甚至会在测试会话开始之前加载(在加载通过-p arg 传递并通过setuptools入口点注册的插件之后)。 Running pytest -s (with output capturing disabled) should reveal the custom print, located above the ==== test session starts ==== line.运行pytest -s (禁用输出捕获)应该显示位于==== test session starts ====行上方的自定义打印。

If you want the print to be identical between the two invocations, put it in a suitable hook.如果您希望两次调用之间的打印相同,请将其放在合适的挂钩中。 For example, to always print CONFTEST loaded after test collection finished, use:例如,要在测试收集完成后始终打印CONFTEST loaded ,请使用:

# api_tests/conftest.py

def pytest_collectreport(report):
    print("CONFTEST loaded")

There are other options available for custom output placement;还有其他选项可用于自定义输出放置; best is to check out the list of available hooks under Hooks in pytest reference.最好是查看pytest参考中Hooks下的可用钩子列表。

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

相关问题 PyTest - 在 conftest.py 中指定清理测试 - PyTest - Specify cleanup tests in conftest.py Pytest:在框架测试中使用App conftest.py - Pytest: use App conftest.py in framework tests pytest 没有运行 conftest.py 来获取自定义命令参数 - pytest is not running conftest.py to pick up custom command args 在父项目上运行 pytest 时,如何忽略 git 子模块(例如 submodule/conftest.py)根目录下的 conftest.py 文件? - How do I ignore a conftest.py file at the root of a git submodule (e.g. submodule/conftest.py) when running pytest on the parent project? 在Robot Framework上是否有conftest.py pytest等价物 - Is there a conftest.py pytest equivalent on Robot Framework 在pytest中,conftest.py文件有什么用? - In pytest, what is the use of conftest.py files? 为子目录中的测试定义一个conftest.py - Defining a single conftest.py for tests in subdirectories 在conftest.py中使用pytest_configure()时pytest --help无法运行 - pytest --help does not run when using pytest_configure() in conftest.py 除了 pytest 的测试 package 之外,conftest.py 文件是否可以放在另一个 package 中? - Can the conftest.py file be put in another package aside the tests package for pytest? 使用 Pytest 和 conftest.py 文件时未显示断言错误的预期结果 - Expected results of Assertion Errors are not showing up when using Pytest and conftest.py file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM