hellchin-lib 0.1.1__tar.gz → 0.1.2__tar.gz

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.
Files changed (26) hide show
  1. {hellchin_lib-0.1.1/hellchin_lib.egg-info → hellchin_lib-0.1.2}/PKG-INFO +1 -1
  2. hellchin_lib-0.1.2/README.md +65 -0
  3. {hellchin_lib-0.1.1/_hellchin_lib/soft_assert → hellchin_lib-0.1.2/_hellchin_lib/assertion}/method_assert.py +146 -46
  4. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/hellchin_lib/__init__.py +3 -3
  5. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2/hellchin_lib.egg-info}/PKG-INFO +1 -1
  6. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/hellchin_lib.egg-info/SOURCES.txt +6 -5
  7. hellchin_lib-0.1.2/pyproject.toml +7 -0
  8. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/setup.py +5 -1
  9. hellchin_lib-0.1.2/tests/test_soft_assert/test_demo.py +40 -0
  10. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/tests/test_soft_assert/test_method_assert.py +1 -1
  11. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/tests/test_soft_assert/test_soft_assert.py +1 -1
  12. hellchin_lib-0.1.1/README.md +0 -0
  13. hellchin_lib-0.1.1/pyproject.toml +0 -3
  14. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/LICENSE +0 -0
  15. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/_hellchin_lib/__init__.py +0 -0
  16. {hellchin_lib-0.1.1/_hellchin_lib/soft_assert → hellchin_lib-0.1.2/_hellchin_lib/assertion}/__init__.py +0 -0
  17. {hellchin_lib-0.1.1/_hellchin_lib/soft_assert → hellchin_lib-0.1.2/_hellchin_lib/assertion}/base_soft_assert.py +0 -0
  18. {hellchin_lib-0.1.1/_hellchin_lib/soft_assert → hellchin_lib-0.1.2/_hellchin_lib/assertion}/logger.py +0 -0
  19. {hellchin_lib-0.1.1/_hellchin_lib/soft_assert → hellchin_lib-0.1.2/_hellchin_lib/assertion}/postman.py +0 -0
  20. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/_hellchin_lib/logger.py +0 -0
  21. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/hellchin_lib.egg-info/dependency_links.txt +0 -0
  22. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/hellchin_lib.egg-info/requires.txt +0 -0
  23. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/hellchin_lib.egg-info/top_level.txt +0 -0
  24. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/setup.cfg +0 -0
  25. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/tests/__init__.py +0 -0
  26. {hellchin_lib-0.1.1 → hellchin_lib-0.1.2}/tests/test_soft_assert/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hellchin_lib
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: A Python library for soft assertions and flexible testing.
5
5
  Author: hellchin
6
6
  Author-email: your.email@example.com
@@ -0,0 +1,65 @@
1
+ # hellchin-lib
2
+
3
+ ## 简介
4
+ 个人开发使用的一些工具包,主要用于自动化测试,目前主要开发断言功能,支持软硬断言。
5
+
6
+ ## 安装
7
+ ```bash
8
+ pip install hellchin-lib
9
+ ```
10
+
11
+ ## 使用方法
12
+
13
+ !!!详细教程有空的时候再进行更新
14
+
15
+ 建议教程如下:
16
+
17
+ 示例代码:
18
+ ```python
19
+ # !/usr/bin/env python
20
+ # -*- coding: utf-8 -*-
21
+ """
22
+ -------------------------------------------------
23
+ File Name: test_demo.py
24
+ Description:
25
+ -------------------------------------------------
26
+ """
27
+
28
+ from hellchin_lib import PostManAssertMethod, MethodAssert
29
+
30
+ pm = PostManAssertMethod()
31
+ pm.response = {
32
+ "a": 1
33
+ }
34
+
35
+ # raise Exception("测试")
36
+ pm_code_test = """
37
+ # pm.expect(1).to.equal(2)
38
+ pm.test("", lambda: pm.expect('1').to.equal(1))
39
+ pm.test("失败断言", lambda: pm.expect(1).to.equal(2))
40
+ pm.test("", lambda: pm.expect(1).to.equal(1))
41
+ pm.test("成功断言", lambda: pm.expect(1).to.equal(1))
42
+ res = pm.response.json()
43
+ pm.test("undefined 测试", lambda: pm.expect(res.b).to.equal(1))
44
+ # pm.expect(res.b).to.equal(1)
45
+ """
46
+
47
+ exec(pm_code_test, {"pm": pm})
48
+
49
+ ma_code_test = """
50
+ method_assert.check(1 == 1)
51
+ method_assert.check(1 == 2)
52
+ method_assert.equal(1, 1)
53
+ method_assert.equal(1, 2)
54
+ method_assert.exists(1)
55
+ method_assert.fail()
56
+ """
57
+
58
+ # exec(ma_code_test, {"method_assert": MethodAssert()})
59
+
60
+ ```
61
+
62
+
63
+ ## 文档链接
64
+
65
+ 更多文档请访问:None
@@ -12,7 +12,35 @@
12
12
 
