test_03_teaching_affairs.py 5.0 KB
# -*- 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):
        """新增班级用例"""
'''