使用unittest進(jìn)行swagger-ui的接口測試--數(shù)據(jù)驅(qū)動-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()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容