13
13
  from typing import Union, Any
14
14
 
15
- from _hellchin_lib.soft_assert.base_soft_assert import SoftAssert
15
+ import requests
16
+ from box import Box
17
+
18
+ from _hellchin_lib.assertion.base_soft_assert import SoftAssert
19
+
20
+
21
+ class AssertMsg:
22
+
23
+ def __init__(self, *args, actual, expected, context):
24
+ self.msg = args[0]
25
+ self.actual = actual
26
+ self.expected = expected
27
+ self.context = context
28
+
29
+ def __call__(self, *args, **kwargs):
30
+ return self.msg # 默认返回第一个参数作为断言信息
31
+
32
+
33
+ class AssertionUndefinedError(AssertionError):
34
+ pass
35
+
36
+
37
+ class Undefined:
38
+ def __repr__(self):
39
+ return "undefined"
40
+
41
+
42
+ # 实例化一个特殊对象
43
+ UNDEFINED = Undefined()
16
44
 
17
45
 
18
46
  class BaseMethodAssert:
@@ -22,6 +50,9 @@ class BaseMethodAssert:
22
50
 
23
51
  def __init__(self, expected_value: Any = None):
24
52
  super().__init__()
53
+
54
+ self.IS_UNDEFINED = isinstance(expected_value, Undefined) # 判断是否是未定义的对象
55
+
25
56
  self.expected = expected_value
26
57
 
27
58
  @staticmethod
@@ -41,8 +72,7 @@ class BaseMethodAssert:
41
72
  else:
42
73
  raise ValueError(f"Unsupported assert_data type: {type(assert_data)}")
43
74
 
44
- @staticmethod
45
- def _generate_assert_messages(success_condition: str, fail_message: str, actual, expected) -> tuple:
75
+ def _generate_assert_messages(self, success_condition: str, fail_message: str, actual=None, expected=None) -> tuple:
46
76
  """
47
77
  生成断言的成功和失败信息。
48
78
 
@@ -52,9 +82,17 @@ class BaseMethodAssert:
52
82
  :param expected: 预期值
53
83
  :return: 成功信息和失败信息
54
84
  """
85
+
55
86
  assert_info_success = success_condition
56
- assert_info_fail = f"{assert_info_success} | AssertionError: {fail_message.format(actual=repr(actual), expected=repr(expected))}"
57
- return assert_info_success, assert_info_fail
87
+ fail_message = fail_message.format(actual=repr(actual), expected=repr(expected))
88
+ assert_info_fail = f"{assert_info_success} | AssertionError: {fail_message}"
89
+ assert_raise_msg = AssertMsg(assert_info_fail, actual=actual, expected=expected, context=fail_message)
90
+
91
+ # 如果实际值是未定义的,则抛出 AssertionUndefinedError
92
+ if self.IS_UNDEFINED:
93
+ raise AssertionUndefinedError(assert_raise_msg) # 抛出一个错误对象,具体的错误信息由 assert_raise_msg 提供
94
+
95
+ return assert_info_success, assert_raise_msg
58
96
 
59
97
  def equal(self, actual, /, expected=None, *, assert_name=None, jsonpath: str = None):
60
98
  """
@@ -67,8 +105,8 @@ class BaseMethodAssert:
67
105
  raise ValueError("Expected value cannot be None.")
68
106
 
69
107
  success_message, fail_message = self._generate_assert_messages(
70
- assert_name or f"{jsonpath or actual} == {repr(expected)}",
71
- "expected {actual} to deeply equal {expected} ",
108
+ assert_name or f"{repr(expected)} == {jsonpath or actual}",
109
+ "expected {expected} to deeply equal {actual} ",
72
110
  actual, expected
73
111
  )
74
112
 
@@ -76,9 +114,6 @@ class BaseMethodAssert:
76
114
 
77
115
  return success_message
78
116
 
79
- # 执行断言并记录结果
80
- # return self.check(actual == expected, success_message, fail_message)
81
-
82
117
  def not_equal(self, actual, expected=None, assert_name=None, jsonpath: str = None):
83
118
  """
