子进程未与 pytest 错误处理测试一起运行 [英] Subprocesses not running with pytest Error Processing Test

查看:28
本文介绍了子进程未与 pytest 错误处理测试一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将我的 unittest 设置转换为 pytest,一切正常,但是在某些测试中,我运行命令行进程以便将代码加载到我通过 USB 连接的硬件中.这个过程在 unittest 中运行良好,但是当使用 pytest 或nose2 时我得到了响应

I am currently converting my unittest setup over to pytest, everything is working however on certain tests I run a command line process in order to load code into the hardware I have attached via USB. this process works fine with unittest however when using pytest or nose2 I get the response

------------------------------------------------ Captured stderr call -------------------------------------------------
Error processing Test

这是在我的进程开始运行时发生的吗?我没有收到错误消息,我不确定为什么没有输出?该命令很好,因为它在 cmd 和 unittest 上运行,我是否缺少某些东西才能使其与 pytest 一起使用?

this happens just when my process begins to run? I get no error message am an unsure why one is not output? The command is fine as it runs on cmd and unittest is there something I am missing in order to make it work with pytest?

供参考我正在运行的课程

For reference my class I am running

class LoadCode():

def __init__(self, comport):
    ''' Constructor
    '''

    self.filename = None   
    self.code_comport = comport
    self.code_loaded = False
    self.logger = logging.getLogger(__name__)

def set_code_filename(self, new_file_name):
    ''' Sets the filename parameter for loading the code
    '''
    if (self.filename != new_file_name):  
        self.filename = new_file_name
        self.logger.info("Setting code File to " + self.filename)
        self.code_loaded = False
    else:
        self.logger.info("Code File Name Is Already Set !")

def write_code(self):
    REBOOT_TIME = 50 #approximatly 50 seconds if enough for a reboot after loading boot and main   and enough time for 
    SUCCESSFUL_RETURNCODE = 0   # 0 is a successful return code for subprocess     

    if(self.filename != None and self.code_comport != None):
        #set up command line to run
        command = <<COMMAND>>   
        self.logger.info("Running: " + command)
        #run command line as subprocess (thread will wait for command line to finish)              
        load_code = subprocess.run(command)
        #successful returncode = 0 anything else means an error has occured during subprocess               
        subprocess.CompletedProcess(args=[command], returncode = SUCCESSFUL_RETURNCODE)
        if (load_code.returncode == SUCCESSFUL_RETURNCODE ):
            self.code_loaded = True
            self.logger.info(self.filename) 
            time.sleep(REBOOT_TIME)  #Allow reboot
    else:
        raise AssertionError("ERROR: No code File Set/No Comport Set")
    self.is_code_loaded()

def is_code_loaded(self):
    '''check the bool of code to ensure it has successfully ran
    '''
    if self.code_loaded == False:
        Print("Failed")
        raise AssertionError("Code Was Not Loaded ..")
    else:
        print("WORKED")

推荐答案

subprocess.CompletedProcess(args=[command], returncode = SUCCESSFUL_RETURNCODE)

这行代码不需要,因为它从 subproccess.run() 返回.感谢@Masklinn 指出这一点.详细信息:https://python.readthedocs.io/en/latest/library/subprocess.html?highlight=CompletedProcess

this line of code is not needed as it returns from subproccess.run(). thanks @Masklinn for pointing that out. details: https://python.readthedocs.io/en/latest/library/subprocess.html?highlight=CompletedProcess

生成的路径中间有空格,开头没有空格,这就是命令没有运行并返回错误的原因!现在我的子流程在 pytest 和nose2 上工作得很好!:)

The path that was generated contained spaces in the middle and did not have one at the start which is why the command did not run and the error was returned! now my subprocess work perfectly fine with pytest and nose2 ! :)

这篇关于子进程未与 pytest 错误处理测试一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