python接口自动化测试

接口关联

下载pytest,allure,requests

import pytest,allure,os,requests
class Testclass():
    jk = ""
    def test001(self):
        global jk
        aa = requests.get("http://apis.juhe.cn/mobile/get?phone=13429667914&key=ef06c0c0573a36268adccac10cb41cfc")
        jk = aa.json()["result"]["city"]
        return aa.json()
    def test002(self):
        url1 = "http://apis.juhe.cn/simpleWeather/query"
        qq = "{'city':jk,'key':'f228ef837921953e9e75b860c8847b5b'}"
        aa = eval(qq)
        bb = requests.get(url=url1,params=aa)
        return bb.json()
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'jieko.py'])  # 生成json类型的测试报告
    # 将测试报告转为html格式
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    os.system(split)

xlsx文件读取,自动化

书写xlsx文件

abcd
http://web.juhe.cn/constellation/getAll{"consName":"金牛座","type":"today","key":"db53d88e528e37b96cf82e79f29bcf39"}get0
http://web.juhe.cn/constellation/getAll{"consName":"金牛","type":"today","key":"db53d88e528e37b96cf82e79f29bcf39"}get205802
http://web.juhe.cn/constellation/getAll{"consName":"金牛座","type":"today","key":"db53d88e528e37b96cbcf39"}get10001
http://web.juhe.cn/constellation/getAll{"consName":"金牛座","type":"today","key":"db53d88e528e37b96cf82e79f29bcf39"}post0

读取xlsx文件

from openpyxl import load_workbook
class UseExcel():
    def get_TestExcel(self):
# 打开表
        workbook =load_workbook('../wenjian/Book1.xlsx')
# 定位表单
        sheet = workbook['Sheet1']
        print(sheet.max_row) #3 行
        print(sheet.max_column) #3 列
        test_data = []#把所有行的数据放到列表中
        for i in range(2,sheet.max_row+1):
            sub_data = {}#把每行的数据放到字典中
            for j in range(1,sheet.max_column+1):
                #字典的key = value
                sub_data[sheet.cell(1,j).value] = sheet.cell(i,j).value
                test_data.append(sub_data)#拼接每行单元格的数据
        return test_data
t = UseExcel()
f = t.get_TestExcel()
print(f)

使用读取文件打印测试报告

import requests,pytest,os,allure
from duwenjian.duxlsx import UseExcel
u = UseExcel()
uu = u.get_TestExcel()
class Testclass():
    def test001(self):
        for i in uu:
            aa = eval(i["b"])
            if i["c"] == "get":
                bb = requests.get(url=i["a"],params=aa)
                cc = bb.json()["error_code"]
                assert cc == int(i["d"])
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report2/result', 'test_002.py'])  # 生成json类型的测试报告
    # 将测试报告转为html格式
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + 'report2/html ' + '--clean'
    os.system(split)

读取csv文件,自动化

书写csv文件

u,k,c,v
http://apis.juhe.cn/mobile/get,"{""phone"":""13429667914"",""key"":""ef06c0c0573a36268adccac10cb41cfc""}",get,0
http://apis.juhe.cn/mobile/get,"{""phone"":""135"",""key"":""ef06c0c0573a36268adccac10cb41cfc""}",get,201102
http://apis.juhe.cn/mobile/get,"{""phone"":""13429667914"",""key"":""ef06c0c0573a36268b43cfc""}",get,10001
http://apis.juhe.cn/mobile/get,"{""phone"":""13429667914"",""key"":""ef06c0c0573a36268adccac10cb41cfc""}",post,0

读取csv文件
import csv,requests
class Csvclass():
    def csvclass(self):
        list = []
        csv1 = csv.reader(open("../wenjian/csvwenjian.csv"))
        for i in csv1:
            list.append(i)
        list = list[1::]
        return list
if __name__ == '__main__':
    c = Csvclass()
    aaa= c.csvclass()
    for i in aaa:
        aaaa = eval(i[1])
        if i[2] == "get":
            aaa1 = requests.get(url=i[0], params=aaaa)
            print(aaa1.json()["error_code"])

使用PYTEST读取文件打印测试报告

import requests,pytest,os,allure
from duwenjian.ducsv import Csvclass
v = Csvclass()
aa = v.csvclass()
class Testclass():
    def test001(self):
        for i in aa:
            aaaa = eval(i[1])
            if i[2] == "get":
                aaa = requests.get(url=i[0], params=aaaa)
                assert aaa.json()["error_code"] == int(i[3])
            else:
                aaa = requests.post(url=i[0], data=aaaa)
                assert aaa.json()["error_code"] == int(i[3])
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'test_001.py'])  # 生成json类型的测试报告
    # 将测试报告转为html格式
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    os.system(split)

使用unittest读取文件打印测试报告

import unittest,requests
from peijian.HTMLTestRunner import HTMLTestRunner
from duwenjian.ducsv import Csvclass
c = Csvclass()
aa = c.csvclass()
class Testclass(unittest.TestCase):
    def test001(self):
        for i in aa:
            aaaa = eval(i[1])
            if i[2] == "get":
                aaa = requests.get(url=i[0],params=aaaa)
                self.assertEqual(aaa.json()["error_code"],int(i[3]))
            else:
                aaa = requests.post(url=i[0],data=aaaa)
                self.assertEqual(aaa.json()["error_code"], int(i[3]))
jiehe = unittest.TestSuite()
list = ["test001"]
for i in list:
    jiehe.addTest(Testclass(i))
with open("../dad.html","wb") as f:
    HTMLTestRunner(
            stream=f,
            title="测试文件",
            description="第一次",
            verbosity=2
         ).run(jiehe)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值