组织架构:

  包括配置文件,反射、文件路径、Excel操作、测试报告生成

case.config

[MODE]
file_name=case_data.xlsx
mode={"register":'all',"login":'all',"recharge":'all'}

tools文件夹里的东西

do_config.py
 # -*- conding:utr-8 -*-
#@Time :2018/11/17 11:21
#@Author:GYP测试
#@File :do_config.py import configparser
class ReadConfig:
def read_config(self,file_name,section,option):
cf=configparser.ConfigParser()
cf.read(file_name,encoding='utf-8')
return cf.get(section,option)
if __name__ == '__main__':
res=ReadConfig().read_config('case.config','MODE','file_name')
print(res)
do_excel.py
 # -*- conding:utr-8 -*-
#@Time :2018/11/17 14:05
#@Author:GYP测试
#@File :do_excel.py
from openpyxl import load_workbook
from tools.do_config import ReadConfig
from tools.project_path import * class Do_Excle:
def __init__(self):
self.file_name = test_data_path
self.sheet_names = eval(ReadConfig().read_config(case_config_path, 'MODE', 'mode'))
def get_header(self):
wb = load_workbook(self.file_name)
for sheet_name in self.sheet_names:
sheet=wb[sheet_name]
header=[]
for i in range(1,sheet.max_column+1):
header.append(sheet.cell(1,i).value)
return header
def Read_Excle(self):
wb = load_workbook(self.file_name)
test_data = []
for sheet_name in self.sheet_names:
sheet = wb[sheet_name]
header = self.get_header()
if self.sheet_names[sheet_name]=='all':
for i in range(2,sheet.max_row+1):
sub_data={}
for j in range(1,sheet.max_column+1):
sub_data[header[j-1]]=sheet.cell(i,j).value
test_data.append(sub_data)
else:
for case_id in self.sheet_names[sheet_name]:
sub_data={}
for j in range(1,sheet.max_column+1):
sub_data[header[j-1]]=sheet.cell(case_id+1,j).value
test_data.append(sub_data)
return test_data
@staticmethod
def write_excel(fiel_name,sheet_name,i,ActaulResult):
wb=load_workbook(fiel_name)
sheet=wb[sheet_name]
sheet.cell(i,8).value=ActaulResult
wb.save(fiel_name)
if __name__ == '__main__':
res=Do_Excle().Read_Excle()
print(res)
print(len(res))

get_data.py

# -*- conding:utf-8 -*-
#@Time :2018/11/19 11:37
#@Author:GYP测试
#@File :get_data.py class Get_Data: cookie=None
http_requests.py
 # -*- conding:utr-8 -*-
#@Time :2018/11/16 21:50
#@Author:GYP测试
#@File :http_requests.py import requests
class Http_Request:
def request(self,method,url,data,cookie=None):
try:
if method == 'post':
res=requests.post(url,data,cookies=cookie)
else:
res=requests.get(url,data,cookies=cookie)
except Exception as e:
print("非法请求,请检查{0}".format(e))
raise e
return res
http_test.py
 # -*- conding:utr-8 -*-
#@Time :2018/11/18 15:00
#@Author:GYP测试
#@File :http_test.py import unittest #单元测试框架 from tools.do_excel import Do_Excle #数据读写
from tools.project_path import * #文件路径
from ddt import ddt,data #数据处理框架
from tools.get_data import Get_Data #反射
from tools.http_requests import Http_Request test_data=Do_Excle().Read_Excle() #读取数据
@ddt
class TestHttp(unittest.TestCase):
@data(*test_data)
def test_api(self,item):
res=Http_Request().request(item['method'],item['url'],eval(item['data']),getattr(Get_Data,'cookie'))
if res.cookies: #利用反射获取cookie的值
setattr(Get_Data,'cookie',res.cookies)
try:
self.assertEqual(str(item['ExpectedResult']),res.json()['code'])
ActaulResult='Pass'
except AssertionError as e:
print('执行用例失败,请检查%s' %e)
ActaulResult = 'Faile'
print("获取到的结果是:{0}".format(res.json()))
finally:
# print(item['case_id'])
Do_Excle.write_excel(test_data_path,item['module'],item['case_id']+1,ActaulResult)
project_path.py
# -*- conding:utr-8 -*-
#@Time :2018/11/17 13:46
#@Author:GYP测试
#@File :project_path.py import os
from tools.do_config import ReadConfig # class Get_Path:
# def get_path(self):
path=os.path.split(os.path.split(os.path.realpath(__file__))[0])[0] case_config_path=os.path.join(path,'conf','case.config') data_file_name=ReadConfig().read_config(case_config_path,'MODE','file_name')
test_data_path=os.path.join(path,'test_data',data_file_name)
html_repot_path=os.path.join(path,'test_result','html_report','test_api.html')
print(case_config_path)
print(html_repot_path)
run.py
 # -*- conding:utr-8 -*-
