python+requests+unittest(1)接口自动化测试框架结构介绍
1.项目整体结构如下图
2.配置如下
# -*- coding: utf-8 -*- [HTTP] scheme = http baseUrl = 127.0.0.1 port = 8888 timeout = 10.0 [USER] username = admin password = 123456 [EMAIL] on_off = off title = 接口自动化测试报告 message = 这是今天的测试报告,请查收! password = 1221421dfadfd receiver = test.qq.com server = smtp.qq.com sender = test.qq.com
3.测试服务api,test_api.py如下
import json import flask from flask import request # 创建一个服务,把当前这个python文件当做一个服务 server = flask.Flask(__name__) @server.route('/auth/login', methods=['get', 'post']) def login(): username = request.values.get('username') password = request.values.get('password') if username and password: if username == 'admin' and password == 'e10adc3949ba59abbe56e057f20f883e': response = {'code': 200, 'msg': '登录成功', 'data': {'token': '5ffccad5-b06e-4f20-a88e-0d4d31c0ab8d'}, 'userInfo': {'sex': '女', 'age': 25}} elif username == 'zsy' and password == 'e10adc3949ba59abbe56e057f20f883e': response = {'code': 200, 'msg': '登录成功', 'data': {'token': '3ffccad5-b06e-4f20-a88e-0d4d31c0ab8d'}, 'userInfo': {'sex': '男', 'age': 23}} else: response = {'code': 401, 'msg': '账号密码错误', 'userInfo': {}} else: response = {'code': 400, 'msg': '参数不能为空', 'userInfo': {}} response = json.dumps(response, ensure_ascii=False) print(response) return response @server.route('/statistics/homePage', methods=['get']) def homePage(): token = request.headers.get('Authorization') if token == 'Bearer 5ffccad5-b06e-4f20-a88e-0d4d31c0ab8d': response = {'code': 200, 'msg': 'success', 'pageInfo': {'title': '欢迎您'}} elif token == 'Bearer 3ffccad5-b06e-4f20-a88e-0d4d31c0ab8d': response = {'code': 403, 'msg': '权限不足', 'pageInfo': {}} else: response = {'code': 401, 'msg': '未授权'} response = json.dumps(response, ensure_ascii=False) print(response) return response if __name__ == '__main__': server.run(debug=True, port=8888, host='127.0.0.1')
3.测试用例如下
import unittest from src.api.loginApi import LoginClient class TestLogin(unittest.TestCase): def setUp(self): self.client = LoginClient() def test_login_success(self): """登录成功""" self.client.login('admin', '123456') self.assertEqual(200, self.client.code) def test_login_error(self): """登录失败""" self.client.login('admin', '111') self.assertEqual(400, self.client.code) def test_login_null(self): """参数为空""" self.client.login() self.assertEqual(400, self.client.code) if __name__ == '__main__': unittest.main() import json import unittest from parameterized import parameterized from src.api.loginApi import LoginClient from src.common.readExcel import ReadExcel login_xls = ReadExcel().get_xls('loginCase.xlsx', 'login') class TestUserLogin(unittest.TestCase): def setUp(self): self.client = LoginClient() @parameterized.expand(login_xls) def test_login(self, case_name, query): self.checkResult(case_name, query) def tearDown(self): pass def checkResult(self, case_name, query): # 断言 """ check test result :return: """ client = self.client query = json.loads(query) client.requestBody = query client.login() if case_name == 'login': # 如果case_name是login,说明合法,返回的code应该为200 self.assertEqual(200, client.code) if case_name == 'login_error': # 同上 self.assertEqual(400, client.code) if case_name == 'login_null': # 同上 self.assertEqual(400, client.code) if __name__ == '__main__': unittest.main()
4.html报告如下
5.日志打印如下