我有一个 YAML 文件,其中数据包含字符串和整数。当加载 YAML 文件并生成字典时,YAML “智能地”将不带引号的整数设为 int 类型。有没有什么简单的方法可以让 YAML 将所有内容视为字符串,而无需在所有整数周围添加双引号?
以下是 YAML 文件的一部分:
devices:
rtr1:
reachable: True
instances: [Region-58,]
system_id: 0101.8800.0008
level: Level-2
isis_lsps:
- lsp_id: RTR1CA22CWP.00-00
instance: Region-58
prot_supported: "IPv4, IPv6"
hostname: RTR1CA22CWP
te_router_id: 10.1.0.8
extended_reach:
- ext_reach_id: 0101.8890.0207.00
metric: 10
adm_group: 0x601
local_int_ip: 10.14.2.21
remote_int_ip: 10.14.2.20
max_bw: 400000
max_reserve_bw: 380000
te_metric: 10
Run Code Online (Sandbox Code Playgroud)
使用yaml_load后,数字的类型为“int”。
例如,公制:10
无论如何,是否可以让 yaml_load 将数字 10 视为字符串,而不必在其周围添加双引号。
我使用 pytest_generate_tests 挂钩使用外部 YAML 文件中定义的变量对 pytest 测试进行参数化。变量文件的名称在 pytest 命令行上指定(--params_file)。只有模块内的某些测试函数被参数化,并且需要此文件中的变量。因此,定义变量的命令行选项是一个可选参数。如果从命令行中省略了可选参数,那么我希望 pytest 只是“跳过”那些需要外部参数化变量的测试函数,而只运行未参数化的“其他”测试。问题是,如果省略命令行选项,pytest 将跳过所有测试函数,而不仅仅是需要参数的测试函数。
这是测试模块文件:
def test_network_validate_1(logger, device_connections,):
### Test code omitted.....
def test_lsp_throttle_timers(params_file, logger, device_connections):
### Test code omitted.....
def test_network_validate_2(logger, device_connections,):
### Test code omitted.....
Run Code Online (Sandbox Code Playgroud)
conftest.py 中的 pytest_generate_tests 挂钩:
# Note, I tried scope at function level as well but that did not help
@pytest.fixture(scope='session')
def params_file(request):
pass
def pytest_generate_tests(metafunc):
### Get Pytest rootdir
rootdir = metafunc.config.rootdir
print(f"*********** Test Function: {metafunc.function.__name__}")
if "params_file" in metafunc.fixturenames:
print("*********** Hello Silver ****************")
if …Run Code Online (Sandbox Code Playgroud)