test_02_system_management.py
7.5 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/12/8 23:53
# @Author : Shitou
# @FileName: test_02_department.xlsx.py
# @Software: PyCharm
"""
系统管理菜单用例
"""
import json
import os
import unittest
import requests
import time
from common import myddt
from common.handle_excel import Excel
from common.handle_log import HandleLog
from common.handle_path import DataExcel_Path
from common.handle_config import conf
from tools.handle_token import LoginToken
# 系统管理相关的用例目录
System_path = os.path.join(DataExcel_Path, "system")
# 院系相关的用例
@myddt.ddt
class Test01Department(unittest.TestCase):
"""院系"""
open_excel = Excel(os.path.join(System_path, "test_02_department.xlsx"), "add_department")
test_case = open_excel.read_excel()
@classmethod
def setUpClass(cls):
"""
用例类执行前执行的函数
:return:token
"""
# 获取token
cls.token = LoginToken.login_token()
def setUp(self):
"""单条用例执行前执行的函数"""
new_time = time.strftime("%Y%m%d_%H:%M:%S")
# 随机院系名称
self.department_name = "py自动化院系" + new_time
# 随机院系代码
self.department_code = "py_code" + new_time
@myddt.data(*test_case)
def test_add_department(self, case):
"""新增院系"""
url = conf.get("url", "url_ip") + case['url']
# 准备数据
# -----写入数据
if "#department#" in case["data"]:
case["data"] = case["data"].replace("#department#", self.department_name + "_" + str(case["id"]))
if "#department_code#" in case["data"]:
case["data"] = case["data"].replace("#department_code#", self.department_code + "_" + str(case["id"]))
data = json.loads(case['data'])
# -----预期结果
expected = json.loads(case['expected'])
# 调用接口
# 请求头
headers = {}
headers["Authorization"] = self.token
response = requests.request(method=case["method"], url=url, json=data, headers=headers)
res = response.json()
print("用例入参:{}".format(data))
print("预期结果:", expected)
print("实际结果:", 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']))
HandleLog.log.exception(e)
raise e
else:
self.open_excel.write_excel(row=case["id"] + 1, column=7, value="通过")
# 将创建使用的数据写入到excel表格中
self.open_excel.write_excel(row=case["id"] + 1, column=9,
value=case["data"])
HandleLog.log.info("用例{},执行通过".format(case["title"]))
# 专业相关的用例
@myddt.ddt
class Test02Specialty(unittest.TestCase):
"""专业相关的用例"""
open_speciality_excel = Excel(os.path.join(System_path, "test_03_specialty.xlsx"), "add_specialty")
test_data = open_speciality_excel.read_excel()
@classmethod
def setUpClass(cls):
"""
:return: token
"""
# 获取token
cls.token = LoginToken.login_token()
def setUp(self):
"""单条用例执行前执行的函数"""
new_time = time.strftime("%H:%M:%S")
# 随机专业名称
self.specialty_name = "py专业" + new_time
# 随机专业代码
self.specialty_code = "py_code" + new_time
@myddt.data(*test_data)
def test_add_specialty(self, case):
"""新增专业"""
# 准备数据
# --写入数据
if "#specialty_name#" in case["data"]:
case["data"] = case["data"].replace("#specialty_name#", self.specialty_name + "_" + str(case["id"]))
if "#specialty_code#" in case["data"]:
case["data"] = case["data"].replace("#specialty_code#", self.specialty_code + "_" + str(case["id"]))
data = json.loads(case["data"])
# --预期结果
expected = json.loads(case["expected"])
# 调用接口
url = conf.get("url", "url_ip") + case["url"]
headers = {}
headers["Authorization"] = self.token
req = requests.request(method=case["method"], url=url, json=data, headers=headers)
res = req.json()
print("用例入参:{}".format(data))
print("预期结果:", expected)
print("实际结果:", res)
# 断言
try:
self.assertEqual(expected["msg"], res["msg"])
self.assertEqual(expected["code"], res["code"])
except AssertionError as e:
self.open_speciality_excel.write_excel(row=case["id"] + 1, column=7, value="不通过")
HandleLog.log.error("用例标题{},不通过".format(case['title']))
HandleLog.log.exception(e)
raise e
else:
self.open_speciality_excel.write_excel(row=case["id"] + 1, column=7, value="通过")
HandleLog.log.info("用例{},执行通过".format(case["title"]))
# 校历(学年学期)相关的用例---2022 12.15
@myddt.ddt
class Test03Year(unittest.TestCase):
"""校历相关的用例"""
open_year_excel = Excel(os.path.join(System_path, "test_04_term.xlsx"), "add_term")
test_data = open_year_excel.read_excel()
@classmethod
def setUpClass(cls):
"""
:return: token
"""
# 获取token
cls.token = LoginToken.login_token()
def setUp(self):
"""单条用例执行前执行的函数"""
new_time = time.strftime("%H:%M:%S")
# 随机学年名称
self.year_name = "py学年" + new_time
# 随机学年备注
self.year_comment = "py_comment备注" + new_time
@myddt.data(*test_data)
def test_add_year(self, case):
"""新增专业"""
# 准备数据
# --写入数据
if "#year#" in case["data"]:
case["data"] = case["data"].replace("#year#", self.year_name + "_" + str(case["id"]))
if "#remark#" in case["data"]:
case["data"] = case["data"].replace("#remark#", self.year_comment + "_" + str(case["id"]))
data = json.loads(case["data"])
# --预期结果
expected = json.loads(case["expected"])
# 调用接口
url = conf.get("url", "url_ip") + case["url"]
headers = {}
headers["Authorization"] = self.token
req = requests.request(method=case["method"], url=url, json=data, headers=headers)
res = req.json()
print("用例入参:{}".format(data))
print("预期结果:", expected)
print("实际结果:", res)
# 断言
try:
self.assertEqual(expected["msg"], res["msg"])
self.assertEqual(expected["code"], res["code"])
except AssertionError as e:
self.open_year_excel.write_excel(row=case["id"] + 1, column=7, value="不通过")
HandleLog.log.error("用例标题{},不通过".format(case['title']))
HandleLog.log.exception(e)
raise e
else:
self.open_year_excel.write_excel(row=case["id"] + 1, column=7, value="通过")
HandleLog.log.info("用例{},执行通过".format(case["title"]))