84
119
  判断响应不相等的逻辑。
@@ -90,7 +125,7 @@ class BaseMethodAssert:
90
125
  raise ValueError("Expected value cannot be None.")
91
126
 
92
127
  success_message, fail_message = self._generate_assert_messages(
93
- assert_name or f"{jsonpath or actual} != {repr(expected)}",
128
+ assert_name or f"{repr(expected)} != {jsonpath or actual}",
94
129
  "expected {actual} not to deeply equal {expected} ",
95
130
  actual, expected
96
131
  )
@@ -99,9 +134,6 @@ class BaseMethodAssert:
99
134
 
100
135
  return success_message
101
136
 
102
- # 执行断言并记录结果
103
- # self.check(actual != expected, success_message, fail_message)
104
-
105
137
  def greater_than(self, actual, expected=None, assert_name=None, jsonpath: str = None):
106
138
  """
107
139
  判断响应大于的逻辑。
@@ -122,9 +154,6 @@ class BaseMethodAssert:
122
154
 
123
155
  return success_message
124
156
 
125
- # 执行断言并记录结果
126
- # self.check(actual > expected, success_message, fail_message)
127
-
128
157
  def greater_than_or_equal(self, actual, expected=None, *, assert_name=None, jsonpath: str = None) -> None:
129
158
  """
130
159
  判断响应值是否大于或等于预期值的逻辑。
@@ -214,48 +243,60 @@ class BaseMethodAssert:
214
243
 
215
244
  return success_message
216
245
 
217
- # 执行断言并记录结果
218
- # self.check(actual <= expected, success_message, fail_message)
219
-
220
- def exists(self, actual, expected=None, *, assert_name=None, jsonpath: str = None) -> None:
246
+ @property
247
+ def exists(self):
221
248
  """
222
249
  判断 key 是否存在的逻辑。
250
+ self, expected=None, *, assert_name=None, jsonpath: str = None
223
251
  """
224
252
 
225
- expected = self.expected or expected
253
+ def func(*args, **kwargs):
254
+ print(f"IS_UNDEFINED: {self.IS_UNDEFINED}")
226
255
 
227
- if expected is None:
228
- raise ValueError("Expected value cannot be None.")
256
+ expected = self.expected or args[0]
229
257
 
230
- success_message, fail_message = self._generate_assert_messages(
231
- assert_name or f"{jsonpath or actual} exists",
232
- "expected {actual} to exist",
233
- actual, expected
234
- )
258
+ desc = kwargs.get("desc", None)
235
259
 
236
- assert actual in expected, fail_message
260
+ if expected is None:
261
+ raise ValueError("Expected value cannot be None.")
237
262
 
238
- return success_message
263
+ success_message, fail_message = self._generate_assert_messages(
264
+ desc or f"{expected} exists",
265
+ "expected {expected} to exist",
266
+ expected=expected
267
+ )
268
+
269
+ assert bool(expected), fail_message
270
+
271
+ return success_message
239
272
 
240
- def not_exists(self, actual, expected=None, *, assert_name=None, jsonpath: str = None) -> None:
273
+ return func
274
+
275
+ @property
276
+ def not_exists(self):
241
277
  """
242
278
  判断 key 不存在的逻辑。
243
279
  """
244
280
 
245
- expected = self.expected or expected
281
+ def func(*args, **kwargs) -> str:
282
+ expected = self.expected or args[0]
246
283
 
247
- if expected is None:
248
- raise ValueError("Expected value cannot be None.")
284
+ desc = kwargs.get("desc", None)
249
285
 
250
- success_message, fail_message = self._generate_assert_messages(
251
- assert_name or f"{jsonpath or actual} not exists",
252
- "expected {actual} to not exist",
253
- actual, expected
254
- )
286
+ if expected is None:
287
+ raise ValueError("Expected value cannot be None.")
255
288
 
256
- assert actual not in expected, fail_message
289
+ success_message, fail_message = self._generate_assert_messages(
290
+ desc or f"{expected} does not exist",
291
+ "expected {expected} to not exist",
292
+ expected=expected
293
+ )
257
294
 
258
- return success_message
295
+ assert not bool(expected), fail_message
296
+
297
+ return success_message
298
+
299
+ return func
259
300
 
260
301
 
261
302
  class Expect:
@@ -270,9 +311,11 @@ class Expect:
270
311
  class PostManAssertMethod:
271
312
 
272
313
  def __init__(self):
