test_02_system_management.py
11.2 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/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
from tools.fixture import SelectData
from jsonpath import jsonpath
# 系统管理相关的用例目录
System_path = os.path.join(DataExcel_Path, "system")
# ==================院系相关的用例==================
@myddt.ddt
class Test01Department(unittest.TestCase):
"""院系"""
# 新增院系用例
open_add_excel = Excel(os.path.join(System_path, "test_02_department.xlsx"), "add_department")
test_add_case = open_add_excel.read_excel()
# 查看院系用例
open_select_excel = Excel(os.path.join(System_path, "test_02_department.xlsx"), "select_department")
test_select_case = open_select_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_add_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_add_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_add_excel.write_excel(row=case["id"] + 1, column=7, value="通过")
# 将创建使用的数据写入到excel表格中
self.open_add_excel.write_excel(row=case["id"] + 1, column=9,
value=case["data"])
HandleLog.log.info("用例{},执行通过".format(case["title"]))
# ====================================================================
# 查看院系
# ====================================================================
@myddt.data(*test_select_case)
def test_select_department(self, select_case):
"""查看院系"""
url = conf.get("url", "url_ip") + select_case['url']
# 准备数据
# -----写入数据
data = json.loads(select_case['data'])
# -----预期结果
expected = json.loads(select_case['expected'])
# 调用接口
# 请求头
headers = {}
headers["Authorization"] = self.token
response = requests.request(method=select_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.select_excel.write_excel(row=select_case["id"] + 1, column=7, value="不通过")
HandleLog.log.error("用例标题{},不通过".format(select_case['title']))
HandleLog.log.exception(e)
raise e
else:
self.open_select_excel.write_excel(row=select_case["id"] + 1, column=7, value="通过")
# 将创建使用的数据写入到excel表格中
self.open_select_excel.write_excel(row=select_case["id"] + 1, column=9,
value=select_case["data"])
HandleLog.log.info("用例{},执行通过".format(select_case["title"]))
# ==================专业相关的用例==================
@myddt.ddt
class Test02Specialty(unittest.TestCase):
"""专业相关的用例"""
open_speciality_excel = Excel(os.path.join(System_path, "test_02_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):
"""新增专业"""
# 准备数据
# --写入数据
# 查看获取院系Excel表格的url
select_department_excel = Excel(os.path.join(System_path, "test_02_department.xlsx"), "select_department")
select_url = select_department_excel.read_excel_location("C2")
department_list = SelectData(str(select_url))
department_json = department_list.select_list()
# 获取院系id
department_id = jsonpath(department_json, "$..id")[0]
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"]))
if "#departmentId#" in case["data"]:
case["data"] = case["data"].replace("#departmentId#", department_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="通过")
# 将创建使用的数据写入到excel表格中
self.open_speciality_excel.write_excel(row=case["id"] + 1, column=9,
value=case["data"])
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_02_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="通过")
# 将创建使用的数据写入到excel表格中
self.open_year_excel.write_excel(row=case["id"] + 1, column=9,
value=case["data"])
HandleLog.log.info("用例{},执行通过".format(case["title"]))