#@Time :2018/11/16 21:36
#@Author:GYP测试
#@File :run.py import unittest
import HTMLTestRunner
from tools.project_path import * from tools.http_test import TestHttp
suite=unittest.TestSuite() loader=unittest.TestLoader() suite.addTest(loader.loadTestsFromTestCase(TestHttp)) with open(html_repot_path,'wb') as file:
runner=HTMLTestRunner.HTMLTestRunner(
stream=file,
title='这个是接口自动化的测试报告',
description='我来测试哦!',
tester='GYP')
runner.run(suite)
 
 

python接口自动化1的更多相关文章

  1. Python接口自动化——soap协议传参的类型是ns0类型的要创建工厂方法纪要

    1:在Python接口自动化中,对于soap协议的xml的请求我们可以使用Suds Client来实现,其soap协议传参的类型基本上是有2种: 第一种是传参,不需要再创建啥, 第二种就是ns0类型的 ...

  2. python接口自动化(十)--post请求四种传送正文方式(详解)

    简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...

  3. python接口自动化-Cookie_绕过验证码登录

    前言 有些登录的接口会有验证码,例如:短信验证码,图形验证码等,这种登录的验证码参数可以从后台获取(或者最直接的可查数据库) 获取不到也没关系,可以通过添加Cookie的方式绕过验证码 前面在“pyt ...

  4. python接口自动化28-requests-html爬虫框架

    前言 requests库的好,只有用过的人才知道,最近这个库的作者又出了一个好用的爬虫框架requests-html.之前解析html页面用过了lxml和bs4, requests-html集成了一些 ...

  5. python接口自动化-参数化

    原文地址https://www.cnblogs.com/yoyoketang/p/6891710.html python接口自动化 -参数关联(一)https://www.cnblogs.com/11 ...

  6. python接口自动化 -参数关联(一)

    原文地址https://www.cnblogs.com/yoyoketang/p/6886610.html 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址ht ...

  7. python接口自动化20-requests获取响应时间(elapsed)与超时(timeout)

    前言 requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的. 如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 关于reques ...

  8. python接口自动化24-有token的接口项目使用unittest框架设计

    获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...

  9. python接口自动化6-重定向(Location)

    前言 某屌丝男A鼓起勇气向女神B打电话表白,女神B是个心机婊觉得屌丝男A是好人,不想直接拒绝于是设置呼叫转移给闺蜜C了,最终屌丝男A和女神闺蜜C表白成功了,这种场景其实就是重定向了. 一.重定向 1. ...

  10. python接口自动化5-Json数据处理

    前言 有些post的请求参数是json格式的,这个前面第二篇post请求里面提到过,需要导入json模块处理. 一般常见的接口返回数据也是json格式的,我们在做判断时候,往往只需要提取其中几个关键的 ...

随机推荐

  1. BeanUtils 日期转换(本地格式yyyy-MM-dd)转换成date

    1.BeanUtils工具的使用 1)beanUtils 可以便于对javaBean的属性进行赋值. 2)beanUtils 可以便于对javaBean的对象进行赋值. 3)beanUtils可以将一 ...

  2. 实时通讯系列目录篇之SignalR详解

    一. 简单说几句 最早使用SignalR的时候大约是两年前了,记得当时是一个OA中消息的实时提醒,轮询的方式有点耗资源,WebSocket写起来又比较麻烦,最终选择了SignalR,当时是什么版本已经 ...

  3. windows7 64位使用anaconda傻瓜式安装tensorflow

    1.下载anaconda并一键安装 登录网页:https://www.anaconda.com/download/ 这里选择Python3.6 version 64-Bit Graphlcal Ins ...

  4. java常见题目总结

    编写多线程程序的几种方法:java5以前可以通过继承Thread类或者实现Runnable接口,重写run方法来定义线程行为:java5以后出现了另一种方式,实现Callable接口,该接口的call ...

  5. linux mint18 cinnamon 64bit 安装 docker

    参考官方文档:https://docs.docker.com/engine/installation/linux/ubuntu/ 1. 安装一些使 apt 可以使用 https 的源 sudo apt ...

  6. 20164305 徐广皓 Exp1+ 逆向进阶

    实验内容 Task1 (5-10分) 自己编写一个64位shellcode.参考shellcode指导. 自己编写一个有漏洞的64位C程序,功能类似我们实验1中的样例pwn1.使用自己编写的shell ...

  7. awk和sed截取nginx和tomcat时间段日志

    1 nginx日志截取示例 日志路径:/usr/local/nginx/logs, 截取access.log中2019年3月24日17点00~02之间的日志: 写法1: cat access.log ...

  8. Spring ES

    elasticsearchTemplate 和 ElasticsearchRepository JPA中有个ElasticsearchRepository可以做Elasticsearch的相关增删改查 ...

  9. Ansible-----循环

    with_dict迭代字典项 使用"with_dict"可以迭代字典项.迭代时,使用"item.key"表示字典的key,"item.value&qu ...

  10. SQL server查询语句

    作者:邓聪聪 mysql部分语句的查询,持续更新 系统函数 函数名 描述 举例 convert() 数据类型转换 selece convert(varchar(5),12345) 返回:字符串1234 ...