我在main_script.sh(bash版本4.4.19(2((中声明了一个关联数组,如下所示:
#!/bin/bash
declare -A hash_map
hash_map["MOCK_KEY"]="99999"
pytest --name abc --strict_mode True --hash_details hash_map ./test_file.py;
我有一个conftest.py文件如下:
parser.addoption("--hash_details", action="store", default="default name")
parser.addoption("--format_name", action="store", default="default name")
parser.addoption("--strict_mode", action="store", default="default name")
test_file.py:的内容
@pytest.fixture()
def name(pytestconfig):
return pytestconfig.getoption("name")
@pytest.fixture()
def strict_mode(pytestconfig):
return pytestconfig.getoption("strict_mode")
@pytest.fixture()
def trace_details(pytestconfig):
return pytestconfig.getoption("hash_details")
def test_function(name, strict_mode, hash_details):
print("hash_details", hash_details)
当我在test_file.py中打印hash_details时,我期望有一个带有bash脚本中定义的键值的字典,但我得到了字符串";hash_map";。如何检索hash_map词典?
我对pytest
不是很熟悉,但Bash不能将对象发送到Python。
Bash中的参数只能解析为字符串。您需要使用JSON或Python/您可以解析/转换为所需类型的其他格式来格式化发送到Python的字符串。
您可以使用sys.argv
来解析从BASH发送到python脚本的参数。
也许pytest在某种程度上已经支持这种参数处理,在这种情况下,您需要研究pytest
的文档