接口测试(3)

接口自动化

# 获取图片验证码import requestsresponse = requests.get(url="http://kdtx-test.itheima.net/api/captchaImage")print(response.status_code)
print(response.text)
import requestsurl = "http://kdtx-test.itheima.net/api/login"
header_data = {"Content-Type":"application/json"
}
login_data = {"username":"admin","password":"HM_2023_test","code":2,"uuid":"247dec0137944faeaf539d4d644ee92a"
}
response = requests.post(url=url,headers=header_data,json=login_data)print(response.status_code)
print(response.json())

# login api
import requestsclass LoginAPI:def __init__(self):self.url_verify = "http://kdtx-test.itheima.net/api/captchaImage"self.url_login = "http://kdtx-test.itheima.net/api/login"# 验证码def get_verify_code(self):return requests.get(url=self.url_verify)# 登录def login(self, test_data):# 不用headers 因为json 默认 content-Type : application/jsonreturn requests.post(url=self.url_login, json=test_data)
from api.login import LoginAPI
import unittest
# 调用
class TestContractBusiness(unittest.TestCase):def setUp(self):self.login_api = LoginAPI()def tearDown(self):passdef test01_login_success(self):res_v = self.login_api.get_verify_code()print(res_v.status_code)print(res_v.json())print(res_v.json().get("uuid"))login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())
import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})
return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})json headers自动设置Content-Type = application/json
from api.login import LoginAPI
from api.course import CourseAPI
import unittestclass TestContractBusiness(unittest.TestCase):token = Nonedef setUp(self):self.login_api = LoginAPI()self.course_api = CourseAPI()def tearDown(self):passdef test01_login_success(self):# 获取验证码res_v = self.login_api.get_verify_code()# print(res_v.status_code)print(res_v.json())# 打印uuidprint(res_v.json().get("uuid"))# 登录login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())TestContractBusiness.token = res_l.json().get("token")print(TestContractBusiness.token)# 课程新增成功def test02_add_course(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course_api.add_course(test_data=add_data, token=self.token)print(response.json())

上传文件

import requestsclass ContractAPI:def __init__(self):self.url_upload = "http://kdtx-test.itheima.net/api/common/upload"self.url_add_contract = "http://kdtx-test.itheima.net/api/contract"# 合同上传接口def upload_contract(self, test_data, token):return requests.post(url=self.url_upload, files={"file": test_data}, headers={"Authorization": token})
from api.login import LoginAPI
from api.course import CourseAPI
from api.contract import ContractAPI
import unittestclass TestContractBusiness(unittest.TestCase):token = Nonedef setUp(self):self.login_api = LoginAPI()self.course_api = CourseAPI()self.contract = ContractAPI()def tearDown(self):passdef test01_login_success(self):# 获取验证码res_v = self.login_api.get_verify_code()# print(res_v.status_code)print(res_v.json())# 打印uuidprint(res_v.json().get("uuid"))# 登录login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())TestContractBusiness.token = res_l.json().get("token")print(TestContractBusiness.token)# 课程新增成功def test02_add_course(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course_api.add_course(test_data=add_data, token=self.token)print(response.json())# 上传合同成功def test03_upload_contract(self):f = open("../data/test.pdf", "rb")response = self.contract.upload_contract(test_data=f, token=TestContractBusiness.token)print(response.json())
import requestsclass ContractAPI:def __init__(self):self.url_upload = "http://kdtx-test.itheima.net/api/common/upload"self.url_add_contract = "http://kdtx-test.itheima.net/api/contract"# 合同上传接口def upload_contract(self, test_data, token):return requests.post(url=self.url_upload, files={"file": test_data}, headers={"Authorization": token})# 合同新增def add_contract(self, test_data, token):return requests.post(url=self.url_add_contract, json=test_data, headers={"Authorization": token})
from api.login import LoginAPI
from api.course import CourseAPI
from api.contract import ContractAPI
import unittestclass TestContractBusiness(unittest.TestCase):token = Nonedef setUp(self):self.login_api = LoginAPI()self.course_api = CourseAPI()self.contract = ContractAPI()def tearDown(self):passdef test01_login_success(self):# 获取验证码res_v = self.login_api.get_verify_code()# print(res_v.status_code)print(res_v.json())# 打印uuidprint(res_v.json().get("uuid"))# 登录login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())TestContractBusiness.token = res_l.json().get("token")print(TestContractBusiness.token)# 课程新增成功def test02_add_course(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course_api.add_course(test_data=add_data, token=self.token)print(response.json())# 上传合同成功def test03_upload_contract(self):f = open("../data/test.pdf", "rb")response = self.contract.upload_contract(test_data=f, token=TestContractBusiness.token)print(response.json())def test04_add_contract(self):# contractNo 数据唯一add_data = {"name": "测试888","phone": "13612345678","contractNo": "HT2024070806","subject": "6","courseId": "99","channel": "0","activityId": 77,"fileName": "xxx"}response = self.contract.add_contract(test_data=add_data, token=TestContractBusiness.token)print(response.json())

    断言

