
本文详解如何通过 `--credentials` 等自定义命令行参数,将结构化 json 数据(如凭据)可靠传递给 pytest,避免 shell 解析错误(如 `zsh: event not found`),并推荐使用 `json.dumps()` + `subprocess.run()` 的安全实践。
在 Pytest 中通过 --addoption 接收 JSON 格式参数(例如认证凭据)是常见需求,但直接在 Shell 命令中拼接 JSON 字符串极易引发解析异常——你遇到的 zsh: event not found: \\ 错误,正是由于 Bash/Zsh 将反斜杠 \ 识别为历史扩展或转义符号,而非 JSON 字符串的一部分。手动替换双引号(如 replace('"', '\\"'))不仅不可靠,还会因 Shell 层级、引号嵌套和平台差异导致行为不一致。
✅ 正确做法:绕过 Shell,使用 subprocess.run() 直接调用 Python 进程
不要用 os.system() 或字符串拼接构造带引号的 shell 命令,而是将参数组织为纯 Python 列表,交由 subprocess 安全分发:
import json
import subprocess
# 构建结构化凭据数据(Python dict)
credentials = {
"vcenter": {"username": "admin@vsphere.local", "password": "vc-pass-123"},
"vm": {"username": "Administrator", "password": "win-pass-456"}
}
# 构造 pytest 命令参数列表(无 shell 解析,无引号逃逸问题)
cmd = [
"python3",
"-m", "pytest",
"/usr/local/auto/tests/shared/test_shared.py",
"--vm-name", "my_vm",
"--vm-ip", "10.10.10.10",
"--credentials", json.dumps(credentials), # ✅ 自动处理引号与转义
"-rA", "--capture=tee-sys", "--show-capture=no",
"--disable-pytest-warnings",
"--junit-xml=/tmp/test_shared.xml"
]
# 安全执行(shell=False 是默认值,显式强调更佳)
result = subprocess.run(cmd, capture_output=True, text=True)
print("Return code:", result.returncode)
if result.stdout:
print("stdout:\n", result.stdout)
if result.stderr:
print("stderr:\n", result.stderr)? 关键要点说明:
- json.dumps(credentials) 生成标准 JSON 字符串(如 {"vcenter": {"username": "..."}}),内部双引号自动转义,且不包含任何 shell 元字符;
- subprocess.run(cmd) 以 shell=False(默认)方式执行,参数逐个传递给 python3 进程,完全规避 Shell 解析阶段;
- 在 conftest.py 中,type=load_credentials 函数应直接 json.loads(value) 解析该字符串,无需额外去反斜杠;
- ❌ 避免 shell=True + 手动拼接字符串(如 f"... --credentials '{json_str}'"),这是绝大多数转义失败的根源。
? 补充建议:
若必须通过 shell 调用(如 CI 脚本中),可改用单引号包裹整个 JSON 字符串,并确保内部不含单引号(或用 '"' 拼接),但该方案脆弱且难维护,强烈推荐优先采用 subprocess.run() 列表传参方式。
综上,安全传递 JSON 参数的核心原则是:让数据保持为 Python 对象 → 序列化为标准 JSON 字符串 → 通过进程参数列表直接传递 → 由 Pytest 解析器接收并反序列化。这一链路彻底脱离 Shell 的干扰,既健壮又可移植。










