python - 为什么 pycharm 无法识别我的 pytest 测试并显示测试输出?

标签 python pycharm pytest

我是 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(特别是如果此设置在过去)enter image description here

(图片由 Brandon Braner 提供)

关于python - 为什么 pycharm 无法识别我的 pytest 测试并显示测试输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54422312/

相关文章:

python - 使用pytest教程出错

python - 如何在 Eclipse 上运行 pytest?

python - 为数值积分提供路径点

作为 Windows 服务的 Python XML-RPC 服务器

python - 检测 python 脚本是从 ipython shell 运行,还是从命令行运行

python - 字体损坏

python - Powershell 文件路径命令正在被 Pycharm 更改

python - 是否可以在 PyCharm 中使用静态类型断言?

python - 如何避免在 PyTest 中从 session 范围固定装置中改变对象?

python - Convert_obj_two.py 语法错误