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.日志打印如下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM