1. 题目:
      基于以下两个接口和数据完成接口自动化测试,并生成测试报告:
  1. '''
    登录
  2.  
  3. login='http://47.107.168.87:8080/futureloan/mvc/api/member/login'
    login_data={'mobilephone':18688773467,'pwd':'123456'}
  4.  
  5. 充值
  6.  
  7. recharge='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'
    recharge_data={'mobilephone':18688773467,'amount':'1000'}
    '''
  1. test_excel.xlsx:

  1. http_request.py
  1. # -*- coding:utf-8 -*-
  2.  
  3. '''
  4. @project: jiaxy
  5. @author: Jimmy
  6. @file: http_request.py
  7. @ide: PyCharm Community Edition
  8. @time: 2018-12-05 10:06
  9. @blog: https://www.cnblogs.com/gotesting/
  10.  
  11. '''
  12.  
  13. import requests
  14.  
  15. class HttpRequest:
  16.  
  17. def http_request(self,url,param,method,cookies=None):
  18. if method == 'get':
  19. res = requests.get(url,param,cookies = cookies)
  20. elif method == 'post':
  21. res = requests.post(url,param,cookies = cookies)
  22. else:
  23. print('请求方法错误!')
  24. return res
  1.  
  1. read_excel.py
  1. # -*- coding:utf-8 -*-
  2.  
  3. '''
  4. @project: jiaxy
  5. @author: Jimmy
  6. @file: read_excel.py
  7. @ide: PyCharm Community Edition
  8. @time: 2018-12-05 11:57
  9. @blog: https://www.cnblogs.com/gotesting/
  10.  
  11. '''
  12.  
  13. from openpyxl import load_workbook
  14.  
  15. class ReadExcel:
  16.  
  17. def read_excel(self):
  18. wb = load_workbook('test_excel.xlsx')
  19. sheet = wb['登录及充值测试数据']
  20. test_data = []
  21. for i in range(2,sheet.max_row+1):
  22. sub_data = {}
  23. sub_data['url'] = sheet.cell(i,1).value
  24. sub_data['param'] = eval(sheet.cell(i,2).value)
  25. sub_data['method'] = sheet.cell(i,3).value
  26. sub_data['expected'] = sheet.cell(i,4).value
  27. test_data.append(sub_data)
  28. return test_data
  1.  
  1. test_api.py
  1. # -*- coding:utf-8 -*-
  2.  
  3. '''
  4. @project: jiaxy
  5. @author: Jimmy
  6. @file: TestApi.py
  7. @ide: PyCharm Community Edition
  8. @time: 2018-12-05 10:09
  9. @blog: https://www.cnblogs.com/gotesting/
  10.  
  11. '''
  12.  
  13. import unittest
  14.  
  15. from TestApi.http_request import HttpRequest
  16.  
  17. cookies = None
  18.  
  19. class TestHttpApi(unittest.TestCase):
  20.  
  21. def setUp(self):
  22. pass
  23.  
  24. def tearDown(self):
  25. pass
  26.  
  27. # 改写__init__方法,使用超继承
  28. def __init__(self,url,param,method,expected,methodName):
  29. self.url = url
  30. self.param = param
  31. self.method = method
  32. self.expected = expected
  33. super(TestHttpApi,self).__init__(methodName)
  34.  
  35. def test_api(self):
  36. global cookies
  37. res = HttpRequest().http_request(self.url,self.param,self.method,cookies)
  38. print('请求结果:',res.json())
  39. if res.cookies:
  40. cookies = res.cookies
  41.  
  42. try:
  43. self.assertEquals(self.expected,res.json()['msg'])
  44. except AssertionError as e:
  45. print('断言异常:',e)
  46. raise e
  1. test_suite.py
  1. # -*- coding:utf-8 -*-
  2.  
  3. '''
  4. @project: jiaxy
  5. @author: Jimmy
  6. @file: test_suite.py
  7. @ide: PyCharm Community Edition
  8. @time: 2018-12-05 10:28
  9. @blog: https://www.cnblogs.com/gotesting/
  10.  
  11. '''
  12.  
  13. import unittest
  14. import HTMLTestRunner
  15. from TestApi.test_api import TestHttpApi
  16. from TestApi.read_excel import ReadExcel
  17.  
  18. # 定义测试数据列表
  19. test_data = ReadExcel().read_excel()
  20. print(test_data)
  21. '''
  22. test_data = [{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':'','pwd':'123456'},'method':'get','expected':'手机号不能为空'},
  23. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':''},'method':'get','expected':'密码不能为空'},
  24. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':'12345678'},'method':'get','expected':'用户名或密码错误'},
  25. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':'123456'},'method':'get','expected':'登录成功'},
  26. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':'','amount':'1000'},'method':'post','expected':'手机号不能为空'},
  27. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':1868877346,'amount':'1000'},'method':'post','expected':'手机号码格式不正确'},
  28. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':''},'method':'post','expected':'请输入金额'},
  29. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':'100000000000'},'method':'post','expected':'请输入范围在0到50万之间的正数金额'},
  30. {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':'1000'},'method':'post','expected':'充值成功'}]
  31. '''
  32.  
  33. # 加载测试集
  34. suite = unittest.TestSuite()
  35. for item in test_data:
  36. suite.addTest(TestHttpApi(item['url'],
  37. item['param'],
  38. item['method'],
  39. item['expected'],
  40. 'test_api'))
  41.  
  42. # 执行测试,输出测试报告
  43. with open('TestApi.html','wb+') as file:
  44. runner = HTMLTestRunner.HTMLTestRunner(file)
  45. runner.run(suite)
  1.  
  1.  

