test_03_teaching_affairs.py
5.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
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
# -*- coding: utf-8 -*-
# """======================================
# @Software: PyCharm
# @Author : Shitou ✊
# @Time : 2022/12/17 22:10
# @FileName: test_03_teaching_affairs.py
# ====================================="""
"""
实习教务管理菜单
"""
import json
import time
import unittest
import os
import requests
from jsonpath import jsonpath
from common.handle_log import HandleLog
from common.handle_path import DataExcel_Path
from common.myddt import ddt, data
from common.handle_excel import Excel
from common.handle_config import conf
from tools.fixture import SelectData
from tools.handle_token import LoginToken
from tools.fixture import RandomPhone, RandomIdentification, RandomEmail
TeachingAffairs_path = os.path.join(DataExcel_Path, "teaching_affairs")
# ==================教师相关的用例==================
@ddt
class Test01CTeacher(unittest.TestCase):
"""教师相关"""
# ---获取添加教师用例
add_teacher = Excel(os.path.join(TeachingAffairs_path, "test_03_teacher.xlsx"), "add_teacher")
add_teacher_case = add_teacher.read_excel()
# ---获取添加院系Excel表格
select_department_excel = Excel(os.path.join(os.path.join(DataExcel_Path, "system"), "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]
@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.teacher_name = "py自动化教师" + new_time
# # 随机院系代码
# self.department_code = "py_code" + new_time
# ====================================================================
# 新增教师
# ====================================================================
@data(*add_teacher_case)
def test_add_class_info(self, case):
"""新增教师用例"""
url = conf.get("url", "url_ip") + case['url']
# 准备数据
# ------判断教师姓名
if "#name#" in case["data"]:
case["data"] = case["data"].replace("#name#", str(self.teacher_name + "_" + str(case["id"])))
# ------判断教师姓名
if "#departmentId#" in case["data"]:
case["data"] = case["data"].replace("#departmentId#", str(self.department_id))
# ------判断教师手机号
if "#phone#" in case["data"]:
case["data"] = case["data"].replace("#phone#", RandomPhone.random_phone())
# ------判断教师身份证号
if "#idNumber#" in case["data"]:
case["data"] = case["data"].replace("#idNumber#", RandomIdentification().ran_end())
# ------判断教师身份证号
if "#email#" in case["data"]:
case["data"] = case["data"].replace("#email#", RandomEmail.rand_email())
data = json.loads(case["data"])
expected = json.loads(case["expected"])
herders = {}
herders["Authorization"] = self.token
# 调用接口
response = requests.request(url=url, method=case["method"], json=data, headers=herders)
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.add_teacher.write_excel(row=case["id"] + 1, column=7, value="不通过")
HandleLog.log.error("用例标题{},不通过".format(case['title']))
HandleLog.log.exception(e)
raise e
else:
self.add_teacher.write_excel(row=case["id"] + 1, column=7, value="通过")
# 将创建使用的数据写入到excel表格中
self.add_teacher.write_excel(row=case["id"] + 1, column=9,
value=case["data"])
HandleLog.log.info("用例{},执行通过".format(case["title"]))
'''
# ==================班级相关的用例==================
@ddt
class Test02Class(unittest.TestCase):
"""班级相关"""
add_class = Excel(os.path.join(TeachingAffairs_path, "test_03_class.xlsx"), "add_class")
add_class_case = add_class.read_excel()
# ====================================================================
# 新增班级
# ====================================================================
@data(*add_class_case)
def test_add_class_info(self, case):
"""新增班级用例"""
'''