- 接口自动化框架
登录接口
- 普通方式实现
import unittest
import requests
class TestIhrmLogin(unittest.TestCase):
# 测试方法1,登录成功
def test01_login_success(self):
# 组织url
url = "http://ihrm-test.itheima.net/api/sys/login"
header = {
"Content-Type": "application/json"}
json_data = {
"mobile": "13800000002", "password": "123456"}
resp = requests.post(url=url, headers=header, json=json_data)
print("登录成功:", resp.json())
# 断言
self.assertEqual(200, resp.status_code)
self.assertEqual(True, resp.json().get("success"))
self.assertEqual(10000, resp.json().get("code"))
self.assertIn("操作成功", resp.json().get("message"))
# 测试方法2,密码错误
def test02_pwd_err(self):
# 组织url
url = "http://ihrm-test.itheima.net/api/sys/login"
header = {
"Content-Type": "application/json"}
json_data = {
"mobile": "13800000002", "password": "123456789"}
resp = requests.post(url=url, headers=header, json=json_data)
print("密码错误:", resp.json())
# 断言
self.assertEqual(200, resp.status_code)
self.assertEqual(False, resp.json().get("success"))
self.assertEqual(20001, resp.json().get("code"))
self.assertIn("用户名或密码错误", resp.json().get("message"))
- 登录接口对象层
思路:
- 动态变化的,写入参数
- 固定不变,直接写到方法实现内
- 响应结果,通过返回值 return
分析:
封装实现:
import requests
"""
思路:
> - 动态变化的,写入参数
> - 固定不变,直接写到方法实现内
> - 响应结果,通过返回值 return
"""
class IhrmLoginApi(object):
# 登录方法
@classmethod
def login(cls, json_data):
url = "http://ihrm-test.itheima.net/api/sys/login"
header = {
"Content-Type": "application/json"}
# json_data = {
"mobile": "13800000002", "password": "123456"}
resp = requests.post(url=url, headers=header, json=json_data)
return resp
if __name__ == '__main__':
json_data = {
"mobile": "13800000002", "password": "123456"}
res = IhrmLoginApi.login(json_data)
print(res.json())
- 登录接口测试用例层
1、在 scripts/ 下,创建 test_ihrm_login.py 文件
2. 在 文件内,创建 测试类 TestIhrmLogin 从 unittest.TestCase 继承
3. 添加 测试方法, 并实现
import unittest
from api.ihrm_login_api import IhrmLoginApi
class TestIhrmLogin(unittest.TestCase):
# 登录成功
def test01_login_success(self):
# 1、组织请求数据
json_data = {
"mobile": "13800000002", "password": "123456"}
# 调用自己封装的接口
resp = IhrmLoginApi.login(json_data)
print("登录成功:", resp.json())
# 断言
self.assertEqual(200, resp.status_code)
self.assertEqual(True, resp.json().get("success"))
self.assertEqual(10000, resp.json().get("code"))
self.assertIn("操作成功", resp.json().get("message"))
# 手机号为空
def test02_moblie_none(self):
# 1、组织请求数据
json_data = {
"mobile": None, "password": "123456"}
# 调用自己封装的接口
resp = IhrmLoginApi.login(json_data)
print("手机号为空:", resp.json())
# 断言
self.assertEqual(200, resp.status_code)
self.assertEqual(True, resp.json().get("success"))
self.assertEqual(10000, resp.json().get("code"))
self.assertIn("用户名或密码错误", resp.json().get("message"))
# 密码错误
def test03_pwd_err(self):
# 组织请求数据
json_data = {
"mobile": "13800000002", "password": "123456890"}
# 调用自己封装的接口
resp = IhrmLoginApi.login(json_data)
print("密码错误:", resp.json())
# 断言
self.assertEqual(200, resp.status_code)
self.assertEqual(False, resp.json().get("success"))
self.assertEqual(20001, resp.json().get("code"))
self.assertIn("用户名或密码错误", resp.json().get("message"))
- 封装断言方法
1、 在 common/ 下,新建文件 assert_util.py 文件,
2. 在文件内,添加 函数 assert_util()
3. 在 函数内,实现通用的断言函数。
4. 在 测试方法中,使用 直接封装的 通用断言函数, 实现断言
# 定义通用断言方法
def assert_util(self, resp, status_code, success, code, message):
self.assertEqual(status_code, resp.status_code)
self.assertEqual(success, resp.json().get("success"))
self.assertEqual(code, resp.json().get("code"))
self.assertIn(message, resp.json().get("message"))
使用断言方法:
import unittest
from api.ihrm_login_api import IhrmLoginApi
from common.assert_util import assert_util
class TestIhrmLogin(unittest.TestCase):
# 登录成功
def test01_login_success(self):
# 1、组织请求数据
json_data = {
"mobile": "13800000002", "password": "123456"}
# 调用自己封装的接口
resp = IhrmLoginApi.login(json_data)
print("登录成功:", resp.json())
# 断言
# self.assertEqual(200, resp.status_code)
# self.assertEqual(True, resp.json().get("success"))
# self.assertEqual(10000, resp.json().get("code"))
# self.assertIn("操作成功", resp.json().get("message"))
assert_util(self, 200, True, 10000, "操作成功")
# 手机号为空
def test02_moblie_none(self):
# 1、组织请求数据
json_data = {
"mobile": None, "password": "123456"}
# 调用自己封装的接口
resp = IhrmLoginApi.login(json_data)
print("手机号为空:", resp.json())
# 断言
# self.assertEqual(200, resp.status_code)
# self.assertEqual(False, resp.json().get("success"))
# self.assertEqual(20001, resp.json().get("code"))
# self.assertIn("用户名或密码错误", resp.json().get("message"))
assert_util(self, 200, False, 20001, "用户名或密码错误")
# 密码错误
def test03_pwd_err(self):
# 组织请求数据
json_data = {
"mobile": "13800000002", "password": "123456890"}
# 调用自己封装的接口
resp = IhrmLoginApi