为什么pycharm不能识别我的pytest测试并显示测试输出?

gz5pxeao  于 2023-02-22  发布在  PyCharm
关注(0)|答案(5)|浏览(471)

我是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一样,即使我右键单击某个测试函数左边的绿色箭头,也会出现一个菜单,显示我可以单击:“为test_db运行'pytest.py::测试数据库函数::测试有效数据库'”
如果我在命令行中使用以下命令运行它:

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里做什么特别的事情吗?
抱歉问你这个新手问题,但我一点也想不明白!

mwecs4sa

mwecs4sa1#

请确保Settings > Tools > Python Integrated Tools > Default test runner设置为pytest &清除缓存并重新启动pycharm(特别是如果此设置在过去更改过)

(图片由布兰登Braner提供)

noj0wjuj

noj0wjuj2#

我找到了答案:我不得不:
在Run/Debug Configurations窗口中点击左上角的+按钮,转到Python测试并选择py.test,点击OK按钮。
在左边的窗格中,你可以看到Python测试已经创建,在右边的窗格中,在Name文本框中给出的值为py. test in module_name. py你应该总是在Python测试文件名前面加上单词py. test in。
在目标文本框中指定Python测试文件的路径。
在"工作目录"(Working directory)文本框中,指定Python测试文件所在目录的路径。
点击OK按钮。
不管这起了什么作用现在它起作用了!

iyzzxitl

iyzzxitl3#

如果其他解决方案不起作用,请确保正确设置内容根,以便代码被正确索引,并且可以检测测试。PyCharm -〉首选项-〉项目-〉项目结构-〉添加内容根-〉选择根文件夹作为内容根。

wi3ka0sx

wi3ka0sx4#

我尝试了其他两个答案,并意识到还有一步要走,如果您以前设置鼻子或其他测试类型为默认值。
打开运行/调试配置Python测试〉点击**+按钮〉Pytest〉In目标:选择模块名称**〉Ctrl+空格键,这样就可以完成每个单元测试的代码完成。
screenshot here

cngwdvgl

cngwdvgl5#

我也遇到过类似的问题,我可以在终端中完美地运行测试,但使用PyCharm时却无法运行。结果是,如果您选择test目录并运行它,PyCharm会自动将test目录设置为工作目录,而不是root,这会导致conftest.py中的导入问题。因此,这破坏了我使用PyCharm而不是终端时的测试。

    • 溶液**:手动将工作目录更改为根目录

相关问题