接口测试(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) &#…

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

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

CTF php RCE(二)

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

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

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

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;增加了对…

【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;孩子应能对外界刺激如声音…

读书笔记-Java并发编程的艺术-第4章(Java并发编程基础)-第3节(线程间通信)

文章目录 4.3 线程间通信4.3.1 volatile和synchronized 关键字4.3.2 等待/通知机制4.3.3 等待/通知的经典范式4.3.4 管道输入 / 输出流4.3.5 Thread.join()的使用4.3.6 ThreadLocal的使用 4.3 线程间通信 线程开始运行&#xff0c;拥有自己的栈空间&#xff0c;就如同一个脚本…

Java字符串(String、字符串拼接、原理)

文章目录 一、String字符串1.1创建方式【直接赋值、new一个对象】1.1.1 使用字符串字面值直接赋值&#xff1a;&#xff08;1&#xff09;字符串字面量创建String对象的转换过程&#xff08;2&#xff09;一些方法&#xff08;3&#xff09;说明 1.1.2 使用new关键字创建字符串…

接口测试(1)

什么是接口测试 路线&#xff1a; 项目介绍 URL&#xff1a;统一资源定位符 HTTP协议 请求URL 请求行 请求方法 post/put 请求行 请求数据类型 content-type json 请求数据 请求体 响应状态码 响应行 响应数据类型 content-type json 响应数据 请响应体 用户名&#xf…

Mybatis Plus 3.X版本的insert填充自增id的IdType.ID_WORKER策略源码分析

总结/朱季谦 某天同事突然问我&#xff0c;你知道Mybatis Plus的insert方法&#xff0c;插入数据后自增id是如何自增的吗&#xff1f; 我愣了一下&#xff0c;脑海里只想到&#xff0c;当在POJO类的id设置一个自增策略后&#xff0c;例如TableId(value "id",type …

Git 命令行快速入门

前言 &#xff08;1&#xff09;新手个人建议使用TortoiseGit这类图形化界面来上手学习。 &#xff08;2&#xff09;如果一定需要用命令行进行操作&#xff0c;可以按照B站&#xff1a;程式与网页开发者必备技能&#xff01;Git 和 GitHub 零基础快速上手&#xff0c;轻松掌握…

干货:高水平论文写作思路与方法

前言:Hello大家好,我是小哥谈。高水平论文的写作需要扎实的研究基础和严谨的思维方式。同时,良好的写作技巧和时间管理也是成功的关键。本篇文章转载自行业领域专家所写的一篇文章,希望大家阅读后可以能够有所收获。🌈 目录 🚀1.依托事实/证据,通过合理的逻辑,…

汇川CodeSysPLC教程03-2-14 与HMI通信

硬件连接 PLC与HMI连接采用何种连接方式&#xff0c;通常是参考双方支持哪些接口。PLC&#xff08;可编程逻辑控制器&#xff09;与HMI&#xff08;人机界面&#xff09;之间的通讯方式主要有以下几种&#xff1a; 串行通讯&#xff08;Serial Communication&#xff09;&…