简体   繁体   English

使用Pycharm(IDE)运行Pytest

[英]Running Pytest using Pycharm(IDE)

I'm trying to run following simple test cases using pycharm, when I ran those test cases I see output 我正在尝试使用pycharm运行以下简单测试用例,当我运行那些测试用例时,我看到输出

Process finished with exit code 0 进程以退出代码0结束

instead pass/fail. 而是通过/失败。

Test Case I was running : 测试用例我正在运行:

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

I have edited settings in IDE as follows: 我在IDE中编辑了如下设置:

1) file -> settings -> tools -> python integrated tools -> set default as py.test 1)文件 - >设置 - >工具 - > python集成工具 - >设置默认为py.test

when I ran other programs(other than pytest) I can see o/p. 当我运行其他程序(除了pytest)我可以看到o / p。

  1. You need to edit/create new configuration by following this guide 您需要按照本指南编辑/创建新配置
  2. you need to choose python test and in the list choose pytest: 你需要选择python test并在列表中选择pytest:

选择py.test

  1. edit the configurations by choosing the correct test file and the virtual environment : check 通过选择正确的测试文件和虚拟环境来编辑配置:检查 在此输入图像描述
  2. Apply/save and run the selected configuration , and it should works hope this will helps 应用/保存并运行所选配置,它应该有用,希望这会有所帮助

The setting you configured is correct. 您配置的设置是正确的。 The next step would be to make a new run configuration for your tests. 下一步是为测试创建新的运行配置。 Right-click anywhere in your file and navigate the context menu. 右键单击文件中的任意位置,然后导航上下文菜单。 Look for an option labeled Create 'py.test in <file_name>.py'... . Create 'py.test in <file_name>.py'...查找标记为Create 'py.test in <file_name>.py'...的选项。 Select it, and a window should pop up showing the config PyCharm will create for your test file. 选择它,然后会弹出一个窗口,显示PyCharm将为您的测试文件创建的配置。 Here you can add modifications to your config (the Target field should already be filled in with the correct info). 在这里,您可以添加对配置的修改(目标字段应该已经填入正确的信息)。 When you are done, click Apply and then OK. 完成后,单击“应用”,然后单击“确定”。 Then you can run PyTest using this config by selecting it from the dropdown to the left of the green Run button. 然后,您可以使用此配置运行PyTest,方法是从绿色“运行”按钮左侧的下拉列表中选择它。 Once you have it selected, click the Run button and give it a go! 选择它后,单击“运行”按钮并开始使用!

也许你在使用pytest之前运行了这个函数,然后pycharm将使用python(而不是pytest)自动创建一个配置,所以在运行/调试配置中删除相对python配置(或所有python)(别忘了申请)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM