Skip to main content

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 测试数据生成和模拟引擎,专为自动化测试和开发环境设计。它提供了灵活的模板系统和代码执行引擎,支持数据库操作、断言验证等功能。

目录

快速开始

安装

pip install mock-engine

基础示例

from mock_engine.core.template_engine import TemplateEngine
from mock_engine.core.code_engine import CodeEngine

# 模板引擎示例
template_engine = TemplateEngine(debug=True)
code_engine = CodeEngine(debug=True)

模板引擎使用指南

1. 基础模板渲染

# 创建模板
template = {
    "greeting": "Hello, @request_body.name!",
    "age": "@request_body.age",
    "headers": {
        "content_type": "@request_header.content-type"
    }
}

# 提供数据
request = {
    "headers": {
        "content-type": "application/json"
    },
    "body": {
        "name": "张三",
        "age": 25
    }
}

# 生成数据
result, debug = template_engine.generate(template, request)

2. 自定义方法

@template_engine.register_method()
def format_greeting(name):
    return f"你好,{name}!"

template = {
    "greeting": "{{format_greeting(name)}}"
}

3. 断言功能

# 定义断言规则
assertions = [
    {"path": "$.age", "op": ">", "value": 18},
    {"path": "$.name", "op": "==", "value": "张三"}
]

# 生成数据并验证断言
result, assertion_results, debug = template_engine.generate_with_assertions(
    template, 
    request, 
    assertions
)

代码引擎使用指南

1. 基础代码执行

code = """
# 获取上下文变量
headers = get_context('headers')
body = get_context('body')

# 修改上下文变量
set_context('headers', {'user': '张三'})

# 设置状态码
set_status_code(201)
"""

result = code_engine.execute(code, request)

2. HTTP 请求

code = """
# GET 请求
response = http_request('GET', 'https://api.example.com/data', 
                       headers={'X-Test': 'test'})

# POST 请求
post_data = {'name': 'test', 'value': 123}
response = http_request('POST', 'https://api.example.com/create', 
                       headers={'Content-Type': 'application/json'},
                       data=post_data)
"""

3. SQL 操作

code = """
# 执行 SQL 查询
result = sql_execute("SELECT * FROM users WHERE id = %s", params=[1])

# 执行 SQL 更新
sql_execute("UPDATE users SET name = %s WHERE id = %s", 
           params=['新名字', 1])
"""

4. 断言功能

code = """
# 基础断言
ASSERT(1 + 1, op='==', expected=2)
ASSERT(5, op='>', expected=3)

# 字符串断言
ASSERT('hello world', op='contains', expected='hello')
"""

调试模式

启用调试

# 模板引擎调试
template_engine = TemplateEngine(debug=True)

# 代码引擎调试
code_engine = CodeEngine(debug=True)

调试输出示例

# 模板引擎调试输出
[
    "开始生成数据",
    "模板内容: {...}",
    "请求数据: {...}",
    "生成完成"
]

# 代码引擎调试输出
[
    "开始执行代码",
    "代码内容: ...",
    "执行SQL: ...",
    "代码执行完成"
]

安装说明

环境要求

  • Python 3.7+
  • pip

安装步骤

# 克隆仓库
git clone https://github.com/yourusername/mock-engine.git

# 创建虚拟环境
python -m venv venv
source venv/bin/activate  # Linux/Mac
# 或
.\venv\Scripts\activate  # Windows

# 安装依赖
pip install -r requirements.txt

注意事项

  1. 调试模式会影响性能,建议只在开发环境使用
  2. 数据库配置信息建议通过配置文件管理
  3. 生产环境请关闭调试模式
  4. 注意保护敏感信息(如数据库密码)

许可证

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

mock_engine-1.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mock_engine-1.0.2-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file mock_engine-1.0.2.tar.gz.

File metadata

  • Download URL: mock_engine-1.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.0b2

File hashes

Hashes for mock_engine-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8f7f671528356855cee0ee212564ea088caef51acf2f1284349e15ab19ddab2e
MD5 ed77723f70e522db6b346feccb1bd1a3
BLAKE2b-256 2813ffc7e347bf430f895477332ba892fad4292d8f4f6524a094017af5d6a1fb

See more details on using hashes here.

File details

Details for the file mock_engine-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: mock_engine-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.0b2

File hashes

Hashes for mock_engine-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 22dc0ed7ebc1fcdacfe5d75c18f01e32d0a79563580bcbd0dacea00472526f68
MD5 87a29198834484e61a8f256600f1daef
BLAKE2b-256 8a1e6667c868a859079258c3532db477c9ea5eaec85d4c94c078565c596812b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page