test_01_login.py 2.0 KB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2022/12/8 21:53
# @Author  : Shitou
# @FileName: test_login.py
# @Software: PyCharm
"""
优秀学乐业----登陆测试用例
"""
import json
import os
import unittest
import requests
from common import myddt
from common.handle_excel import Excel
from common.handle_log import HandleLog
from common.handle_path import DataExcel_Path
from jsonpath import jsonpath
from common.handle_config import conf

headers = {
    "Authorization": "Basic" + " d29ya2FpOjEyMzQ1Ng=="
}


@myddt.ddt
class TestLogin(unittest.TestCase):
    open_excel = Excel(os.path.join(DataExcel_Path, "test_01_login.xlsx"), "login")
    test_case = open_excel.read_excel()
    @myddt.data(*test_case)
    def test_login(self, case):

        # 准备数据
        data = eval(case['data'])
        expected = json.loads(case['expected'])
        # 请求接口
        url = conf.get("url", "url_ip") + case['url']
        method = case['method']
        # req = requests.request(method=method, url=url, json=data, headers=headers)
        req = requests.post(url=url, json=data, headers=headers)
        res = req.json()
        # 将数据在测试报告中进行展示
        print("用例入参:{}".format(data))
        print("预期结果:{}".format(expected))
        print("实际结果:{}".format(res))
        # 断言
        try:
            self.assertEqual(expected['msg'], res['msg'])
            self.assertEqual(expected['code'], res['code'])
        except AssertionError as e:
            # 结果写入到excel中
            self.open_excel.write_excel(row=case['id'] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            #  将异常信息输出到日志,等级为Error
            HandleLog.log.exception(e)
            raise e
        else:
            # 结果写入到excel中
            self.open_excel.write_excel(row=case['id'] + 1, column=7, value="通过")
            HandleLog.log.error("用例标题{},通过".format(case['title']))