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

时间:2019-01-29 13:35:04

标签: 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一样。即使我右键单击我的测试功能之一左侧的绿色箭头,也会出现一个菜单,并显示我可以单击:“运行'testtest_db.py::TestDBFunctions::test_valid_db'的pytest”“

如果我使用以下命令在命令行上运行它:

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中我需要做些特别的事情吗?

对新手问题很抱歉,但我对此一无所知!

3 个答案:

答案 0 :(得分:0)

请确保将Settings > Tools > Python Integrated Tools > Default test runner设置为pytest并清理缓存,然后重新启动pycharm(尤其是如果此设置以前已更改)

答案 1 :(得分:0)

我找到了答案:我必须:

在“运行/调试配置”窗口中,单击左上角的+按钮。转到Python测试并选择py.test。点击确定按钮。

在左窗格中,您可以看到Python测试已创建。在“名称”文本框的右窗格中,在module_name.py中将值指定为py.test。您应始终在Python测试文件名前加上py.test in字样。

在“目标”文本框中,指定Python测试文件的路径。

在“工作目录”文本框中,指定包含Python测试文件的目录的路径。

单击“确定”按钮。

无论这有什么用,现在都可以用!

答案 2 :(得分:0)

我尝试了另外两个答案,并且意识到如果您之前将鼻子或其他测试类型设置为默认值,则还有一个步骤要做。

打开运行/调试配置> Python测试>单击 + 按钮> Pytest >在目标:选择模块名称> Ctrl +空格,以便可以完成每个单元测试的代码。

screenshot here

相关问题