接口测试流程

  1. 需求分析
  2. api文档分析
  3. 编写测试用例(文本用例)
  4. 搭建工具或代码所需环境
  5. 搭建项目结构
  6. 将功能文本用例转为代码(编写脚本)
  7. 封装被测试系统的接口
  8. 编写测试用例调用封装好的API接口
  9. 执行/生成测试报告

搭建项目结构

# 首先创建project文件夹
1、api:【Python Package】被封装的测试的接口
2、scripts:【Python Package】编写测试用例
3、data:【Directory】管理测试数据文件
4、report:【Directory】生成测试报告
5、tools:【Python Package】第三方的工具包
6、app.py:配置文件
7、run_suite.py:测试套件的入口

登录及地址管理测试用例编写

08_达达商城登录模块测试用例.png

06_dadashop地址管理.png

  • 定义相关配置
TOKEN = None
USERNAME = None
ID = None

HEADERS = {
    'Authorization':TOKEN
}
  • 封装登录接口
"""
封装登录接口
api/login.py
"""
import requests

class LoginAPI:
    def __init__(self):
        self.login_url = 'http://127.0.0.1:8000/v1/tokens'

    def login(self, login_data):
        return requests.post(url=self.login_url,
                             json=login_data)
  • 准备测试数据data/login.json
[
  {
    "desc": "登录成功",
    "login_data": {
      "username": "111111",
      "password": "111111",
      "carts": 0
    },
    "code": 200
  },
  {
    "desc": "用户名错误",
    "login_data": {
      "username": "1111111",
      "password": "111111",
      "carts": 0
    },
    "code": 10200
  },
  {
    "desc": "密码错误",
    "login_data": {
      "username": "111111",
      "password": "1111111",
      "carts": 0
    },
    "code": 10201
  }
]
  • 编写登录模块测试用例
import unittest
from api.login import LoginAPI
from parameterized import parameterized
import json
import app

def build_data():
    test_data = []
    json_file = './data/login.json'
    with open(json_file, encoding='utf-8') as f:
        json_data = json.load(f)
        for case_data in json_data:
            login_data = case_data.get('login_data')
            code = case_data.get('code')

            test_data.append((login_data, code))

    return test_data

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginAPI()

    @parameterized.expand(build_data())
    def test_login(self, login_data, code):
        response = self.login_api.login(login_data)

        self.assertEqual(code, response.json().get('code'))
  • 创建测试套件运行用例
import time
import unittest
from scripts.test01_login import TestLogin
# 将HTMLTestRunner.py文件复制到工具目录tools下
from tools.HTMLTestRunner import HTMLTestRunner

# 组装测试套件
suite = unittest.TestSuite()
# 登录接口测试用例
suite.addTest(unittest.makeSuite(TestLogin))


report_file = './report/report.html'

# 打开文件流
with open(report_file, "wb") as f:
    # 创建HTMLTestRunner运行器
    runner = HTMLTestRunner(f, title='API Report')
    # 执行测试套件
    runner.run(suite)
  • 地址管理模块测试用例实现
"""
api/address.py
封装地址管理功能
"""
import requests
import app

class AddressAPI:
    def __init__(self):
        # 新增 查询
        self.address_url = 'http://127.0.0.1:8000/v1/users/{}/address'.format(app.USERNAME)
        # 修改 删除
        self.address_modify_del_url = 'http://127.0.0.1:8000/v1/users/{}/address/{}'.format(app.USERNAME, app.ID)

    def add_address(self, add_address_data):
        """新增:POST"""
        return requests.post(url=self.address_url,
                             json=add_address_data,
                             headers=app.HEADERS)

    def get_address(self):
        """查询:GET"""
        return requests.get(url=self.address_url,
                            headers=app.HEADERS)

    def modify_address(self, new_address_data):
        """修改:PUT"""
        return requests.put(
            url=self.address_modify_del_url,
            json=new_address_data, headers=app.HEADERS)

    def delete_address(self, del_address_data):
        """删除:DELETE"""
        return requests.delete(
            url=self.address_modify_del_url,
            json=del_address_data, headers=app.HEADERS)
  • 编写地址管理测试用例
"""
scripts/test02_address.py
"""

import unittest
from api.address import AddressAPI

class TestAddress(unittest.TestCase):
    def setUp(self):
        self.address_api = AddressAPI()

    def test01_add_address(self):
        add_address_data = {
          'receiver':'小王',
          'receiver_phone':'18667018590',
          'address':'北京市东城区珠市口大街珍贝大厦2楼',
          'postcode':'722405',
          'tag':'公司'
        }
        response = self.address_api.add_address(add_address_data)
        # 断言
        self.assertEqual(200, response.json().get('code'))
  • 添加登录成功测试用例,回写token
    def test_login_success(self):
        login_data = {
                "username": "chaogege",
                "password": "123456",
                "carts": 0
            }
        response = self.login_api.login(login_data)

        # 回写app.py中的TOKEN和USERNAME
        app.TOKEN = response.json().get('data').get('token')
        app.USERNAME = response.json().get("username")
        app.HEADERS["Authorization"] = app.TOKEN
  • 测试套件添加测试用例
from scripts.test02_address import TestAddress

# 组装测试套件
suite = unittest.TestSuite()
# 登录接口测试用例
suite.addTest(TestLogin('test_login_success'))
suite.addTest(unittest.makeSuite(TestAddress))