如何修复 pytest 解析以下警告配置时的 ERROR:...

问题:

你想在单元测试期间忽略警告,使用类似这样的代码

pytest_filterwarnings.py
@pytest.mark.filterwarnings("divide by zero encountered in log10")
def test_auto_suffix_1d(self):
  # ...

但你看到类似这样的错误消息

pytest_error.txt
ERROR: while parsing the following warning configuration:

  divide by zero encountered in log10

This error occurred:

invalid action: 'divide by zero encountered in log10'

解决方案

@pytest.mark.filterwarnings(...) 的参数中你忘记了 ignore: 前缀:

而不是

pytest_filterwarnings_fixed.py
@pytest.mark.filterwarnings("divide by zero encountered in log10")

pytest_filterwarnings_ignore.py
@pytest.mark.filterwarnings("ignore: divide by zero encountered in log10")

你可能会看到类似这样的错误:

pytest_error_output.txt
INTERNALERROR> while parsing the following warning:
INTERNALERROR> configuration

通常原因是无效的 pytest.inipyproject.toml 配置。示例 pytest.ini

pytest.ini
[pytest]
addopts = -ra -q
python_files = tests.py test_*.py *_test.py

验证你的配置并删除无效选项以修复错误。


Check out similar posts by category: Python