Skip to main content

实现了Swagger文档生成Python请求数据功能,借助主流测试框架可快速完成大批量的接口测试任务。

Project description

Swagger2

介绍

实现了Swagger文档生成Python请求数据功能,借助主流测试框架可快速完成大批量的接口测试任务。

安装教程

pip install swagger2

使用说明

  • 快速开始
'''文档转换'''
import json

import swagger2


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

swagger = swagger2.parse(url)

print('转换接口:{}个'.format(len(swagger.apis)))

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

'''api.json内容

[
    {
        "id": "de0993295bf94750980b3bf62e08a02b",
        "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": "8b5d1baa6cc44e418861bc97c1a04855",
        "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": {}
    },
    ...
    ...
]
'''
  • 接口可用性测试
import unittest
import requests
import os
import warnings

import swagger2

from swagger2 import utils


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

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

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

        cls.swagger = swagger2.parse(url,verify=False)

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

    def test_apis(self):
        for api in self.swagger.apis:
            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框架的请求头
                if headers.get('Content-Type') == 'multipart/form-data':
                    del headers['Content-Type']
                # 路径参数格式化
                url = utils.path_format(url, paths)

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

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


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

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

swagger2-0.0.6.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

swagger2-0.0.6-py3.9.egg (9.3 kB view details)

Uploaded Egg

File details

Details for the file swagger2-0.0.6.tar.gz.

File metadata

  • Download URL: swagger2-0.0.6.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.9

File hashes

Hashes for swagger2-0.0.6.tar.gz
Algorithm Hash digest
SHA256 addabf72897ceb425033ff52c73a5b1ddc45739c4b27f4a83f59961ec90d8d11
MD5 43a345e1f2e8924fda5c63514f042c27
BLAKE2b-256 5d9dc63498f3d705164dc96b2f77887afdbab5c702882af5d0a0d0b450ce804c

See more details on using hashes here.

File details

Details for the file swagger2-0.0.6-py3.9.egg.

File metadata

  • Download URL: swagger2-0.0.6-py3.9.egg
  • Upload date:
  • Size: 9.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.9

File hashes

Hashes for swagger2-0.0.6-py3.9.egg
Algorithm Hash digest
SHA256 7d3f67027c46de9eeae424af3005244242362482a13a51a89522b7f96945c57c
MD5 78307e403d526d21336c13540f66ba21
BLAKE2b-256 addeae953d91c30415cb870a25d802b130c0ff4140d8ffcdf026c217660977f2

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