使用unittest进行swagger-ui的接口测试--数据驱动-yaml-ddt

test_data.yaml
yaml文件使用格式:
-test_post_tag:
url: https://petstore.swagger.io/v2/pet/8
method: POST
hearder:
Content-Type: application/x-www-form-urlencoded
payload:
name: duod3o
status: nor3mal
validate:
{"status_code":200}
-test_get_pet_by_id:
url: https://petstore.swagger.io/v2/pet/8
method: GET
hearder:
Content-Type:
payload:
validate:
{"status_code":200}
-test_post_add_pet:
url: https://petstore.swagger.io/v2/pet
method: POST
hearder:
Content-Type: application/json
payload:
{
"id": 8,
"category": {
"id": 7,
"name": "whoami"
},
"name": "doggie",
"photoUrls": [
"http://www.baidu.com"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
validate:
{"status_code":200}


代码
TestSwaggerddt.py

http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

import unittest
import requests
from ddt import ddt, file_data, unpack
import yaml
@ddt
class TestSwaggerDdt(unittest.TestCase):

@file_data('test_data.yaml')
@unpack
def test_post_pet(self, **kwargs):

    url = kwargs.get("url")
    method = kwargs.get("method")
    header = kwargs.get("hearder")['Content-Type']
    data = kwargs.get("payload")
    ok = kwargs.get("validate")

    print(url, method, data, ok, type(data),header)
    if header == 'application/json':
        res = requests.request(method=method, url=url, json=data, )
    else:
        res = requests.request(method=method, url=url, data=data, )
    self.assertEqual(200, res.status_code)

if name == 'main':
unittest.main()

你可能感兴趣的:(使用unittest进行swagger-ui的接口测试--数据驱动-yaml-ddt)