测试报告:

python - 接口自动化测试实战 - case1 - 优化版的更多相关文章

  1. python - 接口自动化测试实战 - case1 - 再次优化版

    本次优化: 1.  各级分Package 2.  封装[ReadExcel]类 3.  封装[ReadConfig]类 4.  封装[GetLog]类 5.  引入ddt数据驱动测试,优化测试用例代码 ...

  2. Python接口自动化测试实战-----附源码

    目录 1. 接口定义 2. 基本流程 3. 需求分析 4. 用例设计 5. 脚本开发 6. 结果分析 接口定义: 接口普遍有两种意思,一种是API(Application Program Interf ...

  3. ptyhon - 接口自动化测试实战case1

    work_20181203_httprequest.py: import requestsclass http_request: def http_get(url,params): res = req ...

  4. Python接口自动化测试框架实战 从设计到开发

    第1章 课程介绍(不要错过)本章主要讲解课程的详细安排.课程学习要求.课程面向用户等,让大家很直观的对课程有整体认知! 第2章 接口测试工具Fiddler的运用本章重点讲解如何抓app\web的htt ...

  5. 基于Python接口自动化测试框架+数据与代码分离(进阶篇)附源码

    引言 在上一篇<基于Python接口自动化测试框架(初级篇)附源码>讲过了接口自动化测试框架的搭建,最核心的模块功能就是测试数据库初始化,再来看看之前的框架结构: 可以看出testcase ...

  6. 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 ...

  7. python接口自动化测试七:获取登录的Cookies

    python接口自动化测试七:获取登录的Cookies,并关联到下一个请求   获取登录的cookies:loginCookies = r.cookies 把获取到的cookies传入请求:cooki ...

  8. 《selenium2 Java 自动化测试实战(第二版)》 更新2016.5.3

    java 版来了!! 本文档在<selenium2 Python 自动化测试实战>的基础上,将代码与实例替换为java ,当然,部分章节有变更.这主要更语言本身的特点有关.集合和java下 ...

  9. 面面俱到的Java接口自动化测试实战

    第1章 接口自动化测试整体认知了解什么是接口和为什么要做接口测试.并且知道接口自动化测试应该学习哪些技术以及接口自动化测试的落地过程. 1-1 导学章节 1-2 什么是接口 1-3 为什么要做接口测试 ...

随机推荐

  1. eclipse中安装lombok插件

    一:下载lombok 下载地址:https://projectlombok.org/downloads/lombok.jar 或者访问官网下载  https://projectlombok.org/ ...

  2. python-day1作业(感谢视频老师留的作业)

    __author__ = 'zht' #!/usr/bin/env python # -*- coding: utf-8 -*- ''' #努力学习每一天 ''' #尝试次数计数器 tries = 0 ...

  3. DataSource--DBCP--C3P0--DBUtils

    一.DataSource 接口(javax.sql)     1.连接池:         由于与数据库连接的创建和销毁非常占用资源,因此提出了连接池技术,用于提升java程序操作数据库的性能;连接池 ...

  4. Selenium3+webdriver学习笔记2(常用元素定位方式,定位单个元素共8种,总共有23种)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...

  5. pc端常见布局---垂直居中布局 单元素定高

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. npm在linux即mac下更新时报错

    nam在linux即mac下需要更新到新版本:

  7. javaSe-字符型和布尔型

    其实java数据类型一节就可以全部写完了,为什么还需要字符型和布尔型呢,原因是这俩个都很重要: 字符型用char表示,字符分三种: 普通字符:char a = 'a',普通字符表示一个普通的字符,没有 ...

  8. 【转】iOS-生成Bundle包-引入bundle-使用bundle

    在我们使用第三方框架时,常常看到XXX.bundle的文件. 我们找到该文件,显示包内容,大致看到很多资源文件:图片.配置文本.XIB文件……   什么是Bundle文件? 简单理解,就是资源文件包. ...

  9. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  10. C++ 值传递&引用&地址