从pycharm使用pytest运行时获取空测试套件

Dav*_*uer 5 pycharm python-3.x

我在从PyCharm内部运行pytest测试时遇到问题

我在C:\ Temp中一个名为agent_automation_2的文件夹中有一个文件x_tests.py,文件的内容是

import pytest
def test_mytest():
    assert  False
Run Code Online (Sandbox Code Playgroud)

运行时,我得到以下输出

C:\ Python36-32 \ python.exe“ C:\ Program Files \ JetBrains \ PyCharm 2017.3.2 \ helpers \ pycharm_jb_pytest_runner.py” --path C:/ Temp / agents_automation_2使用参数C:/ Temp /启动py.test C:\ Temp \ agents_automation_2中的agent_automation_2 ==============================测试会话开始========== ==================平台win32-Python 3.6.4,pytest-3.4.2,py-1.5.2,pluggy-0.6.0 rootdir:C:\ Temp \ agents_automation_2,inifile:插件:xdist-1.22.0,forked-0.2收集了0个项目======================== 0.01秒内没有运行测试========================

但是,当我从文件夹内的常规Windows命令行运行时,测试运行正常

知道可能是什么问题吗?

谢谢 !!!

Bel*_*fon 9

您的测试文件的名称不符合默认的 pytest 发现规则。如果您将x_tests.py更改为x_test.py,它将运行测试并按预期失败。有关 pytest 发现规则的更多信息,请查看此页面

  • 另外,待测试文件名中可以​​是 test_*.py 或 *_test.py。两种约定都是正确的。另外,测试函数有两点,即 1. 在类之外测试带前缀的测试函数或方法 2. 在带有 Test 前缀的测试类内测试带前缀的测试函数或方法(没有 __init__ 方法) (2认同)

小智 5

就我而言,测试类包含init方法,也会导致空套件。你需要做的是避免编写 init 方法。

  • 为了清楚起见,您可以重新编写/编辑您的答案吗? (2认同)