from api.login import LoginAPI
import unittestclass TestLoginAPI(unittest.TestCase):uuid = Nonedef setUp(self):self.login_api = LoginAPI()response = self.login_api.get_verify_code()TestLoginAPI.uuid = response.json().get("uuid")def tearDown(self):passdef test01_sucess(self):login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)# 预期 实际assert 200 == response.status_codeassert '成功' in response.textassert 200 == response.json().get("code")def test02_without_username(self):login_data = {"username": "","password": "HM_2023_test","code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)print(response.json())# 预期 实际assert 200 == response.status_codeassert '错误' in response.textassert 500 == response.json().get("code")def test03_username_not_exist(self):login_data = {"username": "jack666","password": "HM_2023_test","code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)print(response.json())# 预期 实际assert 200 == response.status_codeassert '错误' in response.textassert 500 == response.json().get("code")

参数化

数据准备:

{"login_001":{"username": "admin", "password": "HM_2023_test", "status": 200,"message": "成功","code": 200},"login_002":{"username": "", "password": "HM_2023_test", "status": 200,"message": "错误","code": 500},"login_003":{"username": "jack666", "password": "HM_2023_test", "status": 200,"message": "错误","code": 500}
}

读入数据代码 

import jsondef read_json(filename):filepath = "../data/" + filenamewith open(filepath, "r", encoding="utf-8") as f:return json.load(f)

scripts:

from api.login import LoginAPI
import unittest
from parameterized import parameterized
from common.read_json import read_jsondef get_data():data = read_json("login_data.json")list1 = []for i in data.values():list1.append((i.get("username"),i.get("password"),i.get("status"),i.get("message"),i.get("code")))return list1class TestLoginAPI(unittest.TestCase):uuid = Nonedef setUp(self):self.login_api = LoginAPI()response = self.login_api.get_verify_code()TestLoginAPI.uuid = response.json().get("uuid")def tearDown(self):pass@parameterized.expand(get_data())def test_login(self,username,password,status,message,code):login_data = {"username": username,"password": password,"code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)# 预期 实际assert status == response.status_codeassert message in response.textassert code == response.json().get("code")

如何测试未登录

import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestAddCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestAddCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_success(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course.add_course(test_data=add_data, token=TestAddCourseAPI.token)assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")# 如何测试未登录def test02_fail(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course.add_course(test_data=add_data, token="xxx")assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")
import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})def select_course(self, test_data, token):return requests.get(url=self.url_select_course + "/{}".format(test_data), headers={"Authorization": token})
import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestSelectCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestSelectCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_select_success(self):response = self.course.select_course(test_data="?name=测试开发提升课01",token=TestSelectCourseAPI.token)print(response.json())assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")def test02_select_fail(self):response = self.course.select_course(test_data="?name=测试开发提升课01", token="xxx")print(response.json())assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")
import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})def select_course(self, test_data, token):return requests.get(url=self.url_select_course + "/{}".format(test_data), headers={"Authorization": token})def update_course(self,test_data,token):return requests.put(url=self.url_add_course, json=test_data, headers={"Authorization": token})
import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestUpdateCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestUpdateCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_update_success(self):update_data = {"id": "23056458704355861", "name": "测试开发提升课", "subject": "6", "price": 899,"applicablePerson": "2", "info": "测试开发提升课"}response = self.course.update_course(test_data=update_data,token=TestUpdateCourseAPI.token)assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")def test02_update_fail(self):update_data = {"id": "23056458704355861", "name": "测试开发提升课", "subject": "6", "price": 899,"applicablePerson": "2", "info": "测试开发提升课"}response = self.course.update_course(test_data=update_data,token="xxxx")assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")

