Home > OS >  Call Pytest as a Subprocess
Call Pytest as a Subprocess

Time:10-15

I want to call pytest from a python function. I am able to make it work with os.system. I would like to use a sub process to retrieve the pytest output.

cmd = ["pytest", "-s", debug, "DeviceTests/{0}".format(test_name), "--device_id {0}".format(device_address), "--firmware_version {0}".format(firmware_version)]
print(cmd)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
print(out)

The subprocess does run but tells me there is an error. My pytest requires two CLI options. Running the subprocess I am told the two CLI options are not present.

Here is my cmd and the error.

Running Test DisioOnBACnetTest
['pytest', '-s', '--disable-pytest-warnings', 'DeviceTests/DisioOnBACnetTest', '--device_id 9101', '--firmware_version 0.1.1']
b''
b'ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]\npytest: error: the following arguments are required: --device_id, --firmware_version\n\n'

As you can see I am sending the device_id and firmware_version but the sub process is not properly sending them to pytest.

CodePudding user response:

You need to properly separate the arguments.

This:

"--device_id {0}".format(device_address), "--firmware_version {0}".format(firmware_version)

should be

"--device_id", "{0}".format(device_address), "--firmware_version", "{0}".format(firmware_version)
  • Related