A powerful Python mock engine for API testing, template rendering, and code execution with debugging and assertion support.
Project description
Mock Engine
Mock Engine 是一个强大的 Python 模拟执行引擎,提供了模板渲染、结构化断言和代码执行功能,支持调试模式,帮助开发者更好地理解和调试接口或脚本执行过程。
功能特点
- 模板引擎:支持变量替换、自定义方法、结构化断言
- 代码引擎:支持 SQL 查询、上下文变量、灵活的代码级断言
- 调试模式:详细的执行日志和调试信息
- 数据库支持:内置 SQL 查询功能
- 上下文管理:支持变量传递和共享
- 通用断言:在 code 脚本中可直接用 ASSERT 进行多类型断言
安装
pip install mock-engine
快速开始
模板引擎示例
from mock_engine.core.template_engine import TemplateEngine
engine = TemplateEngine(debug=True)
template = {
"greeting": "Hello, @name!",
"name": "@name",
"age": "@age",
"faker_name": "@faker.name",
"nested": {
"level1": {
"level2": "@nested.level1.level2"
}
}
}
request_data = {
"name": "John",
"age": 25,
"nested": {"level1": {"level2": "deep"}}
}
# 定义结构化断言
assertions = [
{"path": "$.age", "op": ">", "value": 18},
{"path": "$.name", "op": "==", "value": "John"},
{"path": "$.nested.level1.level2", "op": "==", "value": "deep"},
{"path": "$.faker_name", "op": "len>", "value": 0}
]
result, assertion_results, debug = engine.generate_with_assertions(template, request_data, assertions)
print("渲染结果:", result)
print("断言结果:", assertion_results)
print("调试信息:", debug)
代码引擎示例
from mock_engine.core.code_engine import CodeEngine
engine = CodeEngine(debug=True)
# 代码模式下的断言和SQL示例
code = '''
ASSERT("hello world", op="contains", expected="hello")
ASSERT(5, op=">", expected=3)
ASSERT("abc123", op="regex", expected=r"\\d+")
ASSERT("foo", op="==", expected="foo")
ASSERT("bar", op="not_contains", expected="baz")
result = "断言全部通过"
'''
output = engine.execute(code)
print(output)
通用断言(代码模式)
在 code 脚本中可直接使用 ASSERT 进行多类型断言:
ASSERT("hello world", op="contains", expected="hello")
ASSERT(5, op=">", expected=3)
ASSERT("abc123", op="regex", expected=r"\\d+")
ASSERT("foo", op="==", expected="foo")
ASSERT("bar", op="not_contains", expected="baz")
支持的操作符包括:==, !=, >, <, >=, <=, contains, not_contains, regex, startswith, endswith。
断言失败会自动抛出 AssertionError 并在 execute 返回结果的 error 字段体现。
文档
开发
环境设置
- 克隆仓库:
git clone https://github.com/Yarsar-l/mock_engine.git
cd mock_engine
- 创建虚拟环境:
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
- 安装依赖:
pip install -r requirements.txt
贡献
欢迎提交 Pull Request 或创建 Issue。
许可证
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mock_engine-1.0.1.tar.gz.
File metadata
- Download URL: mock_engine-1.0.1.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.0b2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e4222292cd0faeeb74c256656f487c6da8914e9ad2580c4c6f812e95cd00504
|
|
| MD5 |
24d049b5da1c9df999f55a061cea8e06
|
|
| BLAKE2b-256 |
1867c4da4700a0afb06ddca79f307d0eabb5cfead1fe3144fdfadf7730167540
|
File details
Details for the file mock_engine-1.0.1-py3-none-any.whl.
File metadata
- Download URL: mock_engine-1.0.1-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.0b2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d6e3857854e9c56f99e4510316d27097415a938127b071b0569604146778d3e
|
|
| MD5 |
3d1be6ee781210e8cbca69bbf7eeb742
|
|
| BLAKE2b-256 |
8d4f29b7c87a327a23cd1712adf19761bfa089d1e12438d70f019f9ea3de741c
|