当我使用时
from allpairspy import AllPairs
parameters = OrderedDict([('Content-Type', ['"application/json"', ' "application/xml"'])]
test_cases = list(AllPairs(parameters))
它错误以下消息:
Traceback (most recent call last):
File "/home/nio/Documents/projects/ironball/pairwise_engine/gen_pairwise_cases.py", line 60, in <module>
for i, pairs in enumerate(AllPairs(parameters, filter_func=is_valid_combination)):
File "/home/nio/miniconda3/envs/kytest/lib/python3.9/site-packages/allpairspy/allpairs.py", line 60, in __init__
self.__pairs_class = namedtuple("Pairs", self.__param_name_list)
File "/home/nio/miniconda3/envs/kytest/lib/python3.9/collections/__init__.py", line 390, in namedtuple
raise ValueError('Type names and field names must be valid '
ValueError: Type names and field names must be valid identifiers: 'Content-Type'
当我将
'Content-Type'
更改为 'Content_Type'
时,它起作用了。但这里有什么问题呢?如何直接使用'Content-Type'
?
引擎盖下有
namedtuple
,并且 typename
的 field_names
和 namedtuple
都必须是“有效标识符”。
如果字符串仅包含以下内容,则该字符串被视为有效标识符 字母数字字母 (a-z) 和 (0-9) 或下划线 (_)。一个有效的 标识符不能以数字开头,也不能包含任何空格。
要检查此情况
isidentifier
,请使用以下方法:
"Content-Type".isidentifier() # False
"Content_Type".isidentifier() # True
因此,在您的情况下,它返回
False
并命名元组引发错误。