python - matplotlib 测试无头且没有警告
问题描述
pytest
如果测试包含matplotlib绘图例程,则会弹出我必须手动关闭的窗口。为了防止这种情况,我通常通过调用 pytest 来无头运行测试
MPLBACKEND=Agg pytest
或通过设置
import matplotlib
matplotlib.use("Agg")
在测试文件中。两种变体都会产生警告
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
我宁愿避免。大量的 agg-warnings 可能会隐藏我关心的实际警告。
也许还有另一种无头运行 matplotlib 测试的规范方式。有什么提示吗?
解决方案
在 pytest 中,有多种方法可以忽略警告,请参见此处。一种可能性是通过装饰函数来忽略单个测试的警告
@pytest.mark.filterwarnings(
'ignore:Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.:UserWarning'
)
如果您想忽略项目中的所有此类警告,请添加
[tool.pytest.ini_options]
filterwarnings = [
'ignore:Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.:UserWarning'
]
到你的pyproject.toml
或
[pytest]
filterwarnings =
ignore:Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.:UserWarning
到你的pytest.ini
.
推荐阅读
- ruby-on-rails - Rails -> swagger -> rswag:是否可以使用 AND/OR 链接所需的属性?
- javascript - 在 => 第二次单击任何表单元素后显示 div
- objective-c - 使用 Firebase 的 NSCache 存储图像 - 目标 C
- python - 使用 pip 在 macOS 上安装 pycurl 失败
- spring - 在启用 SSL 的 Spring Admin 上监控 Web 服务的问题
- excel - VBA excel rightfax
- javascript - 无法使用 CSS-Approach 或 JQuery-Apprach 向文本区域添加必需的验证
- git - Git 提交消息未进入下一步
- javascript - 在 Angular Material 手风琴中打开第一个附加的扩展面板
- c - C访问冲突错误,但不在调试模式下