mcpField
A
api-response-mocker

api-response-mocker

github.com/dkyazzentwatwa测试与质量12458 次安装29 周前更新
代码仓库

关于 api-response-mocker

api-response-mocker 使用 Faker 根据自定义模式生成 API 模拟数据,面向开发与测试。它支持嵌套结构、数组、数据关联和可复现输出,并可生成 JSON 或 XML。

主要功能

  1. 01按模式生成响应
  2. 02使用 Faker 生成数据
  3. 03嵌套对象与数组
  4. 04模拟数据间的引用
  5. 05输出 JSON 和 XML
  6. 06基于种子的可复现生成

适用场景

  1. 01为测试创建示例 API 响应
  2. 02生成模拟用户或产品列表
  3. 03按模式生成可重复的数据
  4. 04创建嵌套 JSON 或 XML 测试夹具

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dkyazzentwatwa/chatgpt-skills --skill api-response-mocker

安装后在 Claude Code 中输入/api-response-mocker确认已加载。

附带文件scripts/api_mocker.pyscripts/requirements.txt

详细介绍

API 响应模拟器

使用 Faker 生成逼真的模拟 API 响应和虚假数据。

功能

  • 基于架构生成:定义响应结构
  • Faker 集成:生成逼真的虚假数据
  • 嵌套对象:构建复杂的嵌套结构
  • 数组:生成对象列表
  • 关联关系:引用其他模拟数据
  • 多种格式:输出 JSON、XML

快速入门

from api_mocker import APIMocker

mocker = APIMocker()

# Generate user response
user = mocker.generate({
    "id": "uuid",
    "name": "name",
    "email": "email",
    "created_at": "datetime"
})

# Generate list of users
users = mocker.generate_list({
    "id": "uuid",
    "name": "name",
    "email": "email"
}, count=10)

CLI 用法

# Generate from schema file
python api_mocker.py --schema user_schema.json --output user.json

# Generate list
python api_mocker.py --schema product.json --count 50 --output products.json

# Generate with seed (reproducible)
python api_mocker.py --schema order.json --seed 42 --output order.json

# Preview without saving
python api_mocker.py --schema customer.json --preview

架构格式

使用 Faker 提供方名称定义字段:

{
    "id": "uuid",
    "first_name": "first_name",
    "last_name": "last_name",
    "email": "email",
    "phone": "phone_number",
    "company": "company",
    "address": {
        "street": "street_address",
        "city": "city",
        "state": "state",
        "zip": "zipcode",
        "country": "country"
    },
    "created_at": "date_time_this_year",
    "is_active": "boolean"
}

可用数据类型

个人信息
  • name、first_name、last_name
  • email、safe_email
  • phone_number
  • ssn
地址
  • address、street_address
  • city、state、state_abbr
  • zipcode、postcode
  • country、country_code
  • latitude、longitude
互联网
  • url、domain_name
  • ipv4、ipv6
  • user_name、password
  • uuid、uuid4
  • mac_address
商务
  • company、company_suffix
  • job、job_title
  • bs、catch_phrase
金融
  • credit_card_number
  • iban、bban
  • currency_code
  • price(自定义:返回浮点数)
日期/时间
  • date、time
  • date_time、date_time_this_year
  • date_of_birth
  • iso8601
文本
  • text、sentence、paragraph
  • word、words
  • slug
数值
  • random_int、random_number
  • random_float(使用 {"type": "float", "min": 0, "max": 100})
  • boolean

高级架构

数组
{
    "id": "uuid",
    "name": "name",
    "tags": {
        "_array": true,
        "_count": 3,
        "_item": "word"
    },
    "orders": {
        "_array": true,
        "_count": 5,
        "_item": {
            "order_id": "uuid",
            "amount": "random_int",
            "date": "date"
        }
    }
}
自定义值
{
    "id": "uuid",
    "status": {
        "_choice": ["pending", "active", "completed"]
    },
    "priority": {
        "_range": [1, 5]
    },
    "score": {
        "_float": {"min": 0.0, "max": 100.0, "decimals": 2}
    }
}
嵌套对象
{
    "user": {
        "id": "uuid",
        "profile": {
            "bio": "paragraph",
            "avatar_url": "image_url",
            "social": {
                "twitter": "user_name",
                "linkedin": "url"
            }
        }
    }
}

API 参考

APIMocker 类
class APIMocker:
    def __init__(self, locale: str = "en_US", seed: int = None)

    # Generation
    def generate(self, schema: dict) -> dict
    def generate_list(self, schema: dict, count: int = 10) -> list

    # File operations
    def from_schema_file(self, filepath: str) -> dict
    def save(self, data: any, filepath: str, format: str = "json")

    # Utilities
    def set_seed(self, seed: int)
    def get_faker(self) -> Faker

架构示例

用户响应
{
    "id": "uuid",
    "username": "user_name",
    "email": "email",
    "profile": {
        "first_name": "first_name",
        "last_name": "last_name",
        "avatar": "image_url",
        "bio": "sentence"
    },
    "created_at": "iso8601",
    "last_login": "date_time_this_month"
}
电商产品
{
    "sku": "uuid",
    "name": "catch_phrase",
    "description": "paragraph",
    "price": {"_float": {"min": 9.99, "max": 999.99}},
    "currency": "currency_code",
    "category": {"_choice": ["Electronics", "Clothing", "Home", "Sports"]},
    "in_stock": "boolean",
    "rating": {"_float": {"min": 1, "max": 5, "decimals": 1}},
    "reviews_count": {"_range": [0, 500]}
}
API 错误响应
{
    "error": {
        "code": {"_choice": ["NOT_FOUND", "UNAUTHORIZED", "BAD_REQUEST"]},
        "message": "sentence",
        "request_id": "uuid",
        "timestamp": "iso8601"
    }
}

依赖项

  • faker>=22.0.0

评论

支持 Markdown