test_05_student_practice.py 2.2 KB
# -*- coding: utf-8 -*-
# ======================================
# @Software: PyCharm
# @Author  : Shitou ✊
# @Time    : 2023/1/9 16:32
# @FileName: test_05_student_practice.py
# ======================================
"""
学生小程序首页界面"实习界面"
"""
import json
import os
import unittest
import requests

from common.handle_config import conf
from common.handle_excel import Excel
from common.handle_log import HandleLog
from common.myddt import ddt, data
from common.handle_path import Student_practice
from tools.handle_token import StudentLoginToken


@ddt
class Test01SelectAllPractice(unittest.TestCase):
    """学生查看全部实习"""
    select_student_practice = Excel(os.path.join(Student_practice, "test_06__practice.xlsx"),
                                    "select_internship_practice")
    select_student_practice_case = select_student_practice.read_excel()

    @classmethod
    def setUpClass(cls):
        cls.token = StudentLoginToken.login_token()

    @data(*select_student_practice_case)
    def test01select_allpatice(self, case):
        # 准备数据
        data = json.loads(case["data"])
        expected = json.loads(case["expected"])

        # 调用接口
        url = conf.get("url", "url_ip") + case["url"]
        herders = {}
        herders["Authorization"] = self.token
        request = requests.request(url=url, method=case["method"], params=data, headers=herders)
        res = request.json()
        # 断言
        try:
            self.assertEqual(expected['msg'], res['msg'])
            self.assertEqual(expected['code'], res['code'])
        except AssertionError as e:
            # 写入Excel
            self.select_student_practice.write_excel(row=case["id"] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            HandleLog.log.exception(e)
            raise e
        else:
            self.select_student_practice.write_excel(row=case["id"] + 1, column=7, value="通过")
            # 将创建使用的数据写入到excel表格中
            self.select_student_practice.write_excel(row=case["id"] + 1, column=9, value=case["data"])
            HandleLog.log.info("用例{},执行通过".format(case["title"]))