test_01_login.py
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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']))