python-3.x 从pycharm运行pytest时得到空的测试套件

voase2hg  于 2023-03-24  发布在  Python
关注(0)|答案(7)|浏览(840)

我在PyCharm内部运行pytest测试时遇到了问题
我在C:\Temp中的agents_automation_2文件夹中有一个文件x_tests. py,该文件的内容是

import pytest
def test_mytest():
    assert  False

当我运行时,我得到以下输出

  • 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\agents_automation_2中使用参数C:/Temp/agents_automation_2启动py. test ====================================================================================================================================C:\Temp\agents_automation_2,初始化文件:plugins:xdist-1.22.0,forked-0.2收集了0项======================= 0.01秒内未运行任何测试=========================*

但是,当我从文件夹内的常规Windows命令行运行时,测试运行正常
你知道是什么问题吗?
谢谢!!!

jm2pwxwz

jm2pwxwz1#

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

pxy2qtax

pxy2qtax2#

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

ecbunoof

ecbunoof3#

确保PyCharm配置为使用PyTest。默认情况下,它使用unittests。查看此文档:https://www.jetbrains.com/help/pycharm/choosing-your-testing-framework.html。一旦您正确配置了PyCharm以使用PyTest,例如,当您打开测试文件的上下文菜单时,您将看到类似于**Run 'PyTest in ... '**的内容

7rfyedvj

7rfyedvj4#

1.确保您的conftest.py文件位于同一文件夹中。
1.如果你使用的是fixture。在类名中添加“Test”关键字(example = TestClass)

@pytest.mark.usefixtures("setup1")

class TestClass:
      def test_abc(self):
          print("testcase execution")
svmlkihl

svmlkihl5#

在我的例子中,我只需要从项目中删除pytest.ini文件。

c9qzyr3d

c9qzyr3d6#

对于那些使用WSL的人,问题可能与WSL无法访问有关。如果您在WSL上遇到此错误,
Logon failure: the user has not been granted the requested logon type at this computer.
尝试在PowerShell中重新启动vmcompute服务(以管理员身份运行):

powershell restart-service vmcompute

来源:https://github.com/microsoft/WSL/issues/5401

ldxq2e6h

ldxq2e6h7#

在我的例子中,我只在启动时用test更改了类名,这就解决了我的问题。

相关问题