接口测试(3)

接口自动化

# 获取图片验证码

import requests

response = requests.get(url="http://kdtx-test.itheima.net/api/captchaImage")

print(response.status_code)
print(response.text)
import requests

url = "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 requests


class 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/json
        return 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):
        pass

    def 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 requests

class 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 unittest

class TestContractBusiness(unittest.TestCase):
    token = None

    def setUp(self):
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()

    def tearDown(self):
        pass

    def test01_login_success(self):
        # 获取验证码
        res_v = self.login_api.get_verify_code()
        # print(res_v.status_code)
        print(res_v.json())
        # 打印uuid
        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())
        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 requests

class 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 unittest

class TestContractBusiness(unittest.TestCase):
    token = None

    def setUp(self):
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        self.contract = ContractAPI()

    def tearDown(self):
        pass

    def test01_login_success(self):
        # 获取验证码
        res_v = self.login_api.get_verify_code()
        # print(res_v.status_code)
        print(res_v.json())
        # 打印uuid
        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())
        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 requests

class 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 unittest

class TestContractBusiness(unittest.TestCase):
    token = None

    def setUp(self):
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        self.contract = ContractAPI()

    def tearDown(self):
        pass

    def test01_login_success(self):
        # 获取验证码
        res_v = self.login_api.get_verify_code()
        # print(res_v.status_code)
        print(res_v.json())
        # 打印uuid
        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())
        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 unittest


class TestLoginAPI(unittest.TestCase):
    uuid = None

    def setUp(self):
        self.login_api = LoginAPI()
        response = self.login_api.get_verify_code()
        TestLoginAPI.uuid = response.json().get("uuid")

    def tearDown(self):
        pass

    def 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_code

        assert '成功' in response.text

        assert 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_code

        assert '错误' in response.text

        assert 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_code

        assert '错误' in response.text

        assert 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 json

def read_json(filename):
    filepath = "../data/" + filename
    with 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_json

def 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 list1


class TestLoginAPI(unittest.TestCase):
    uuid = None

    def 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_code

        assert message in response.text

        assert code == response.json().get("code")

如何测试未登录

import unittest
from api.login import LoginAPI
from api.course import CourseAPI


class TestAddCourseAPI(unittest.TestCase):
    token = None

    def 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):
        pass

    def 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_code
        assert "成功" in response.text
        assert 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_code
        assert "认证失败" in response.text
        assert 401 == response.json().get("code")
import requests

class 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 CourseAPI


class TestSelectCourseAPI(unittest.TestCase):
    token = None

    def 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):
        pass

    def test01_select_success(self):
        response = self.course.select_course(test_data="?name=测试开发提升课01",token=TestSelectCourseAPI.token)
        print(response.json())
        assert 200 == response.status_code
        assert "成功" in response.text
        assert 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_code
        assert "认证失败" in response.text
        assert 401 == response.json().get("code")


import requests

class 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 CourseAPI


class TestUpdateCourseAPI(unittest.TestCase):
    token = None

    def 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):
        pass

    def 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_code
        assert "成功" in response.text
        assert 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_code
        assert "认证失败" in response.text
        assert 401 == response.json().get("code")

路径参数时

import requests



class 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 CourseAPI


class TestDeleteCourseAPI(unittest.TestCase):
    token = None

    def 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):
        pass

    def test01_delete_success(self):
        response = self.course.delete_course(course_id=23056458704355867,token=TestDeleteCourseAPI.token)
        print(response.json())
        assert 200 == response.status_code
        assert "成功" in response.text
        assert 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_code
        assert "失败" in response.text
        assert 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_code
        assert "认证失败" in response.text
        assert 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.mfbz.cn/a/787607.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ffmpeg滤镜-drawtext-命令行

使用 FFmpeg 在视频上添加文字可以通过 drawtext 滤镜来实现。这个滤镜允许你指定字体、大小、颜色、位置等。 基本用法 以下命令将 "Hello, World!" 添加到视频的顶部左侧: ffmpeg -i input.mp4 -vf "drawtexttextHello, World\!:fontcolorwhite…

使用redis进行短信登录验证(验证码打印在控制台)

