pytest-dsl 0.13.0__py3-none-any.whl → 0.14.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  """YAML变量加载器模块
2
2
 
3
3
  该模块负责处理YAML变量文件的加载和管理,支持从命令行参数加载单个文件或目录。
4
+ 同时支持通过hook机制从外部系统动态加载变量。
4
5
  """
5
6
 
6
7
  import os
@@ -29,14 +30,25 @@ def add_yaml_options(parser):
29
30
  )
30
31
 
31
32
 
32
- def load_yaml_variables_from_args(yaml_files=None, yaml_vars_dir=None, project_root=None):
33
+ def load_yaml_variables_from_args(yaml_files=None, yaml_vars_dir=None,
34
+ project_root=None, environment=None):
33
35
  """从参数加载YAML变量文件(通用函数)
34
36
 
35
37
  Args:
36
38
  yaml_files: YAML文件列表
37
39
  yaml_vars_dir: YAML变量目录路径
38
40
  project_root: 项目根目录(用于默认config目录)
41
+ environment: 环境名称(用于hook加载)
39
42
  """
43
+ # 首先尝试通过hook加载变量
44
+ hook_variables = _load_variables_through_hooks(
45
+ project_root=project_root, environment=environment)
46
+
47
+ if hook_variables:
48
+ print(f"通过Hook加载了 {len(hook_variables)} 个变量")
49
+ # 将hook变量加载到yaml_vars中
50
+ yaml_vars._variables.update(hook_variables)
51
+
40
52
  # 加载单个YAML文件
41
53
  if yaml_files:
42
54
  yaml_vars.load_yaml_files(yaml_files)
