@@ -48,6 +48,15 @@ class RunMode(Enum):
4848 BATCH = "batch"
4949
5050
51+ def should_enable_header_injection () -> bool :
52+ """判断是否需要启用请求头注入功能"""
53+ return os .environ .get ("ENABLE_API_COLLECTING" , "" ) == "1"
54+
55+
56+ if should_enable_header_injection ():
57+ initialize_header_injection ()
58+
59+
5160class PytestExecutor :
5261 def __init__ (
5362 self ,
@@ -62,13 +71,6 @@ def __init__(
6271 self .comment_fields = comment_fields
6372 self .data_drive_key = data_drive_key
6473
65- # 初始化API请求头注入功能,传入判定方法
66- initialize_header_injection ()
67-
68- def _should_enable_header_injection (self ) -> bool :
69- """判断是否需要启用请求头注入功能"""
70- return os .environ .get ("ENABLE_API_COLLECTING" , "" ) == "1"
71-
7274 def pytest_runtest_logstart (self , nodeid : str , location : Any ) -> None :
7375 """
7476 Called at the start of running the runtest protocol for a single item.
@@ -79,7 +81,7 @@ def pytest_runtest_logstart(self, nodeid: str, location: Any) -> None:
7981 testcase_name = normalize_testcase_name (nodeid , self .data_drive_key )
8082
8183 # 设置当前测试用例的nodeid到上下文
82- if self . _should_enable_header_injection ():
84+ if should_enable_header_injection ():
8385 testcase_class_name = testcase_name .split ("?" , 1 )[- 1 ]
8486 set_current_test_nodeid (testcase_class_name )
8587
@@ -228,7 +230,7 @@ def pytest_runtest_logfinish(self, nodeid: str, location: Any) -> None:
228230 self .testdata .pop (testcase_name , None )
229231
230232 # 清除当前测试用例的nodeid
231- if self . _should_enable_header_injection ():
233+ if should_enable_header_injection ():
232234 set_current_test_nodeid (None )
233235
234236 logger .info (f"E { nodeid } runtest_logfinish" )
0 commit comments