路径参数时

import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})def select_course(self, test_data, token):return requests.get(url=self.url_select_course + "/{}".format(test_data), headers={"Authorization": token})def update_course(self,test_data,token):return requests.put(url=self.url_add_course, json=test_data, headers={"Authorization": token})def delete_course(self,course_id, token):return requests.delete(url=self.url_add_course + "/{}".format(course_id), headers={"Authorization": token})
import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestDeleteCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestDeleteCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_delete_success(self):response = self.course.delete_course(course_id=23056458704355867,token=TestDeleteCourseAPI.token)print(response.json())assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")def test02_delete_id_not_exist(self):response = self.course.delete_course(course_id=987654321,token=TestDeleteCourseAPI.token)print(response.json())assert 200 == response.status_codeassert "失败" in response.textassert 500 == response.json().get("code")def test03_delete_fail(self):response = self.course.delete_course(course_id=23056458704355868,token="xxxx")print(response.json())assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")

config:

# 存放被测项目基本信息 如URL地址等
import os
BASE_URL = "http://kdtx-test.itheima.net"BASE_PATH = os.path.dirname(__file__)
print(BASE_PATH)

测试报告

from common.HTMLTestRunner import HTMLTestRunner
import unittest
import time
import config
suite = unittest.defaultTestLoader.discover(config.BASE_PATH + "/script/","test*.py")with open(config.BASE_PATH + "/report/{}.html".format(time.strftime("%Y_%m_%d %H_%M_%S")),"wb") as f:HTMLTestRunner(stream=f).run(suite)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/43860.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机网络之WPAN 和 WLAN

上一篇文章内容:无线局域网 1.WPAN(无线个人区域网) WPAN 是以个人为中心来使用的无线个人区域网,它实际上就是一个低功率、小范围、低速率和低价格的电缆替代技术。 (1) 蓝牙系统(Bluetooth) &#…

常见的Java注解类

Java 注解类大多是在 java.lang.annotation 包中定义的。这个包包括了定义注解和元注解的核心类和接口。Java 注解类主要包括几种核心注解和用于定义注解的元注解。以下是一些常见的 Java 注解类: 核心注解类 @Override 用于标识方法重写。编译器会检查方法是否正确地重写了超…

项目构思及ER图

构思: 首先是登录界面,我们首先绘制一个界面,在界面里面存储两个文本框,用于读取用户输入的文本,然后由客户端传到服务器里面和数据库进行对比,如果密码和账号对应的时候就可以进入到qq的主界面 注册&…

QT文件生成可执行的exe程序

将qt项目生成可执行的exe程序可按照以下步骤进行: 1、在qt中构建运行生成.exe文件; 2、从自定义的路径中取出exe文件放在一个单独的空文件夹中(exe文件在该文件夹中的release文件夹中); 3、从开始程序中搜索qt&#xf…

CTF php RCE(二)

0x04 php伪协议 这种我们是先看到了include才会想到,利用伪协议来外带文件内容,但是有些同学会问,我们怎么知道文件名是哪个,哪个文件名才是正确的,那么这里我们就得靠猜了 include函数 因为 include 是一个特殊的语…

R语言学习笔记3-基本类型篇

R语言学习笔记-基本类型篇 基本类型介绍数值型(Numeric)整数型(Integer)浮点数型(Double)数值运算数值型 NA(缺失值) 字符型(Character)字符型的特点和用途基…

产品原型设计:从概念到实现的完整指南

如果你是一位产品经理,那么你一定会和原型图打交道,产品原型是产品设计方案和底层逻辑的可视化表达,需要完整清晰地表达出产品目的及需求,在整个产品创造的过程中发挥着不可或缺的作用。而对于一些刚入行的产品经理来说&#xff0…

Instruct-GS2GS:通过用户指令编辑 GS 三维场景

Paper: Instruct-GS2GS: Editing 3D Gaussian Splats with Instructions Introduction: https://instruct-gs2gs.github.io/ Code: https://github.com/cvachha/instruct-gs2gs Instruct-GS2GS 复用了 Instruct-NeRF2NeRF 1 的架构,将基于 NeRF 的三维场景编辑方法迁…

4:表单和通用视图