使用redis进行短信登录验证 一、流程1. 总体流程图2. 流程文字讲解:3.代码3.1 UserServiceImpl:(难点)3.2 拦截器LoginInterceptor:3.3 拦截器配置类: 4 功能实现,成功存入redis (黑…

飞速(FS)10G光模块选择指南

飞速(FS)的10G SFP光模块专为万兆每秒(10 Gbps)的数据传输设计,满足多样化网络需求。该光模块支持多种传输距离,具备热插拔和数字诊断监控功能,全面适配200品牌,为客户提供更灵活的选…

CTF php RCE(二)

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

Tomcat的安全配置

1、生产环境优化 2、部分漏洞修复 转载自风险评估:Tomcat的安全配置,Tomcat安全基线检查加固-CSDN博客

氛围感视频素材高级感的去哪里找啊?带氛围感的素材网站库分享

亲爱的创作者们,大家好!今天我们来聊聊视频创作中至关重要的一点——氛围感。一个好的视频,不仅要有视觉冲击力,还要能够触动观众的情感。那我们应该去哪里寻找这些充满氛围感且高级的视频素材呢?别急,我这…

telnet在windows和linux上的使用方法

telnet在windows上使用 ‘telnet’ 不是内部或外部命令,也不是可运行的程序或批处理文件。 windows上有自带的telnet工具的,这只是没有安装添加进来而已。 处理 方法: 打开控制面板-点击程序与功能 进到程序与功能界面,点击启用或…

Debezium报错处理系列之第114篇:No TableMapEventData has been found for table id:256.

Debezium报错处理系列之第114篇:Caused by: com.github.shyiko.mysql.binlog.event.deserialization.MissingTableMapEventException: No TableMapEventData has been found for table id:256. Usually that means that you have started reading binary log within the logic…

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

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

C++基础语法

目录 一、命名空间 1.1 什么是命名空间 1.2 命名空间的定义 1.3 命名空间的使用 二、输入输出流 三、缺省参数 四、函数重载 五、内联函数 C是一种通用的编程语言,具有面向对象、泛型编程和低级内存操作等特性。它是由Bjarne Stroustrup在20世纪80年代初开发…

idea集成本地tomcat

由于网课老师使用的是eclipse,但是……本人用的是idea,所以不得不去找教程。 解决方案1: https://blog.csdn.net/weixin_54048131/article/details/131359793 这个地方,路径一定要到这个tomcat 否则不识别: 这里的JRE也要配置一下 新问题&…

VBA初学:零件成本统计之四(汇总计算)

第四步,最后进行汇总计算 汇总统计的计算 Sub count() Dim rng As Range Dim i As Long, j As Long Dim arr_s, arr, brr, crr, drr Dim rowscount As Long Dim X As Variant Dim rg As Single, xb As Single, zj As SingleMsgBox "汇总计算时间较久&#xff…

24吉林事业单位报名照上传通过别忘了这一步

24吉林事业单位报名照上传通过别忘了这一步 #吉林事业单位 #吉林三支一扶 #吉林事业编 #事业单位报名照片 #吉林事业单位考试 #吉林市事业单位

需求分析|泳道图 ProcessOn教学

文章目录 1.为什么使用泳道图2.具体例子一、如何绘制确定好泳道中枢的角色在中央基于事实来绘制过程不要纠结美观先画主干处理流程再画分支处理流程一个图表达不完,切分子流程过程数不超25 ,A4纸的幅面处理过程过程用动词短语最后美化并加上序号酌情加上…

Java--instanceof和类型转换

1.如图,Object,Person,Teacher,Student四类的关系已经写出来了,由于实例化的是Student类,因此,与Student类存在关系的类在使用instanceof时都会输出True,而无关的都会输出False&…

双色球 | python

1. 玩法规则 “双色球”每注投注号码由 6 个红色球号码和 1 个蓝色球号码组成。红色球号码从 1—33 中选择,蓝色球号码从 1—16 中选择。 球的数字匹配数量和颜色决定了是否中奖。 2. 需求 生成本期双色球中奖号码。(注意:1.生成的红球随机有…

光学传感器图像处理流程(一)

光学传感器图像处理流程(一) 1. 处理流程总览2. 详细处理流程2.1. 图像预处理2.1.1. 降噪处理2.1.2. 薄云处理2.1.3. 阴影处理 2.2. 辐射校正2.2.1. 辐射定标2.2.2. 大气校正2.2.3. 地形校正 2.3. 几何校正2.3.1. 图像配准2.3.2. 几何粗校正2.3.3. 几何精…

Python神经模型评估微分方程图算法

🎯要点 🎯神经网络映射关联图 | 🎯执行时间分析 | 🎯神经网络结构降维 | 🎯量化图结构边作用 | 🎯数学评估算法实现 🍪语言内容分比 🍇Python随机梯度下降算法 随机梯度下降是梯度…

【Python实战因果推断】18_线性回归的不合理效果8

目录 Saturated Regression Model Regression as Variance Weighted Average Saturated Regression Model 还记得我在本章开头强调回归和条件平均值之间的相似性吗?我向你展示了使用二元干预进行回归与比较干预组和对照组的平均值是完全一样的。现在,由…

逆向分析之电脑端如何调试一些只能手机端浏览器才可以打开的网站

手机端浏览器的指纹和电脑端浏览器的指纹是不同的,这样只在手机端浏览器运行的网站则可以检测网站是否满足手机端浏览器指纹的要求,不满足则可以进行一些反爬措施。 例如一些公众号,其实就是使用手机端浏览器打开的H5网站,就可以进行手机端浏览器指纹检测。 这里只是讲解下…