使用Pycharm(IDE)运行Pytest

2024-10-01 07:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用pycharm运行以下简单的测试用例,当我运行这些测试用例时,我看到了输出

Process finished with exit code 0

而是通过/失败。在

我运行的测试用例:

import pytest    
from pytest1 import can_p1

msg=0

@pytest.fixture(scope = "module")    
def msgFraming():

    print ("I'm inside FIXTURE")

    nchannel = 686    
    FuncID  = 144    
    opType  = 8        
    FuncID = FuncID * 2    
    msg = nchannel+FuncID+opType

    return msg


def test_cansendMsg(msgFraming):

    res= can_p1.canMsgsend(msg)    
    assert res == msg


def test_canrecvMsg(msgFraming):

    res = can_p1.canMdgrcv(msg)    
    assert res == msg

我在IDE中编辑了如下设置:

1)文件->设置->工具->python集成工具->将默认设置为py.测试在

当我运行其他程序(除了pytest)时,我可以看到o/p


Tags: 工具testimportpytestdef测试用例resmsg
3条回答
  1. 您需要按照this guide编辑/创建新配置
  2. 您需要选择python test并在列表中选择pytest:

choose py.test

  1. 通过选择正确的测试文件和虚拟环境来编辑配置: 检查enter image description here
  2. 应用/保存并运行所选配置,它应该可以工作 希望这会有帮助

也许在使用pytest之前已经运行了函数,那么pycharm将使用python(而不是pytest)自动创建一个配置,因此删除run/debug配置中的相关python配置(或所有python)(不要忘记应用)

您配置的设置正确。下一步是为测试创建一个新的运行配置。右键单击文件中的任意位置并导航上下文菜单。查找标记为Create 'py.test in <file_name>.py'...的选项。选择它,就会弹出一个窗口,显示PyCharm将为您的测试文件创建的配置。在这里,您可以添加对配置的修改(目标字段应该已经填充了正确的信息)。完成后,单击“应用”,然后单击“确定”。然后,您可以使用这个配置运行PyTest,方法是从绿色run按钮左侧的下拉列表中选择它。一旦你选择了它,点击运行按钮,开始吧!在

相关问题 更多 >