Py学习  »  pycharm

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

ChrisBogDog • 4 年前 • 2508 次点击  

我是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一样。即使我右键单击某个测试函数左侧的绿色箭头,也会出现一个菜单,显示我可以单击:“run'py test for test_db.py::test db functions::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 ==================================================

我在皮查姆有什么特别的事要做吗?

很抱歉有个新问题,但我一点也搞不懂!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44181
 
2508 次点击  
文章 [ 2 ]  |  最新文章 4 年前
ChrisBogDog
Reply   •   1 楼
ChrisBogDog    5 年前

我找到了答案:我必须:

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

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

对于目标文本框,指定python测试文件的路径。

对于工作目录文本框,指定包含python测试文件的目录的路径。

单击“确定”按钮。

不管这起到什么作用,现在它起作用了!

Slam
Reply   •   2 楼
Slam    5 年前

请确保 Settings > Tools > Python Integrated Tools > Default test runner 设置为 pytest &清理缓存并重新启动PyCharm(特别是在以前更改过此设置的情况下)(amp;C)