【发布时间】:2021-10-14 13:37:04
【问题描述】:
我想从 python 函数调用 pytest。我可以使它与os.system 一起工作。我想使用一个子进程来检索 pytest 输出。
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)
子进程确实运行但告诉我有一个错误。我的 pytest 需要两个 CLI 选项。运行子进程,我被告知两个 CLI 选项不存在。
这是我的 cmd 和错误。
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'
如您所见,我正在发送 device_id 和 firmware_version 但子进程未正确将它们发送到 pytest。
【问题讨论】:
-
偏离主题,但如果你在 python3 中,你可以说
f"DeviceTests/{test_name}" -
这是否也给出了脚本的输出?
-
不确定你在问什么。只是指出像
"DeviceTests/{0}".format(test_name)这样的表达式现在可以写成f"DeviceTests/{test_name}"。哪个更简单,更紧凑。 -
我明白了,你在谈论格式化字符串。我虽然你说我可以这样调用脚本。
标签: python python-3.x pytest