273
- self.expect = Expect # 初始化Expect对象
314
+ # self.expect = Expect # 初始化Expect对象
315
+ self._response = dict() # 初始化响应对象
274
316
  self.__success_icon = "✅ "
275
317
  self.__failure_icon = "❌ "
318
+ self.test_results_list = [] # 初始化结果列表
276
319
 
277
320
  @staticmethod
278
321
  def _format_assert_error(desc: str, error_message: str) -> str:
@@ -286,17 +329,74 @@ class PostManAssertMethod:
286
329
 
287
330
  @staticmethod
288
331
  def test(desc, test_func=None):
332
+ """
333
+ 测试方法,用于执行测试函数,并打印测试结果。
334
+
335
+ test 方法要对抛出的异常信息做特殊化处理,用来区分硬断言
336
+ :param desc:
337
+ :param test_func:
338
+ :return:
339
+ """
289
340
  try:
290
- # print(f"Running test: {desc}")
291
341
  assert_result = test_func()
292
342
  print(f"✅ {desc if desc else assert_result}")
293
343
 
344
+ # 自定义异常类,捕捉 expect(参数) 参数为正确传入的情况
345
+ except AssertionUndefinedError as e:
346
+ print(PostManAssertMethod._format_assert_error(desc, e.args[0].msg))
347
+
294
348
  except AssertionError as e:
295
349
  # 如果断言失败,我们需要处理下断言信息
296
- print(PostManAssertMethod._format_assert_error(desc, e.args[0]))
350
+ print(PostManAssertMethod._format_assert_error(desc, e.args[0].msg))
351
+
352
+ except AttributeError as e:
353
+ print("❌ {desc} | {exception_type}: {exception_msg}".format(
354
+ desc=desc,
355
+ exception_type="AssertionError",
356
+ exception_msg=e.args[0]
357
+ ))
297
358
 
298
359
  except Exception as e:
299
- print("⚠️ Unexpected Error: {}".format(e))
360
+ print("⚠️ 未处理的异常错误,请检查: {}".format(e))
361
+
362
+ @property
363
+ def response(self):
364
+ """获取响应"""
365
+
366
+ class ResponseProxy:
367
+ def __init__(self, response):
368
+ self._response = response
369
+
370
+ def json(self):
371
+
372
+ # 判断响应类型
373
+ if isinstance(self._response, requests.Response):
374
+ dict_response = self._response.json()
375
+ elif isinstance(self._response, dict):
376
+ dict_response = self._response
377
+ else:
378
+ raise TypeError(f"Invalid response type: {type(self._response)}")
379
+
380
+ return Box(
381
+ dict_response,
382
+ default_box=True, # 是否在访问不存在的键时返回空的 Box 对象
383
+ default_box_attr=UNDEFINED, # 不存在的键返回默认值 undefined
384
+ )
385
+
386
+ def __getattr__(self, item):
387
+ # 代理其他属性和方法到原始 response 对象
388
+ return getattr(self._response, item)
389
+
390
+ return ResponseProxy(self._response)
391
+
392
+ @response.setter
393
+ def response(self, value):
394
+ """设置响应"""
395
+ self._response = value
396
+
397
+ @staticmethod
398
+ def expect(expected_value: Any):
399
+ return Expect(expected_value)
300
400
 
301
401
 
302
402
  class MethodAssert(SoftAssert):
