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:
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)
测试报告:
python - 接口自动化测试实战 - case1 - 优化版的更多相关文章
- python - 接口自动化测试实战 - case1 - 再次优化版
本次优化: 1. 各级分Package 2. 封装[ReadExcel]类 3. 封装[ReadConfig]类 4. 封装[GetLog]类 5. 引入ddt数据驱动测试,优化测试用例代码 ...
- Python接口自动化测试实战-----附源码
目录 1. 接口定义 2. 基本流程 3. 需求分析 4. 用例设计 5. 脚本开发 6. 结果分析 接口定义: 接口普遍有两种意思,一种是API(Application Program Interf ...
- ptyhon - 接口自动化测试实战case1
work_20181203_httprequest.py: import requestsclass http_request: def http_get(url,params): res = req ...
- Python接口自动化测试框架实战 从设计到开发
第1章 课程介绍(不要错过)本章主要讲解课程的详细安排.课程学习要求.课程面向用户等,让大家很直观的对课程有整体认知! 第2章 接口测试工具Fiddler的运用本章重点讲解如何抓app\web的htt ...
- 基于Python接口自动化测试框架+数据与代码分离(进阶篇)附源码
引言 在上一篇<基于Python接口自动化测试框架(初级篇)附源码>讲过了接口自动化测试框架的搭建,最核心的模块功能就是测试数据库初始化,再来看看之前的框架结构: 可以看出testcase ...
- python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为
python接口自动化测试二十七:密码MD5加密 ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...
- python接口自动化测试七:获取登录的Cookies
python接口自动化测试七:获取登录的Cookies,并关联到下一个请求 获取登录的cookies:loginCookies = r.cookies 把获取到的cookies传入请求:cooki ...
- 《selenium2 Java 自动化测试实战(第二版)》 更新2016.5.3
java 版来了!! 本文档在<selenium2 Python 自动化测试实战>的基础上,将代码与实例替换为java ,当然,部分章节有变更.这主要更语言本身的特点有关.集合和java下 ...
- 面面俱到的Java接口自动化测试实战
第1章 接口自动化测试整体认知了解什么是接口和为什么要做接口测试.并且知道接口自动化测试应该学习哪些技术以及接口自动化测试的落地过程. 1-1 导学章节 1-2 什么是接口 1-3 为什么要做接口测试 ...
随机推荐
- eclipse中安装lombok插件
一:下载lombok 下载地址:https://projectlombok.org/downloads/lombok.jar 或者访问官网下载 https://projectlombok.org/ ...
- python-day1作业(感谢视频老师留的作业)
__author__ = 'zht' #!/usr/bin/env python # -*- coding: utf-8 -*- ''' #努力学习每一天 ''' #尝试次数计数器 tries = 0 ...
- DataSource--DBCP--C3P0--DBUtils
一.DataSource 接口(javax.sql) 1.连接池: 由于与数据库连接的创建和销毁非常占用资源,因此提出了连接池技术,用于提升java程序操作数据库的性能;连接池 ...
- Selenium3+webdriver学习笔记2(常用元素定位方式,定位单个元素共8种,总共有23种)
#!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...
- pc端常见布局---垂直居中布局 单元素定高
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- npm在linux即mac下更新时报错
nam在linux即mac下需要更新到新版本:
- javaSe-字符型和布尔型
其实java数据类型一节就可以全部写完了,为什么还需要字符型和布尔型呢,原因是这俩个都很重要: 字符型用char表示,字符分三种: 普通字符:char a = 'a',普通字符表示一个普通的字符,没有 ...
- 【转】iOS-生成Bundle包-引入bundle-使用bundle
在我们使用第三方框架时,常常看到XXX.bundle的文件. 我们找到该文件,显示包内容,大致看到很多资源文件:图片.配置文本.XIB文件…… 什么是Bundle文件? 简单理解,就是资源文件包. ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
- C++ 值传递&引用&地址