我是 pytest 新手,正在尝试为我的应用程序编写测试模块。我的目录结构是:
broker/
broker/tests
broker/tests/conftest.py
broker/tests/test_db.py
broker/db.py
我想测试 db.py 模块。
我将 pycharm 配置为使用 pytest 作为我的测试运行程序。当我在 pycharm 中运行 test_db.py 时,我得到:
/Users/cbogdon/virtualenv/platinum-onboard/bin/python /Users/cbogdon/coding/platinum-onboard/broker/tests/test_db.py
Process finished with exit code 0
这几乎就像 pycharm 没有执行 pytest。即使我右键单击测试函数之一左侧的绿色箭头,也会出现一个菜单并显示我可以单击:“运行 'pytest for test_db.py::TestDBFunctions::test_valid_db'”
如果我使用以下命令在命令行运行它:
python -m pytest --setup-show tests/test_db.py
我得到了适当的测试输出。
python -m pytest --setup-show tests/test_db.py
========================================================== test session starts ===========================================================
platform darwin -- Python 3.6.1, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: /Users/cbogdon/coding/platinum-onboard/broker, inifile:
collected 4 items
tests/test_db.py
tests/test_db.py::TestDBFunctions::test_uuid.
tests/test_db.py::TestDBFunctions::test_invalid_dbF
tests/test_db.py::TestDBFunctions::test_valid_db.
tests/test_db.py::TestDBFunctions::test_already_created_database.
================================================================ FAILURES ================================================================
____________________________________________________ TestDBFunctions.test_invalid_db _____________________________________________________
self = <test_db.TestDBFunctions object at 0x102606438>
def test_invalid_db(self):
> ret = db.initialize_database()
E TypeError: initialize_database() missing 1 required positional argument: 'dbname'
tests/test_db.py:14: TypeError
=================================================== 1 failed, 3 passed in 0.08 seconds ==================================================
我需要在 PyCharm 中做一些特别的事情吗?
很抱歉这个新手问题,但我只是想不通!
最佳答案
请确保Settings > Tools > Python Integrated Tools > Default test runner
设置为pytest
并清理缓存并重新启动pycharm(特别是如果此设置在过去)
(图片由 Brandon Braner 提供)
关于python - 为什么 pycharm 无法识别我的 pytest 测试并显示测试输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54422312/