如何让 PyCharm 从 setup.cfg(一个由 PyScaffold 创建的文件)中获取测试配置?
我确实从设置 -> 工具 ->Python 集成工具 -> 测试中启用了 PyTest
到目前为止,我所拥有的是保存在 Python 测试部分中的配置,其中包含:
目标:脚本路径 = C:...\setup.py
其他参数:测试
但这是我运行它时得到的:
[...] ERROR: file not found: test
或者如何在 PyCharm 中复制与设置.cfg相同的测试配置?
console_scripts =
program = program.main:run
[test]
# py.test options when running `python setup.py test`
# addopts = --verbose
extras = True
[tool:pytest]
# Options for py.test:
# Specify command line options as you would do when invoking py.test directly.
# e.g. --cov-report html (or xml) for html/xml output or --junitxml junit.xml
# in order to write a coverage file that can be read by Jenkins.
addopts =
--cov reddit_users_info --cov-report term-missing
--verbose
[...]
testpaths = tests
-
打开项目目录并将其配置为使用创建的 venv。在脚本路径中,您将有
srcprogramprogram.py
,在参数中,您可以添加-vv
. -
从
Settings -> Tools -> Python Integrated Tools -> Testing
启用 PyTest -
添加运行配置,但也添加一个用于测试的配置:
Edit Configurations -> + -> Python tests -> pytest
。 -
运行测试。如果您安装了测试所需的软件包,它将起作用(检查上面的"测试"部分)。该过程将在项目目录中创建一个 .coverage 文件。
-
如果你有 PyCharm Professional 而不是 PyCharm Community,你也应该这样做:
Ctrl + Alt + F6 (Choose coverage suite to display) -> + -> select any of the created files -> Show selected
.这将在脚本中添加花哨的颜色,具体取决于测试覆盖代码的位置。