问题
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
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")
回答1:
subprocess.CompletedProcess(args=[command], returncode = SUCCESSFUL_RETURNCODE)
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
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 ! :)
来源:https://stackoverflow.com/questions/60281122/subprocesses-not-running-with-pytest-error-processing-test