@@ -8,9 +8,9 @@
8
8
  """
9
9
 
10
10
  from _hellchin_lib.logger import LoggerManager
11
- from _hellchin_lib.soft_assert.base_soft_assert import SoftAssert
12
- from _hellchin_lib.soft_assert.method_assert import MethodAssert
13
- from _hellchin_lib.soft_assert.method_assert import PostManAssertMethod
11
+ from _hellchin_lib.assertion.base_soft_assert import SoftAssert
12
+ from _hellchin_lib.assertion.method_assert import MethodAssert
13
+ from _hellchin_lib.assertion.method_assert import PostManAssertMethod
14
14
 
15
15
  # 对外暴露的模块
16
16
  __all__ = [
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hellchin_lib
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: A Python library for soft assertions and flexible testing.
5
5
  Author: hellchin
6
6
  Author-email: your.email@example.com
@@ -4,11 +4,11 @@ pyproject.toml
4
4
  setup.py
5
5
  _hellchin_lib/__init__.py
6
6
  _hellchin_lib/logger.py
7
- _hellchin_lib/soft_assert/__init__.py
8
- _hellchin_lib/soft_assert/base_soft_assert.py
9
- _hellchin_lib/soft_assert/logger.py
10
- _hellchin_lib/soft_assert/method_assert.py
11
- _hellchin_lib/soft_assert/postman.py
7
+ _hellchin_lib/assertion/__init__.py
8
+ _hellchin_lib/assertion/base_soft_assert.py
9
+ _hellchin_lib/assertion/logger.py
10
+ _hellchin_lib/assertion/method_assert.py
11
+ _hellchin_lib/assertion/postman.py
12
12
  hellchin_lib/__init__.py
13
13
  hellchin_lib.egg-info/PKG-INFO
14
14
  hellchin_lib.egg-info/SOURCES.txt
@@ -17,5 +17,6 @@ hellchin_lib.egg-info/requires.txt
17
17
  hellchin_lib.egg-info/top_level.txt
18
18
  tests/__init__.py
19
19
  tests/test_soft_assert/__init__.py
20
+ tests/test_soft_assert/test_demo.py
20
21
  tests/test_soft_assert/test_method_assert.py
21
22
  tests/test_soft_assert/test_soft_assert.py
@@ -0,0 +1,7 @@
1
+ [build-system]
2
+ requires = ["setuptools", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [tool.poetry]
6
+ readme = "README.md"
7
+ changelog = "CHANGELOG.md"
@@ -6,11 +6,13 @@
6
6
  Description: 包的元信息
7
7
  -------------------------------------------------
8
8
  """
9
+ from pathlib import Path
10
+
9
11
  from setuptools import setup, find_packages
10
12
 
11
13
  setup(
12
14
  name="hellchin_lib", # 包名
13
- version="0.1.1", # 版本号
15
+ version="0.1.2", # 版本号
14
16
  description="A Python library for soft assertions and flexible testing.", # 描述
15
17
  author="hellchin", # 作者
16
18
  author_email="your.email@example.com", # 作者邮箱
@@ -23,4 +25,6 @@ setup(
23
25
  "License :: OSI Approved :: MIT License",
24
26
  "Operating System :: OS Independent",
25
27
  ],
28
+ # long_description=(Path("README.md").read_text() + "\n\n" + Path("CHANGELOG.md").read_text()),
29
+ # long_description_content_type="text/markdown",
26
30
  )
@@ -0,0 +1,40 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ -------------------------------------------------
5
+ File Name: test_demo.py
6
+ Description:
7
+ -------------------------------------------------
8
+ """
9
+
10
+ from hellchin_lib import PostManAssertMethod, MethodAssert
11
+
12
+ pm = PostManAssertMethod()
13
+ pm.response = {
14
+ "a": 1
15
+ }
16
+
17
+ # raise Exception("测试")
18
+ pm_code_test = """
19
+ # pm.expect(1).to.equal(2)
20
+ pm.test("", lambda: pm.expect('1').to.equal(1))
21
+ pm.test("失败断言", lambda: pm.expect(1).to.equal(2))
22
+ pm.test("", lambda: pm.expect(1).to.equal(1))
23
+ pm.test("成功断言", lambda: pm.expect(1).to.equal(1))
24
+ res = pm.response.json()
25
+ pm.test("undefined 测试", lambda: pm.expect(res.b).to.equal(1))
26
+ # pm.expect(res.b).to.equal(1)
27
+ """
28
+
29
+ exec(pm_code_test, {"pm": pm})
30
+
31
+ ma_code_test = """
32
+ method_assert.check(1 == 1)
33
+ method_assert.check(1 == 2)
34
+ method_assert.equal(1, 1)
35
+ method_assert.equal(1, 2)
36
+ method_assert.exists(1)
37
+ method_assert.fail()
38
+ """
39
+
40
+ # exec(ma_code_test, {"method_assert": MethodAssert()})
@@ -10,7 +10,7 @@ import unittest
10
10
 
11
11
  import pytest
12
12
 
13
- from _hellchin_lib.soft_assert.method_assert import MethodAssert
13
+ from _hellchin_lib.assertion.method_assert import MethodAssert
14
14
 
15
15
 
16
16
  class TestMethodAssert:
@@ -6,7 +6,7 @@
6
6
  Description:
7
7
  -------------------------------------------------
8
8
  """
9
- from _hellchin_lib.soft_assert import MethodAssert
9
+ from _hellchin_lib.assertion import MethodAssert
10
10
 
11
11
 
12
12
  def test_soft_assert():
File without changes
@@ -1,3 +0,0 @@
1
- [build-system]
2
- requires = ["setuptools", "wheel"]
3
- build-backend = "setuptools.build_meta"
File without changes
File without changes