表单和通用视图 1、编写一个简单的表单&#xff08;1&#xff09;更新polls/detail.html文件 使其包含一个html < form > 元素&#xff08;2&#xff09;创建一个Django视图来处理提交的数据&#xff08;3&#xff09;当有人对 Question 进行投票后&#xff0c;vote()视图…

工具推荐:滴答清单

官网地址&#xff1a;DIDA:Todo list, checklist and task manager app for Android, iPhone and Web 使用近一个月&#xff0c;特别方便&#xff0c;使用感受非常棒&#xff0c;功能全面。 我主要用了以下功能&#xff1a; 1、每日事项提醒&#xff1a;写作&#xff0c;背字…

Java中的next、nextLine、nextInt等方法的区别

在刷牛客题目的过程中对很多都要设置读取用户输入&#xff0c;而且很敏感&#xff0c;每次用到next这些方法都会犯迷糊&#xff0c;这次就用博客总结一下&#xff0c;回头忘了方便再看看。 next()用来读取字符串&#xff0c;但是遇到分隔符&#xff08;如空格、换行符、制表符&…

Python实现吃豆人游戏详解(内附完整代码)

一、吃豆人游戏背景 吃豆人是一款由Namco公司在1980年推出的经典街机游戏。游戏的主角是一个黄色的小圆点&#xff0c;它必须在迷宫中吃掉所有的点数&#xff0c;同时避免被四处游荡的幽灵捉到。如果玩家能够吃掉所有的点数&#xff0c;并且成功避开幽灵&#xff0c;就可以进入…

力扣第一题

1. 两数之和 提示 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元素在答案里不能重复出现。 你可…

【测试设计】使用jenkins 插件Allure生成自动化测试报告

前言 以前做自动化测试的时候一直用的HTMLTestRunner来生成测试报告&#xff0c;后来也尝试过用Python的PyH模块自己构建测试报告&#xff0c;在后来看到了RobotFramework的测试报告&#xff0c;感觉之前用的测试报告都太简陋&#xff0c;它才是测试报告应该有的样子。也就是在…

射频硅基氮化镓:两个世界的最佳选择

当世界继续努力追求更高速的连接&#xff0c;并要求低延迟和高可靠性时&#xff0c;信息通信技术的能耗继续飙升。这些市场需求不仅将5G带到许多关键应用上&#xff0c;还对能源效率和性能提出了限制。5G网络性能目标对基础半导体器件提出了一系列新的要求&#xff0c;增加了对…

《妃梦千年》第二十八章:反间之计

第二十八章&#xff1a;反间之计 在经历了一系列的斗争和变故后&#xff0c;林清婉意识到&#xff0c;宫中的敌人和外部的敌人正在联手&#xff0c;他们不仅试图从外部攻破大周的防线&#xff0c;还在内部制造混乱。为了彻底解决这个问题&#xff0c;她决定采取反间之计&#…

【LeetCode刷题笔记】LeetCode.11.盛最多水的容器

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; 更多算法知识专栏&#xff1a;算法分析&#x1f525; 给大家跳段街舞感谢…

CSAPP Lab5- MallocLab

实验目标 本实验需要用c语言实现一个动态的存储分配器&#xff0c;也就是你自己版本的malloc&#xff0c;free&#xff0c;realloc函数。 实验步骤 tar xvf malloclab-handout.tar解压文件 我们需要修改的唯一文件是mm.c&#xff0c;包含如下几个需要实现的函数 int mm_ini…

婴幼儿自闭症的六大警示前兆:家长需警惕的早期信号

婴幼儿自闭症是一种复杂的发育障碍&#xff0c;其早期症状往往较为微妙&#xff0c;但若能及早发现并干预&#xff0c;对孩子的成长至关重要。以下是婴幼儿自闭症的六个主要前兆&#xff1a; 首先&#xff0c;目光呆滞与反应迟钝。婴儿期&#xff0c;孩子应能对外界刺激如声音…

30 个测试工程师面试题

30 个面试题&#xff1a; Python 中的生成器函数是什么&#xff1f;如何使用&#xff1f;MySQL 中如何进行索引优化&#xff1f;请举例说明。正则表达式中的非贪婪模式如何实现&#xff1f;Python 中的装饰器有什么作用&#xff1f;请给出一个示例。MySQL 中的存储过程和函数有…