@@ -55,7 +67,8 @@ def load_yaml_variables_from_args(yaml_files=None, yaml_vars_dir=None, project_r
55
67
  if loaded_files:
56
68
  # 过滤出当前目录的文件
57
69
  if yaml_vars_dir:
58
- dir_files = [f for f in loaded_files if Path(f).parent == Path(yaml_vars_dir)]
70
+ dir_files = [f for f in loaded_files if Path(
71
+ f).parent == Path(yaml_vars_dir)]
59
72
  if dir_files:
60
73
  print(f"目录中加载的文件: {', '.join(dir_files)}")
61
74
  else:
@@ -67,6 +80,103 @@ def load_yaml_variables_from_args(yaml_files=None, yaml_vars_dir=None, project_r
67
80
  load_remote_servers_from_yaml()
68
81
 
69
82
 
83
+ def _load_variables_through_hooks(project_root=None, environment=None):
84
+ """通过hook机制加载变量
85
+
86
+ Args:
87
+ project_root: 项目根目录
88
+ environment: 环境名称
89
+
90
+ Returns:
91
+ dict: 通过hook加载的变量字典
92
+ """
93
+ try:
94
+ from .hook_manager import hook_manager
95
+
96
+ # 确保hook管理器已初始化
97
+ hook_manager.initialize()
98
+
99
+ # 如果没有已注册的插件,直接返回
100
+ if not hook_manager.get_plugins():
101
+ return {}
102
+
103
+ # 提取project_id(如果可以从项目根目录推断)
104
+ project_id = None
105
+ if project_root:
106
+ # 可以根据项目结构推断project_id,这里暂时不实现
107
+ pass
108
+
109
+ # 通过hook加载变量
110
+ hook_variables = {}
111
+
112
+ # 调用dsl_load_variables hook
113
+ try:
114
+ variable_results = hook_manager.pm.hook.dsl_load_variables(
115
+ project_id=project_id,
116
+ environment=environment,
117
+ filters={}
118
+ )
119
+
120
+ # 合并所有hook返回的变量
121
+ for result in variable_results:
122
+ if result and isinstance(result, dict):
123
+ hook_variables.update(result)
124
+
125
+ except Exception as e:
126
+ print(f"通过Hook加载变量时出现警告: {e}")
127
+
128
+ # 列出变量源(用于调试)
129
+ try:
130
+ source_results = hook_manager.pm.hook.dsl_list_variable_sources(
131
+ project_id=project_id
132
+ )
133
+
134
+ sources = []
135
+ for result in source_results:
136
+ if result and isinstance(result, list):
137
+ sources.extend(result)
138
+
139
+ if sources:
140
+ print(f"发现 {len(sources)} 个变量源")
141
+ for source in sources:
142
+ source_name = source.get('name', '未知')
143
+ source_type = source.get('type', '未知')
144
+ print(f" - {source_name} ({source_type})")
145
+
146
+ except Exception as e:
147
+ print(f"列出变量源时出现警告: {e}")
148
+
149
+ # 验证变量(如果有变量的话)
150
+ if hook_variables:
151
+ try:
152
+ validation_results = hook_manager.pm.hook.dsl_validate_variables(
153
+ variables=hook_variables,
154
+ project_id=project_id
155
+ )
156
+
157
+ validation_errors = []
158
+ for result in validation_results:
159
+ if result and isinstance(result, list):
160
+ validation_errors.extend(result)
161
+
162
+ if validation_errors:
163
+ print(f"变量验证发现 {len(validation_errors)} 个问题:")
164
+ for error in validation_errors:
165
+ print(f" - {error}")
166
+
167
+ except Exception as e:
168
+ print(f"验证变量时出现警告: {e}")
169
+
170
+ return hook_variables
171
+
172
+ except ImportError:
173
+ # 如果没有安装pluggy或hook_manager不可用,跳过hook加载
174
+ return {}
175
+ except Exception as e:
176
+ print(f"Hook变量加载失败: {e}")
177
+ return {}
178
+
179
+
70
180
  def load_yaml_variables(config):
71
181
  """加载YAML变量文件(pytest插件接口)
72
182
 
@@ -80,60 +190,50 @@ def load_yaml_variables(config):
80
190
  yaml_vars_dir = config.getoption('--yaml-vars-dir')
81
191
  project_root = config.rootdir
82
192
 
193
+ # 尝试从环境变量获取环境名称
194
+ environment = os.environ.get(
195
+ 'PYTEST_DSL_ENVIRONMENT') or os.environ.get('ENVIRONMENT')
196
+
83
197
  # 调用通用加载函数
84
198
  load_yaml_variables_from_args(
85
199
  yaml_files=yaml_files,
86
200
  yaml_vars_dir=yaml_vars_dir,
87
- project_root=project_root
201
+ project_root=project_root,
202
+ environment=environment
88
203
  )
89
204
 
90
205
 
91
206
  def load_remote_servers_from_yaml():
92
- """从YAML配置中自动加载远程服务器
93
-
94
- 检查YAML变量中是否包含remote_servers配置,如果有则自动连接这些服务器。
95
- """
207
+ """从YAML变量中加载远程服务器配置"""
96
208
  try:
209
+ from pytest_dsl.remote.keyword_client import remote_keyword_manager
210
+
97
211
  # 获取远程服务器配置
98
- remote_servers_config = yaml_vars.get_variable('remote_servers')
99
- if not remote_servers_config:
100
-
212
+ remote_servers = yaml_vars.get_variable('remote_servers')
213
+ if not remote_servers:
101
214
  return
102
215
 
103
- print(f"发现 {len(remote_servers_config)} 个远程服务器配置")
104
-
105
- # 导入远程关键字管理器
106
- from pytest_dsl.remote.keyword_client import remote_keyword_manager
216
+ print(f"发现 {len(remote_servers)} 个远程服务器配置")
107
217
 
108
- # 遍历配置并连接服务器
109
- for server_name, server_config in remote_servers_config.items():
110
- try:
218
+ # 注册远程服务器
219
+ for server_config in remote_servers:
220
+ if isinstance(server_config, dict):
111
221
  url = server_config.get('url')
112
- alias = server_config.get('alias', server_name)
222
+ alias = server_config.get('alias')
113
223
  api_key = server_config.get('api_key')
114
- sync_config = server_config.get('sync_config')
115
-
116
- if not url:
117
- print(f"跳过服务器 {server_name}: 缺少URL配置")
118
- continue
119
-
120
- print(f"正在连接远程服务器: {server_name} ({url}) 别名: {alias}")
121
-
122
- # 注册远程服务器
123
- success = remote_keyword_manager.register_remote_server(
124
- url=url,
125
- alias=alias,
126
- api_key=api_key,
127
- sync_config=sync_config
128
- )
129
-
130
- if success:
131
- print(f"成功连接到远程服务器: {server_name} ({url})")
132
- else:
133
- print(f"连接远程服务器失败: {server_name} ({url})")
134
-
135
- except Exception as e:
136
- print(f"连接远程服务器 {server_name} 时发生错误: {str(e)}")
137
224
 
225
+ if url and alias:
226
+ print(f"自动连接远程服务器: {alias} -> {url}")
227
+ success = remote_keyword_manager.register_remote_server(
228
+ url, alias, api_key=api_key
229
+ )
230
+ if success:
231
+ print(f"✓ 远程服务器 {alias} 连接成功")
232
+ else:
233
+ print(f"✗ 远程服务器 {alias} 连接失败")
234
+
235
+ except ImportError:
236
+ # 如果远程功能不可用,跳过
237
+ pass
138
238
  except Exception as e:
139
- print(f"加载远程服务器配置时发生错误: {str(e)}")
239
+ print(f"自动连接远程服务器时出现警告: {e}")
@@ -5,22 +5,32 @@ from pathlib import Path
5
5
 
6
6
 
7
7
  class YAMLVariableManager:
8
- """管理YAML格式的变量文件"""
8
+ """管理YAML格式的变量文件,支持hook扩展"""
9
9
 
10
10
  def __init__(self):
11
11
  self._loaded_files: List[str] = []
12
12
  self._variables: Dict[str, Any] = {}
13
+ self._enable_hooks = True # 是否启用hook
13
14
 
14
15
  def has_variable(self, name: str) -> bool:
15
16
  """检查变量是否存在
16
-
17
+
17
18
  Args:
18
19
  name: 变量名
19
-
20
+
20
21
  Returns:
21
22
  bool: 变量是否存在
22
23
  """
23
- return name in self._variables
24
+ # 首先检查本地变量
25
+ if name in self._variables:
26
+ return True
27
+
28
+ # 如果启用了hook,尝试通过hook查找
29
+ if self._enable_hooks:
30
+ hook_value = self._get_variable_through_hook(name)
31
+ return hook_value is not None
32
+
33
+ return False
24
34
 
25
35
  def load_yaml_file(self, file_path: str) -> None:
26
36
  """加载单个YAML文件中的变量"""
@@ -54,8 +64,73 @@ class YAMLVariableManager:
54
64
  self.load_yaml_file(str(yaml_file))
55
65
 
56
66
  def get_variable(self, name: str) -> Optional[Any]:
57
- """获取变量值"""
58
- return self._variables.get(name)
67
+ """获取变量值,支持hook扩展
68
+
69
+ 优先级:
70
+ 1. 本地YAML变量
71
+ 2. Hook提供的变量
72
+
73
+ Args:
74
+ name: 变量名
75
+
76
+ Returns:
77
+ 变量值,如果不存在返回None
78
+ """
79
+ # 首先从本地变量获取
80
+ if name in self._variables:
81
+ return self._variables[name]
82
+
83
+ # 如果启用了hook,尝试通过hook获取
84
+ if self._enable_hooks:
85
+ hook_value = self._get_variable_through_hook(name)
86
+ if hook_value is not None:
87
+ # 可选:将hook获取的变量缓存到本地
88
+ # self._variables[name] = hook_value
89
+ return hook_value
90
+
91
+ return None
92
+
93
+ def _get_variable_through_hook(self, name: str) -> Optional[Any]:
94
+ """通过hook获取变量值
95
+
96
+ Args:
97
+ name: 变量名
98
+
99
+ Returns:
100
+ 变量值,如果不存在返回None
101
+ """
102
+ try:
103
+ from .hook_manager import hook_manager
104
+
105
+ # 确保hook管理器已初始化
106
+ hook_manager.initialize()
107
+
108
+ # 如果没有已注册的插件,直接返回
109
+ if not hook_manager.get_plugins():
110
+ return None
111
+
112
+ # 尝试从环境变量获取当前环境
113
+ environment = os.environ.get(
114
+ 'PYTEST_DSL_ENVIRONMENT') or os.environ.get('ENVIRONMENT')
115
+
116
+ # 调用dsl_get_variable hook
117
+ variable_results = hook_manager.pm.hook.dsl_get_variable(
118
+ var_name=name,
119
+ project_id=None, # 可以根据需要传递project_id
120
+ environment=environment
121
+ )
122
+
123
+ # 返回第一个非None的结果
124
+ for result in variable_results:
125
+ if result is not None:
126
+ return result
127
+
128
+ return None
129
+
130
+ except Exception as e:
131
+ # Hook调用失败时记录警告但不影响正常流程
132
+ print(f"通过hook获取变量失败: {e}")
133
+ return None
59
134
 
60
135
  def get_all_variables(self) -> Dict[str, Any]:
61
136
  """获取所有已加载的变量"""
@@ -65,6 +140,14 @@ class YAMLVariableManager:
65
140
  """获取已加载的文件列表"""
66
141
  return self._loaded_files.copy()
67
142
 
143
+ def set_enable_hooks(self, enable: bool) -> None:
144
+ """设置是否启用hook
145
+
146
+ Args:
147
+ enable: 是否启用hook
148
+ """
149
+ self._enable_hooks = enable
150
+
68
151
  def clear(self) -> None:
69
152
  """清除所有已加载的变量"""
70
153
  self._variables.clear()
@@ -72,4 +155,4 @@ class YAMLVariableManager:
72
155
 
73
156
 
74
157
  # 创建全局YAML变量管理器实例
75
- yaml_vars = YAMLVariableManager()
158
+ yaml_vars = YAMLVariableManager()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-dsl
3
- Version: 0.13.0
3
+ Version: 0.14.0
4
4
  Summary: A DSL testing framework based on pytest
5
5
  Author: Chen Shuanglin
6
6
  License: MIT
@@ -1,5 +1,5 @@
1
1
  pytest_dsl/__init__.py,sha256=FzwXGvmuvMhRBKxvCdh1h-yJ2wUOnDxcTbU4Nt5fHn8,301
2
- pytest_dsl/cli.py,sha256=kFGOoGQ3dzi5C895QPawnAkif6UmQ4f9zFZmmag0Zgo,34336
2
+ pytest_dsl/cli.py,sha256=Pt1cyaQ9rd31LWC1e-8IqS-xqvPzBmFXjdw2QAruyvU,35689
3
3
  pytest_dsl/conftest_adapter.py,sha256=cevEb0oEZKTZfUrGe1-CmkFByxKhUtjuurBJP7kpLc0,149
4
4
  pytest_dsl/main_adapter.py,sha256=pUIPN_EzY3JCDlYK7yF_OeLDVqni8vtG15G7gVzPJXg,181
5
5
  pytest_dsl/plugin.py,sha256=Ymepaknms6N5mMoFgtiVi4YYiycE8a_6BPCBONmN-W8,1901
@@ -8,10 +8,14 @@ pytest_dsl/core/auth_provider.py,sha256=IZfXXrr4Uuc8QHwRPvhHSzNa2fwrqhjYts1xh78D
8
8
  pytest_dsl/core/auto_decorator.py,sha256=9Mga-GB4AzV5nkB6zpfaq8IuHa0KOH8LlFvnWyH_tnU,6623
9
9
  pytest_dsl/core/auto_directory.py,sha256=egyTnVxtGs4P75EIivRauLRPJfN9aZpoGVvp_Ma72AM,2714
10
10
  pytest_dsl/core/context.py,sha256=fXpVQon666Lz_PCexjkWMBBr-Xcd1SkDMp2vHar6eIs,664
11
- pytest_dsl/core/custom_keyword_manager.py,sha256=vFXRfKXOu7CxoLsLCd6B2TI_JgOrfnE8JN9iShdv_hw,12308
12
- pytest_dsl/core/dsl_executor.py,sha256=T-8_U3s3KD3HIZ0fWcydftOh9qAwWZDbSNxiT2SBDeA,32039
11
+ pytest_dsl/core/custom_keyword_manager.py,sha256=F9aSbth4x4-5nHb0N5uG04CpgwGnQv4RDGeRKR2WuIk,16001
12
+ pytest_dsl/core/dsl_executor.py,sha256=PQvr7tbCM-ULPa6-2sBzaNFL7Q20xRSXZV-iNxFG9C0,38914
13
13
  pytest_dsl/core/dsl_executor_utils.py,sha256=cFoR2p3qQ2pb-UhkoefleK-zbuFqf0aBLh2Rlp8Ebs4,2180
14
14
  pytest_dsl/core/global_context.py,sha256=NcEcS2V61MT70tgAsGsFWQq0P3mKjtHQr1rgT3yTcyY,3535
15
+ pytest_dsl/core/hook_manager.py,sha256=rqpcj9tBcxwFpoU2ZAfOWO8P0oyq02Ie_XUjq-9vFc0,2445
16
+ pytest_dsl/core/hookable_executor.py,sha256=sxkyWF7bSkKzgVoNwUTvtDGOSgFKXXQHR5uYIf-9MaM,3969
17
+ pytest_dsl/core/hookable_keyword_manager.py,sha256=e_guTVF1tU3pu_uzAUd4xNudFlYth42Ll2ESeRLlpjE,3833
18
+ pytest_dsl/core/hookspecs.py,sha256=fBmvs8uIV9_M_FUQBgvHSDn51Z6ABXo9k0okf80wzeo,5071
15
19
  pytest_dsl/core/http_client.py,sha256=hdx8gI4JCmq1-96pbiKeyKzSQUzPAi08cRNmljiPQmY,15536
16
20
  pytest_dsl/core/http_request.py,sha256=6e-gTztH3wu2eSW27Nc0uPmyWjB6oBwndx8Vqnu5uyg,60030
17
21
  pytest_dsl/core/keyword_manager.py,sha256=hoNXHQcumnufPRUobnY0mnku4CHxSg2amwPFby4gQEs,7643
@@ -21,8 +25,8 @@ pytest_dsl/core/parsetab.py,sha256=o4XbFKwpsi3fYmfI_F6u5NSM61Qp6gTx-Sfh1jDINxI,3
21
25
  pytest_dsl/core/plugin_discovery.py,sha256=3pt3EXJ7EPF0rkUlyDZMVHkIiTy2vicdIIQJkrHXZjY,8305
22
26
  pytest_dsl/core/utils.py,sha256=q0AMdw5HO33JvlA3UJeQ7IXh1Z_8K1QlwiM1_yiITrU,5350
23
27
  pytest_dsl/core/variable_utils.py,sha256=sx5DFZAO_syi0E_0ACisiZUQL9yxGeOfNmMtV6w9V3E,13947
24
- pytest_dsl/core/yaml_loader.py,sha256=IOzRP3iEaVJ-4UiZGkuwy5VAD8_pWQjKHD0tZhwIGb8,4858
25
- pytest_dsl/core/yaml_vars.py,sha256=4Z0q9_ergpDjMrNUS0qU5oHrPPLYOL91bWoNxCioSLw,2551
28
+ pytest_dsl/core/yaml_loader.py,sha256=PC-R05bmHDI1QyBhhnt2jEK2c50Sg2xKsMDDY7p5rRg,8165
29
+ pytest_dsl/core/yaml_vars.py,sha256=PqbCGT_TmOXH09Pmm72sYtMEvV-sp9ocLqkuAUQYhhc,5047
26
30
  pytest_dsl/docs/custom_keywords.md,sha256=03dA_GeHxoLixA8Sqch14bhTbxXQCSfz9Kvged2fMCo,4381
27
31
  pytest_dsl/examples/__init__.py,sha256=FKkyLFOjxfC6XnJlW7iK_BUIX9dTIpXgoYN1pfs91ss,84
28
32
  pytest_dsl/examples/test_assert.py,sha256=zJ9bflsbyyl7v5AKdLHYTVPI4pe8QiQoeym-ULAs0tk,343
@@ -65,9 +69,9 @@ pytest_dsl/remote/keyword_client.py,sha256=BL80MOaLroUi0v-9sLtkJ55g1R0Iw9SE1k11I
65
69
  pytest_dsl/remote/keyword_server.py,sha256=vGIE3Bhh461xX_u1U-Cf5nrWL2GQFYdtQdcMWfFIYgE,22320
66
70
  pytest_dsl/remote/variable_bridge.py,sha256=dv-d3Gq9ttvvrXM1fdlLtoSOPB6vRp0_GBOwX4wvcy8,7121
67
71
  pytest_dsl/templates/keywords_report.html,sha256=7x84iq6hi08nf1iQ95jZ3izcAUPx6JFm0_8xS85CYws,31241
68
- pytest_dsl-0.13.0.dist-info/licenses/LICENSE,sha256=Rguy8cb9sYhK6cmrBdXvwh94rKVDh2tVZEWptsHIsVM,1071
69
- pytest_dsl-0.13.0.dist-info/METADATA,sha256=Z3ywUBY2yKn3V6BouBNFEcZ1l1D6I-_S2-NLCGBcGeE,29655
70
- pytest_dsl-0.13.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
71
- pytest_dsl-0.13.0.dist-info/entry_points.txt,sha256=PLOBbH02OGY1XR1JDKIZB1Em87loUvbgMRWaag-5FhY,204
72
- pytest_dsl-0.13.0.dist-info/top_level.txt,sha256=4CrSx4uNqxj7NvK6k1y2JZrSrJSzi-UvPZdqpUhumWM,11
73
- pytest_dsl-0.13.0.dist-info/RECORD,,
72
+ pytest_dsl-0.14.0.dist-info/licenses/LICENSE,sha256=Rguy8cb9sYhK6cmrBdXvwh94rKVDh2tVZEWptsHIsVM,1071
73
+ pytest_dsl-0.14.0.dist-info/METADATA,sha256=Qryl8-XUSV2y1G1BEXS6RSnmjqKfTRdlTtfPPhi5kEU,29655
74
+ pytest_dsl-0.14.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
75
+ pytest_dsl-0.14.0.dist-info/entry_points.txt,sha256=PLOBbH02OGY1XR1JDKIZB1Em87loUvbgMRWaag-5FhY,204
76
+ pytest_dsl-0.14.0.dist-info/top_level.txt,sha256=4CrSx4uNqxj7NvK6k1y2JZrSrJSzi-UvPZdqpUhumWM,11
77
+ pytest_dsl-0.14.0.dist-info/RECORD,,