文章目录
前言
一、实现登录接口对象封装和调用
1.0 登录接口的接口测试文档
接口信息:1.验证码:(1)地址:http://kdtx-test.itheima.net/api/captchaImage(2)方法:get
2.登录:(1)地址:http://kdtx-test.itheima.net/api/login(2)方法:Post(3)请求数据:(4)请求头:Content-Type: application/json(5)请求体:{"username": "admin", "password": " admin123","code":"2", "uuid":"验证码接口返回数据"}
1.1 接口对象层(封装)
封装的重要概念:
接口封装时,重点是依据接口文档封装接口信息,需要使用的测试数据是从测试用例传递的、接口方法被调用时需要返回对应的响应结果。封装是通过根据接口API文档封装,所以定义在API的目录下。
实现的基本步骤:
(1)导包操作:# 导包 import requests
(2)创建接口的类:
# 创建接口类 class LoginAPI:
(3)创建初始化方法:
初始化方法中需要指定接口的基本信息url。
# 初始化 def __init__(self): # 指定url基本信息 self.url_verify = "http://kdtx-test.itheima.net/api/captchaImage" self.url_login = "http://kdtx-test.itheima.net/api/login"
(4)创建验证码、登录方法:
接口方法被调用时候,需要返回对应的响应结果信息。只有返回之后,才能在测试用例中使用倒响应结果的值。
# 验证码接口方法 def get_verify_code(self): return requests.get(url=self.url_verify) # 登录接口方法 def login(self, test_data): return requests.post(url=self.url_login, json=test_data)
备注:
(1)登录接口方法中的test_data。
因为要从外部传入测试数据,所以在方法定义时候创建一个参数来接收传递的数据。
(2)json = test_data
此处数据是请求体json中的数据。
因为json数据的请求体在request格式中是通过json参数进行处理。即:json = test_data.