• 【接口测试】Day7-接口测试框架


    今日目标

    接口测试框架

    • 能够按照接口测试框架的封装思想,实现TPshop登录接口的接口对象封装
    • 能够按照接口测试框架的封装思想,使用UnitTest编写测试用例实现接口的测试
    • 能够实现接口测试框架项目目录结构的定义
    • 能够提取测试脚本中的测试数据,实现测试数据参数化
    • 能够使用HTMLTestRunner生成接口测试报告

    一、接口测试框架开发

    1 框架结构

    重点说明:

    (1)核心在于将测试用例与被测试系统API进行分离,便于后期维护

    (2)测试用例是通过unittest进行管理,并提供了丰富的断言(等于、包含等)

    (3)可以通过参数化思想测试数据与测试脚本的分离

    (4)可以调用数据库进行结果验证或将数据库作为参数化的数据源

    (5)借助第三方工具快速的生成HTML报告

    2 框架目录结构

     tpshop        -- 项目代号

    • data        -- 管理测试数据的文件夹
    • report        -- 管理测试结果报告的文件夹 
    • api        -- 封装被测试系统的接口
    • scripts        -- 测试用例脚本
    • tools        -- 第三方工具包管理
    • app.py        -- 配置信息文件
    • run_suite.py        -- 测试用例执行入口 
    • utils.py        -- 自定义工具类

    3 封装被测试系统接口

    1. # 封装被测试系统接口
    2. # 定义接口类
    3. class LoginAPI:
    4. # 初始化
    5. def __init__(self):
    6. self.url_verify = "http://localhost/index.php?m=Home&c=User&a=verify"
    7. self.url_login = "http://localhost/index.php?m=Home&c=User&a=do_login"
    8. # 获取验证码接口
    9. def get_verify_code(self, session):
    10. return session.get(self.url_verify)
    11. # 登录接口
    12. def login(self, session, username, password, verify_code):
    13. login_data = {
    14. "username": username,
    15. "password": password,
    16. "verify_code": verify_code
    17. }
    18. return session.post(url=self.url_login, data=login_data)

    4 定义接口测试用例 

    1. """
    2. 定义接口测试用例
    3. 使用unittest
    4. 1.导包
    5. 2.创建测试类
    6. 2.1 前置处理
    7. 2.2 后置处理
    8. 2.3.创建测试方法
    9. """
    10. # 1.导包
    11. import requests
    12. import unittest
    13. from api.login import LoginAPI
    14. # 2.创建测试类
    15. class TestLogin(unittest.TestCase):
    16. # 2.1 前置处理
    17. def setUp(self):
    18. self.login_api = LoginAPI() # 实例化接口类
    19. self.session = requests.Session() # 创建session对象
    20. # 2.2 后置处理
    21. def tearDown(self):
    22. if self.session:
    23. self.session.close()
    24. # 2.3.创建测试用例
    25. # 登录成功
    26. def test01_login_success(self):
    27. # 调用验证码接口获取验证,并进行断言
    28. response = self.login_api.get_verify_code(self.session)
    29. self.assertEqual(200, response.status_code)
    30. self.assertIn("image", response.headers.get("Content-Type"))
    31. # 调用登录接口获取登录信息,并进行断言
    32. response = self.login_api.login(self.session, "13488888888", "123456", "8888")
    33. print(response.json())
    34. self.assertEqual(200, response.status_code)
    35. self.assertEqual(1, response.json().get("status"))
    36. self.assertIn("登陆成功", response.json().get("msg"))
    37. # 账号不存在
    38. def test02_user_is_not_exist(self):
    39. # 调用验证码接口获取验证,并进行断言
    40. response = self.login_api.get_verify_code(self.session)
    41. self.assertEqual(200, response.status_code)
    42. self.assertIn("image", response.headers.get("Content-Type"))
    43. # 调用登录接口获取登录信息,并进行断言
    44. response = self.login_api.login(self.session, "13488888899", "123456", "8888")
    45. print(response.json())
    46. self.assertEqual(200, response.status_code)
    47. self.assertEqual(-1, response.json().get("status"))
    48. self.assertIn("账号不存在", response.json().get("msg"))
    49. # 密码错误
    50. def test03_password_error(self):
    51. # 调用验证码接口获取验证,并进行断言
    52. response = self.login_api.get_verify_code(self.session)
    53. self.assertEqual(200, response.status_code)
    54. self.assertIn("image", response.headers.get("Content-Type"))
    55. # 调用登录接口获取登录信息,并进行断言
    56. response = self.login_api.login(self.session, "13488888888", "error", "8888")
    57. print(response.json())
    58. self.assertEqual(200, response.status_code)
    59. self.assertEqual(-2, response.json().get("status"))
    60. self.assertIn("密码错误", response.json().get("msg"))

    5 集成测试报告

    1. # 导包
    2. import time
    3. import unittest
    4. from scripts.test01_login import TestLoginAPI
    5. from tools.HTMLTestRunner import HTMLTestRunner
    6. # 封装测试套件
    7. suite = unittest.TestSuite()
    8. suite.addTest(unittest.makeSuite(TestLoginAPI))
    9. # 指定报告路径
    10. report = "./report/report-{}.html".format(time.strftime("%Y%m%d-%H%M%S"))
    11. # 打开文件流
    12. with open(report, "wb") as f:
    13. # 创建HTMLTestRunner执行器
    14. runner = HTMLTestRunner(f, title="接口测试报告")
    15. # 执行测试套件
    16. runner.run(suite)

    6 测试数据参数化

    6.1 基于json文件实现参数化

    1. # 导包
    2. import json
    3. import requests
    4. import unittest
    5. from api.login import LoginAPI
    6. from parameterized import parameterized
    7. # 构造测试数据
    8. def build_data():
    9. json_file = "../data/login.json"
    10. test_data = []
    11. with open(json_file, encoding="utf-8") as f:
    12. json_data = json.load(f)
    13. for case_data in json_data:
    14. username = case_data.get("username")
    15. password = case_data.get("password")
    16. verify_code = case_data.get("verify_code")
    17. status_code = case_data.get("status_code")
    18. content_type = case_data.get("content_type")
    19. status = case_data.get("status")
    20. msg = case_data.get("msg")
    21. test_data.append((username, password, verify_code, status_code, content_type, status, msg))
    22. print("test_data = {}".format((username, password, verify_code, status_code, content_type, status, msg)))
    23. return test_data
    24. # 定义测试类
    25. class TestLoginAPI(unittest.TestCase):
    26. # 前置处理
    27. def setUp(self):
    28. self.login_api = LoginAPI()
    29. self.session = requests.Session()
    30. # 后置处理
    31. def tearDown(self):
    32. if self.session:
    33. self.session.close()
    34. # 定义测试方法
    35. @parameterized.expand(build_data)
    36. def test01_login(self, username, password, verify_code, status_code,
    37. content_type, status, msg):
    38. # 调用验证码接口
    39. response = self.login_api.get_verify_code(self.session)
    40. # 断言
    41. self.assertEqual(status_code, response.status_code)
    42. self.assertIn(content_type, response.headers.get("Content-Type"))
    43. # 调用登录接口
    44. response = self.login_api.login(self.session, username, password, verify_code)
    45. print(response.json())
    46. self.assertEqual(status_code, response.status_code)
    47. self.assertEqual(status, response.json().get("status"))
    48. self.assertIn(msg, response.json().get("msg"))

    6.2 基于数据库实现参数化

    1. # 导包
    2. import requests
    3. import unittest
    4. from api.login import LoginAPI
    5. from tools.dbutil import DBUtil
    6. from parameterized import parameterized
    7. # 构造测试数据
    8. def build_data():
    9. sql = "select * from t_login"
    10. db_data = DBUtil.exe_sql(sql)
    11. print(db_data)
    12. test_data = []
    13. for case_data in db_data:
    14. username = case_data[2]
    15. password = case_data[3]
    16. verify_code = case_data[4]
    17. status_code = case_data[5]
    18. content_type = case_data[6]
    19. status = case_data[7]
    20. msg = case_data[8]
    21. test_data.append((username, password, verify_code, status_code, content_type, status, msg))
    22. print("test_data = {}".format((username, password, verify_code, status_code, content_type, status, msg)))
    23. return test_data
    24. # 定义测试类
    25. class TestLoginAPI(unittest.TestCase):
    26. # 前置处理
    27. def setUp(self):
    28. self.login_api = LoginAPI()
    29. self.session = requests.Session()
    30. # 后置处理
    31. def tearDown(self):
    32. if self.session: self.session.close()
    33. # 定义测试方法
    34. @parameterized.expand(build_data)
    35. def test01_login(self, username, password, verify_code, status_code,
    36. content_type, status, msg):
    37. # 调用验证码接口
    38. response = self.login_api.get_verify_code(self.session)
    39. # 断言
    40. self.assertEqual(status_code, response.status_code)
    41. self.assertIn(content_type, response.headers.get("Content-Type"))
    42. # 调用登录接口
    43. response = self.login_api.login(self.session, username, password, verify_code)
    44. print(response.json())
    45. self.assertEqual(status_code, response.status_code)
    46. self.assertEqual(status, response.json().get("status"))
    47. self.assertIn(msg, response.json().get("msg"))

    二、IHRM项目实战任务

    • 登录
    • 员工管理
    • 任务
      • 搭建IHRM项目接口测试框架
      • 封装IHRM登录接口
      • 创建登录模块的测试用例
        • 普通模式
        • 参数化
      • 生成HTML测试报告

    实战任务见下一篇

  • 相关阅读:
    【移动端h5常用的几款插件 】
    Bert-vits2最终版Bert-vits2-2.3云端训练和推理(Colab免费GPU算力平台)
    #卷妹带你回顾Java基础(一)每日更新Day9
    尚硅谷Flink(完)FlinkSQL
    一个基于.Net高性能跨平台内网穿透工具
    vue路由
    PackML 学习笔记(2) OPCUA /PackML
    【C#学习】button:只显示图片
    数据库知识点整合
    聊聊数据库连接池 Druid
  • 原文地址:https://blog.csdn.net/whowhowhoisimportant/article/details/124907191