python接口自动化测试实战 无涯_python - 接口自动化测试实战 - case1 - 优化版

题目:

基于以下两个接口和数据完成接口自动化测试,并生成测试报告:

'''

登录

login='http://47.107.168.87:8080/futureloan/mvc/api/member/login'

login_data={'mobilephone':18688773467,'pwd':'123456'}

充值

recharge='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'

recharge_data={'mobilephone':18688773467,'amount':'1000'}

'''

test_excel.xlsx:

L3Byb3h5L2h0dHBzL2ltZzIwMTguY25ibG9ncy5jb20vYmxvZy8xNTE1NTIzLzIwMTgxMi8xNTE1NTIzLTIwMTgxMjA1MTQxNzMwODk3LTEzNzAyNDYyODEucG5n.jpg

http_request.py

# -*- coding:utf-8 -*-

'''

@project: jiaxy

@author: Jimmy

@file: http_request.py

@ide: PyCharm Community Edition

@time: 2018-12-05 10:06

@blog: https://www.cnblogs.com/gotesting/

'''

import requests

class HttpRequest:

def http_request(self,url,param,method,cookies=None):

if method == 'get':

res = requests.get(url,param,cookies = cookies)

elif method == 'post':

res = requests.post(url,param,cookies = cookies)

else:

print('请求方法错误!')

return res

read_excel.py

# -*- coding:utf-8 -*-

'''

@project: jiaxy

@author: Jimmy

@file: read_excel.py

@ide: PyCharm Community Edition

@time: 2018-12-05 11:57

@blog: https://www.cnblogs.com/gotesting/

'''

from openpyxl import load_workbook

class ReadExcel:

def read_excel(self):

wb = load_workbook('test_excel.xlsx')

sheet = wb['登录及充值测试数据']

test_data = []

for i in range(2,sheet.max_row+1):

sub_data = {}

sub_data['url'] = sheet.cell(i,1).value

sub_data['param'] = eval(sheet.cell(i,2).value)

sub_data['method'] = sheet.cell(i,3).value

sub_data['expected'] = sheet.cell(i,4).value

test_data.append(sub_data)

return test_data

test_api.py

# -*- coding:utf-8 -*-

'''

@project: jiaxy

@author: Jimmy

@file: TestApi.py

@ide: PyCharm Community Edition

@time: 2018-12-05 10:09

@blog: https://www.cnblogs.com/gotesting/

'''

import unittest

from TestApi.http_request import HttpRequest

cookies = None

class TestHttpApi(unittest.TestCase):

def setUp(self):

pass

def tearDown(self):

pass

# 改写__init__方法,使用超继承

def __init__(self,url,param,method,expected,methodName):

self.url = url

self.param = param

self.method = method

self.expected = expected

super(TestHttpApi,self).__init__(methodName)

def test_api(self):

global cookies

res = HttpRequest().http_request(self.url,self.param,self.method,cookies)

print('请求结果:',res.json())

if res.cookies:

cookies = res.cookies

try:

self.assertEquals(self.expected,res.json()['msg'])

except AssertionError as e:

print('断言异常:',e)

raise e

test_suite.py

# -*- coding:utf-8 -*-

'''

@project: jiaxy

@author: Jimmy

@file: test_suite.py

@ide: PyCharm Community Edition

@time: 2018-12-05 10:28

@blog: https://www.cnblogs.com/gotesting/

'''

import unittest

import HTMLTestRunner

from TestApi.test_api import TestHttpApi

from TestApi.read_excel import ReadExcel

# 定义测试数据列表

test_data = ReadExcel().read_excel()

print(test_data)

'''

test_data = [{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':'','pwd':'123456'},'method':'get','expected':'手机号不能为空'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':''},'method':'get','expected':'密码不能为空'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':'12345678'},'method':'get','expected':'用户名或密码错误'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':'123456'},'method':'get','expected':'登录成功'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':'','amount':'1000'},'method':'post','expected':'手机号不能为空'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':1868877346,'amount':'1000'},'method':'post','expected':'手机号码格式不正确'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':''},'method':'post','expected':'请输入金额'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':'100000000000'},'method':'post','expected':'请输入范围在0到50万之间的正数金额'},

{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':'1000'},'method':'post','expected':'充值成功'}]

'''

# 加载测试集

suite = unittest.TestSuite()

for item in test_data:

suite.addTest(TestHttpApi(item['url'],

item['param'],

item['method'],

item['expected'],

'test_api'))

# 执行测试,输出测试报告

with open('TestApi.html','wb+') as file:

runner = HTMLTestRunner.HTMLTestRunner(file)

runner.run(suite)

测试报告:

L3Byb3h5L2h0dHBzL2ltZzIwMTguY25ibG9ncy5jb20vYmxvZy8xNTE1NTIzLzIwMTgxMi8xNTE1NTIzLTIwMTgxMjA1MTQ1MzM3MjEwLTIxMjUwMzA4NzIucG5n.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值