ljh-swagger 0.0.1
实现了Swagger文档快速生成请求数据，借助主流测试框架可快速完成大批量的接口测试

# ljh_swagger

#### 介绍
swagger快速测试框架

#### 软件架构
设计思路：
源文档-->转换引用模型-->模型属性填充-->转换API-->测试用例/测试运行器（暂不提供具体实现，可根据项目需要自行开发）

#### 安装教程
```
pip install ljh-swagger
```

#### 使用说明
1. 快速开始
```python
'''文档转换'''
import json

from ljh_swagger import loader,swagger

url = 'https://petstore.swagger.io/v2/swagger.json'

api_results = swagger.parse(url)
print('接口数量：{}'.format(len(api_results)))

with open('api.json',mode='w',encoding='utf8') as f:
    f.write(json.dumps(api_results,ensure_ascii=False))

'''api.json内容

[
    {
        "id": "137ddb71e90a4b25ac1848a2b7f2c7fc",
        "name": "uploadFile",
        "method": "post",
        "path": "/v2/pet/{petId}/uploadImage",
        "url": "https://petstore.swagger.io/v2/pet/{petId}/uploadImage",
        "headers": {
            "Content-Type": "multipart/form-data"
        },
        "paths": {
            "petId": 0
        },
        "query": {},
        "json": {},
        "form": {},
        "formData": {
            "additionalMetadata": "string",
            "file": "file.txt"
        }
    },
    {
        "id": "0b40d2503d114d268e514688189768f0",
        "name": "addPet",
        "method": "post",
        "path": "/v2/pet",
        "url": "https://petstore.swagger.io/v2/pet",
        "headers": {
            "Content-Type": "application/json"
        },
        "paths": {},
        "query": {},
        "json": {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "doggie",
            "photoUrls": [],
            "tags": [],
            "status": "string"
        },
        "form": {},
        "formData": {}
    }
    ...
    ...
]
'''
```
2. 接口可用性测试
```python

import unittest
import requests
import os

from ljh_swagger import swagger, path_format, form_format


class APITestCase(unittest.TestCase):
    default_file = 'file.txt'

    @classmethod
    def setUpClass(cls):
        # 准备测试资源
        if not os.path.exists(cls.default_file):
            with open(cls.default_file, mode='w') as f:
                f.write('Hello world!')

    @classmethod
    def tearDownClass(cls):
        # 清理测试资源
        if os.path.exists(cls.default_file):
            os.remove(cls.default_file)

    def test_apis(self):
        url = 'https://petstore.swagger.io/v2/swagger.json'

        api_results = swagger.parse(url)

        for api in api_results:
            with self.subTest(api.get('name')) as st:
                # 请求地址
                url = api.get('url')
                # 请求方法
                method = api.get('method')
                # 请求头
                headers = api.get('headers')
                # 路径参数
                paths = api.get('paths')
                # 查询字串，即query string
                params = api.get('query')
                # 普通表单，即 Content-Type = application/x-www-form-urlencoded
                data = api.get('form')
                # 文件表单, 即 Content-Type = multipart/form-data
                formData = api.get('formData')

                # json格式的参数, 即 Content-Type = application/json
                payload = api.get('json')

                # 文件上传时建议用requests框架的请求头，直接用swagger文件解析出来的会报错，不知道是不是接口做了限制
                if headers.get('Content-Type') == 'multipart/form-data':
                    del headers['Content-Type']
                # 路径参数格式化
                url = path_format(url, paths)

                # 文件表单参数格式化
                formData = form_format(formData)

                res = requests.request(method=method,
                                       url=url,
                                       headers=headers,
                                       params=params,
                                       data=data,
                                       files=formData,
                                       json=payload)
                print(res.text)
                self.assertTrue(res.ok)


if __name__ == '__main__':
    unittest.main()
```