【问题标题】:Call Pytest as a Subprocess [duplicate]将 Pytest 作为子进程调用 [重复]
【发布时间】: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


【解决方案1】:

您需要正确分隔参数。

这个:

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

应该是

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

【讨论】:

  • 我在原帖中更新了我的 cmd。我遇到了同样的问题,没有运行。
  • @Michael --device_id 和 9101 需要是两个单独的参数(它们自己的字符串,而不是一个组合字符串!!),--firmware_version 和 0.1.1 也是如此
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 2020-02-26
相关资源
最近更新 更多