55from loguru import logger
66
77CASE_DRIVE_SEPARATOR = "→"
8+ NAME_ATTR_PREFIX = "name="
89
910
1011def selector_to_pytest (test_selector : str ) -> str :
@@ -18,26 +19,34 @@ def selector_to_pytest(test_selector: str) -> str:
1819 # 只要出现 /[ 就说明该 selector 是一个真实的参数化用例节点,
1920 # 即使数据驱动数据中包含 "="(例如 sample_rate=0),也不应该被误判为属性筛选
2021 data_drive_flag = "/["
22+ # 数据驱动里的 & 属于数据内容(例如 [demand_name=测试&author_name=xx]),
23+ # 只有不含数据驱动标记时,& 才表示属性筛选:tests/hello_test.py?name=xxxTest&tag=prod
24+ is_attr_filter = data_drive_flag not in testcase
2125
2226 if "&" in testcase :
2327 testcase_attrs = testcase .split ("&" )
2428 for attr in testcase_attrs :
25- if "name=" in attr : # tests/hello_test.py?name=xxxTest&tag=prod
26- testcase = attr [5 :]
29+ # 属性名只会出现在片段开头,不能用 in 判断:数据驱动中的参数名
30+ # (例如 demand_name=测试、product_name=xx)同样包含 name=,
31+ # 误判后 attr[len(NAME_ATTR_PREFIX):] 会把用例名截断
32+ if is_attr_filter and attr .startswith (NAME_ATTR_PREFIX ):
33+ # tests/hello_test.py?name=xxxTest&tag=prod
34+ testcase = attr [len (NAME_ATTR_PREFIX ) :]
2735 break
28- elif "=" not in attr : # tests/hello_test.py?xxxTest&
36+ elif is_attr_filter and "=" not in attr :
37+ # tests/hello_test.py?xxxTest&tag=prod
2938 testcase = attr
3039 break
3140 else :
3241 # 所有属性均包含 "=",无法区分属性筛选与参数化用例
33- if data_drive_flag not in testcase :
42+ if is_attr_filter :
3443 # 不含数据驱动标记,判定为纯属性筛选
3544 # tests/hello_test.py?test_hello_world&tag=prod
3645 return path
3746 else :
38- if testcase .startswith ("name=" ): # tests/hello_test.py?name=xxxTest
39- testcase = testcase [5 :]
40- elif "=" in testcase and data_drive_flag not in testcase :
47+ if testcase .startswith (NAME_ATTR_PREFIX ): # tests/hello_test.py?name=xxxTest
48+ testcase = testcase [len ( NAME_ATTR_PREFIX ) :]
49+ elif "=" in testcase and is_attr_filter :
4150 # tests/hello_test.py?tag=prod
4251 # 注意:参数化数据中可能包含 "=",例如
4352 # tests/test_asr_websocket.py?TestAsrWebSocket/test_sample_rate_parametrized/[0-default-sample_rate=0-False]
0 commit comments