pytest-dsl 0.14.0__py3-none-any.whl → 0.15.1__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.
pytest_dsl/__init__.py CHANGED
@@ -2,9 +2,208 @@
2
2
  pytest-dsl - 基于pytest的DSL测试框架
3
3
 
4
4
  使用自定义的领域特定语言(DSL)来编写测试用例,使测试更加直观、易读和易维护。
5
+
6
+ 主要功能:
7
+ - DSL语法解析和执行
8
+ - 关键字管理和注册
9
+ - Hook机制支持插件扩展
10
+ - DSL格式校验
11
+ - 远程关键字支持
12
+ - 自定义关键字支持
5
13
  """
6
14
 
7
- __version__ = "0.1.0"
15
+ __version__ = "0.1.0"
16
+
17
+ # 核心执行器
18
+ from pytest_dsl.core.dsl_executor import DSLExecutor
19
+
20
+ # 关键字管理器
21
+ from pytest_dsl.core.keyword_manager import keyword_manager, KeywordManager
22
+
23
+ # Hook系统
24
+ from pytest_dsl.core.hookspecs import hookimpl, hookspec, DSLHookSpecs
25
+ from pytest_dsl.core.hook_manager import hook_manager, DSLHookManager
26
+
27
+ # DSL格式校验
28
+ from pytest_dsl.core.validator import (
29
+ DSLValidator,
30
+ DSLValidationError,
31
+ validate_dsl,
32
+ check_dsl_syntax
33
+ )
34
+
35
+ # 自动装饰器
36
+ from pytest_dsl.core.auto_decorator import auto_dsl
37
+
38
+ # 核心工具类
39
+ from pytest_dsl.core.parser import Node, get_parser
40
+ from pytest_dsl.core.lexer import get_lexer
41
+ from pytest_dsl.core.context import TestContext
42
+ from pytest_dsl.core.global_context import global_context
43
+
44
+ # 变量工具
45
+ from pytest_dsl.core.variable_utils import VariableReplacer
46
+
47
+ # 自定义关键字管理器
48
+ from pytest_dsl.core.custom_keyword_manager import custom_keyword_manager
49
+
50
+ # 关键字加载器
51
+ from pytest_dsl.core.keyword_loader import (
52
+ keyword_loader, KeywordLoader,
53
+ load_all_keywords, categorize_keyword, get_keyword_source_info,
54
+ group_keywords_by_source, scan_project_custom_keywords
55
+ )
56
+
57
+ # 关键字工具
58
+ from pytest_dsl.core.keyword_utils import (
59
+ KeywordInfo, KeywordListOptions, KeywordFormatter, KeywordLister,
60
+ keyword_lister, list_keywords, get_keyword_info, search_keywords,
61
+ generate_html_report
62
+ )
63
+
64
+ # 便捷导入的别名
65
+ Executor = DSLExecutor
66
+ Validator = DSLValidator
67
+ HookManager = DSLHookManager
68
+ KeywordLoader = KeywordLoader
69
+
70
+ # 导出所有公共接口
71
+ __all__ = [
72
+ # 版本信息
73
+ '__version__',
74
+
75
+ # 核心执行器
76
+ 'DSLExecutor', 'Executor',
77
+
78
+ # 关键字管理
79
+ 'keyword_manager', 'KeywordManager',
80
+ 'custom_keyword_manager',
81
+
82
+ # 关键字加载器
83
+ 'keyword_loader', 'KeywordLoader',
84
+ 'load_all_keywords', 'categorize_keyword', 'get_keyword_source_info',
85
+ 'group_keywords_by_source', 'scan_project_custom_keywords',
86
+
87
+ # 关键字工具
88
+ 'KeywordInfo', 'KeywordListOptions', 'KeywordFormatter', 'KeywordLister',
89
+ 'keyword_lister', 'list_keywords', 'get_keyword_info', 'search_keywords',
90
+ 'generate_html_report',
91
+
92
+ # Hook系统
93
+ 'hookimpl', 'hookspec', 'DSLHookSpecs',
94
+ 'hook_manager', 'DSLHookManager', 'HookManager',
95
+
96
+ # DSL校验
97
+ 'DSLValidator', 'Validator',
98
+ 'DSLValidationError',
99
+ 'validate_dsl',
100
+ 'check_dsl_syntax',
101
+
102
+ # 自动装饰器
103
+ 'auto_dsl',
104
+
105
+ # 核心组件
106
+ 'Node', 'get_parser', 'get_lexer',
107
+ 'TestContext', 'global_context',
108
+ 'VariableReplacer',
109
+ ]
110
+
111
+ # 快捷函数
112
+
113
+
114
+ def create_executor(enable_hooks: bool = True) -> DSLExecutor:
115
+ """创建DSL执行器实例
116
+
117
+ Args:
118
+ enable_hooks: 是否启用hook机制
119
+
120
+ Returns:
121
+ DSL执行器实例
122
+ """
123
+ return DSLExecutor(enable_hooks=enable_hooks)
124
+
125
+
126
+ def parse_dsl(content: str) -> Node:
127
+ """解析DSL内容为AST
128
+
129
+ Args:
130
+ content: DSL内容
131
+
132
+ Returns:
133
+ 解析后的AST根节点
134
+ """
135
+ lexer = get_lexer()
136
+ parser = get_parser()
137
+ return parser.parse(content, lexer=lexer)
138
+
139
+
140
+ def execute_dsl(content: str, context: dict = None, enable_hooks: bool = True) -> any:
141
+ """执行DSL内容的便捷函数
142
+
143
+ Args:
144
+ content: DSL内容
145
+ context: 执行上下文(可选)
146
+ enable_hooks: 是否启用hook机制
147
+
148
+ Returns:
149
+ 执行结果
150
+ """
151
+ executor = create_executor(enable_hooks=enable_hooks)
152
+ if context:
153
+ executor.variables.update(context)
154
+ for key, value in context.items():
155
+ executor.test_context.set(key, value)
156
+
157
+ ast = parse_dsl(content)
158
+ return executor.execute(ast)
159
+
160
+
161
+ def register_keyword(name: str, parameters: list = None, source_type: str = "external",
162
+ source_name: str = "user_defined"):
163
+ """注册关键字的装饰器
164
+
165
+ Args:
166
+ name: 关键字名称
167
+ parameters: 参数列表
168
+ source_type: 来源类型
169
+ source_name: 来源名称
170
+
171
+ Returns:
172
+ 装饰器函数
173
+ """
174
+ if parameters is None:
175
+ parameters = []
176
+
177
+ return keyword_manager.register_with_source(
178
+ name=name,
179
+ parameters=parameters,
180
+ source_type=source_type,
181
+ source_name=source_name
182
+ )
183
+
184
+
185
+ # 版本兼容性检查
186
+ def check_version_compatibility():
187
+ """检查版本兼容性"""
188
+ try:
189
+ import sys
190
+ if sys.version_info < (3, 7):
191
+ import warnings
192
+ warnings.warn(
193
+ "pytest-dsl 需要 Python 3.7 或更高版本",
194
+ UserWarning,
195
+ stacklevel=2
196
+ )
197
+ except Exception:
198
+ pass
199
+
200
+
201
+ # 初始化时进行版本检查
202
+ check_version_compatibility()
8
203
 
9
- # 导出 auto_dsl 装饰器,使其可以直接从包导入
10
- from pytest_dsl.core.auto_decorator import auto_dsl
204
+ # 自动初始化hook管理器
205
+ try:
206
+ hook_manager.initialize()
207
+ except Exception as e:
208
+ import warnings
209
+ warnings.warn(f"Hook管理器初始化失败: {e}", UserWarning, stacklevel=2)