test_04_Internship_preparation.py 22.7 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
# -*- coding: utf-8 -*-
# ======================================
# @Software: PyCharm
# @Author  : Shitou ✊
# @Time    : 2022/12/29 21:19
# @FileName: test_04_Internship_preparation.py
# ======================================
"""
实习准备模块
"""
import json
import time
import unittest
import os
import requests
from jsonpath import jsonpath
from common.handle_config import conf
from common.handle_excel import Excel
from common.handle_log import HandleLog
from common.handle_path import DataExcel_Path, System_path, TeachingAffairs_path, Internship_path
from common.myddt import ddt, data
from tools.fixture import SelectData
from tools.handle_token import LoginToken


# ==================实习计划相关的用例==================

# ====================================================================
#                               新增计划
# ====================================================================
@ddt
class Test01addInternshipPlan(unittest.TestCase):
    add_plan = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"), "add_internship_plan")  # 新增计划Excel
    add_plan_case = add_plan.read_excel()

    @classmethod
    def setUpClass(cls):
        # 获取登陆token
        cls.token = LoginToken.login_token()
        # ---获取添加院系id
        select_department_excel = Excel(os.path.join(System_path, "test_02_department.xlsx"),
                                        "select_department")
        read_department_excel = select_department_excel.read_excel_location("C2")
        department_list = SelectData(str(read_department_excel))
        department_json = department_list.select_list_positive()
        cls.department_id = jsonpath(department_json, "$..id")[-1]  # 获取院系id
        # ---获取查看专业id
        select_specialty_excel = Excel(os.path.join(System_path, "test_02_specialty.xlsx"),
                                       "select_specialty")
        read_specialty_excel = select_specialty_excel.read_excel_location("C2")
        specialty_list = SelectData(str(read_specialty_excel))
        specialty_json = specialty_list.select_list()
        cls.professionalId = jsonpath(specialty_json, "$..id")[0]  # 获取专业id
        # ---获取查看班级id
        select_class_excel = Excel(os.path.join(TeachingAffairs_path, "test_03_class.xlsx"),
                                   "select_class")
        read_class_excel = select_class_excel.read_excel_location("C2")
        class_list = SelectData(str(read_class_excel))
        class_json = class_list.select_list()
        cls.classId = jsonpath(class_json, "$..id")[0]  # 获取班级id
        # ---获取课程id
        select_course_excel = Excel(os.path.join(TeachingAffairs_path, "test_03_course.xlsx"),
                                    "select_course")
        read_course_excel = select_course_excel.read_excel_location("C2")
        course_list = SelectData(str(read_course_excel))
        course_json = course_list.select_list()
        cls.courseId = jsonpath(course_json, "$..id")[0]  # 获取课程id
        # ---获取教师id
        select_teacher_excel = Excel(os.path.join(TeachingAffairs_path, "test_03_teacher.xlsx"),
                                     "select_teacher")
        read_teacher_excel = select_teacher_excel.read_excel_location("C2")
        teacher_list = SelectData(str(read_teacher_excel))
        teacher_json = teacher_list.select_list()
        cls.teacherId = jsonpath(teacher_json, "$..id")[0]  # 获取教师id
        # ---获取查看学期id
        select_term_excel = Excel(os.path.join(System_path, "test_02_term.xlsx"),
                                  "select_term")
        read_term_excel = select_term_excel.read_excel_location("C2")
        term_list = SelectData(str(read_term_excel))
        term_json = term_list.select_list()
        cls.termsId = term_json["data"]["records"][0]["terms"][0]["id"]  # 获取学年学期id
        # ---获取计划id
        select_plan_excel = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                                  "select_internship_plan")
        read_plan_excel = select_plan_excel.read_excel_location("C2")
        read_plan_excel_d = select_plan_excel.read_excel_location("E2")  # 读取params
        plan_list = SelectData(str(read_plan_excel), json.loads(read_plan_excel_d))
        plan_json = plan_list.select_list()
        cls.planId = jsonpath(plan_json, "$..id")[0]  # 获取待发布计划id

    def setUp(self):
        """单条用例执行前执行的函数"""
        new_time = time.strftime("%Y%m%d_%H:%M:%S")
        # 随机计划名称
        self.name = "py自动化计划" + new_time

    # ====================================================================
    #                               新增计划
    # ====================================================================
    @data(*add_plan_case)
    def test_add_plan(self, case):
        """新增计划用例"""
        url = conf.get("url", "url_ip") + case["url"]
        # 准备数据
        if "#name#" in case["data"]:
            case["data"] = case["data"].replace("#name#", str(self.name + "_" + str(case["id"])))  # 名称
        if "#courseId#" in case["data"]:
            case["data"] = case["data"].replace("#courseId#", str(self.courseId))  # 课程
        if "#departmentId#" in case["data"]:
            case["data"] = case["data"].replace("#departmentId#", str(self.department_id))  # 院系id
        if "#professionalIds#" in case["data"]:
            case["data"] = case["data"].replace("#professionalIds#", str(self.professionalId))  # 专业id
        if "#classInfoId#" in case["data"]:
            case["data"] = case["data"].replace("#classInfoId#", str(self.classId))  # 班级id
        if "#teacherIds#" in case["data"]:
            case["data"] = case["data"].replace("#teacherIds#", str(self.teacherId))  # 教师id
        if "#termId#" in case["data"]:
            case["data"] = case["data"].replace("#termId#", str(self.termsId))  # 学期id

        # 准备数据
        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_plan.write_excel(row=case["id"] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            HandleLog.log.exception(e)
            raise e
        else:
            self.add_plan.write_excel(row=case["id"] + 1, column=7, value="通过")
            # 将创建使用的数据写入到excel表格中
            self.add_plan.write_excel(row=case["id"] + 1, column=9, value=case["data"])
            HandleLog.log.info("用例{},执行通过".format(case["title"]))


# ====================================================================
#                               提交审核计划
# ====================================================================
@ddt
class Test02submitInternshipPlan(unittest.TestCase):
    submit_plan = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                        "submit_internship_plan")  # 提交审核计划
    submit_plan_case = submit_plan.read_excel()

    @classmethod
    def setUpClass(cls):
        # 获取登陆token
        cls.token = LoginToken.login_token()

    # 前置:
    def setUp(self):
        # 获取计划id
        # ---获取计划id
        select_plan_excel = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                                  "select_internship_plan")
        read_plan_excel = select_plan_excel.read_excel_location("C4")
        read_plan_excel_d = select_plan_excel.read_excel_location("E4")  # 读取params
        plan_list = SelectData(str(read_plan_excel), json.loads(read_plan_excel_d))
        plan_json = plan_list.select_list()
        self.planId = jsonpath(plan_json, "$..id")[0]  # 获取待审核计划id

    @data(*submit_plan_case)
    def test_submit_plan(self, case):
        url = conf.get("url", "url_ip") + case["url"]

        # 准备数据
        if "#ids#" in case["data"]:
            case["data"] = case["data"].replace("#ids#", str(self.planId))  # 计划id
        data = json.loads(case["data"])
        expected = json.loads(case["expected"])
        # 调用接口
        herders = {}
        herders["Authorization"] = self.token
        # 调用接口
        self.request = requests.request(url=url, method=case["method"], json=data, headers=herders)
        res = self.request.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.submit_plan.write_excel(row=case["id"] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            HandleLog.log.exception(e)
            raise e
        else:
            self.submit_plan.write_excel(row=case["id"] + 1, column=7, value="通过")
            # 将创建使用的数据写入到excel表格中
            self.submit_plan.write_excel(row=case["id"] + 1, column=9, value=case["data"])
            HandleLog.log.info("用例{},执行通过".format(case["title"]))


# ====================================================================
#                               审核成功计划
# ====================================================================
@ddt
class Test03auditInternshipPlan(unittest.TestCase):
    audit_plan = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                       "audit_internship_plan")  # 待审核的计划
    audit_plan_case = audit_plan.read_excel()

    @classmethod
    def setUpClass(cls):
        # 获取登陆token
        cls.token = LoginToken.login_token()

    def setUp(self):
        # 获取待审核计划的id
        select_audit_plan_excel = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                                        "select_internship_plan")
        read_plan_excel = select_audit_plan_excel.read_excel_location("C5")
        read_plan_excel_d = select_audit_plan_excel.read_excel_location("E5")  # 读取params
        plan_list = SelectData(str(read_plan_excel), json.loads(read_plan_excel_d))
        plan_json = plan_list.select_list()
        self.planId = jsonpath(plan_json, "$..id")[0]  # 获取待审核计划id

    # 一、准备用例数据
    @data(*audit_plan_case)
    def test_audit_plan(self, case):
        # 1。接口地址
        url = conf.get("url", "url_ip") + case["url"]
        #     2。请求参数
        if "#ids#" in case["data"]:
            case["data"] = case["data"].replace("#ids#", str(self.planId))  # 计划id
        data = json.loads(case["data"])
        expected = json.loads(case["expected"])
        headers = {}
        headers["Authorization"] = self.token
        self.request = requests.request(url=url, method=case["method"], json=data, headers=headers)
        res = self.request.json()
        print("用例入参:{}".format(data))
        print("预期结果:", expected)
        print("实际结果:", res)
        #     3。预期结果
        try:
            self.assertEqual(expected['msg'], res['msg'])
            self.assertEqual(expected['code'], res['code'])
        except AssertionError as e:
            self.audit_plan.write_excel(row=case["id"] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            HandleLog.log.exception(e)
            raise e
        else:
            self.audit_plan.write_excel(row=case["id"] + 1, column=7, value="通过")
            # 将创建使用的数据写入到excel表格中
            self.audit_plan.write_excel(row=case["id"] + 1, column=9, value=case["data"])
            HandleLog.log.info("用例{},执行通过".format(case["title"]))


# ====================================================================
#                               发布计划
# ====================================================================
@ddt
class Test04publishInternshipPlan(unittest.TestCase):
    publish_plan = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                         "publish_internship_plan")  # 发布计划Excel
    publish_plan_case = publish_plan.read_excel()

    @classmethod
    def setUpClass(cls):
        # 获取登陆token
        cls.token = LoginToken.login_token()

    def setUp(self):
        # ---获取计划id
        select_plan_excel = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                                  "select_internship_plan")
        read_plan_excel = select_plan_excel.read_excel_location("C2")
        read_plan_excel_d = select_plan_excel.read_excel_location("E2")  # 读取params
        plan_list = SelectData(str(read_plan_excel), json.loads(read_plan_excel_d))
        plan_json = plan_list.select_list()
        self.planId = jsonpath(plan_json, "$..id")[0]  # 获取待发布计划id

    # ====================================================================
    #                               发布计划
    # ====================================================================
    @data(*publish_plan_case)
    def test_publish_plan(self, case):
        """发布计划用例"""
        url = conf.get("url", "url_ip") + case["url"]

        # 准备数据
        if "#ids#" in case["data"]:
            case["data"] = case["data"].replace("#ids#", str(self.planId))  # 计划id
        data = json.loads(case["data"])
        expected = json.loads(case["expected"])
        # 调用接口
        herders = {}
        herders["Authorization"] = self.token
        # 调用接口
        self.request = requests.request(url=url, method=case["method"], json=data, headers=herders)
        res = self.request.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.publish_plan.write_excel(row=case["id"] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            HandleLog.log.exception(e)
            raise e
        else:
            self.publish_plan.write_excel(row=case["id"] + 1, column=7, value="通过")
            # 将创建使用的数据写入到excel表格中
            self.publish_plan.write_excel(row=case["id"] + 1, column=9, value=case["data"])
            HandleLog.log.info("用例{},执行通过".format(case["title"]))


# ---------------------------------------------------------------2023年10月24修改,删除掉项目相关的功能
'''
# ==================实习项目相关的用例==================
# ====================================================================
#                               新增项目
# ====================================================================
@ddt
class Test03addInternshipProject(unittest.TestCase):
    add_project = Excel(os.path.join(Internship_path, "test_05_internship_project.xlsx"),
                        "add_internship_project")  # 新增项目Excel
    add_project_case = add_project.read_excel()

    @classmethod
    def setUpClass(cls):
        # 获取登陆token
        cls.token = LoginToken.login_token()
        # ---获取已经发布的计划id
        select_plan_excel = Excel(os.path.join(Internship_path, "test_04_internship_plan.xlsx"),
                                  "select_internship_plan")
        read_plan_excel = select_plan_excel.read_excel_location("C3")
        plan_list = SelectData(str(read_plan_excel))
        plan_json = plan_list.select_list()
        cls.planId = jsonpath(plan_json, "$..id")[0]  # 获取已经发布的计划id

        # ---获取教师id
        select_teacher_excel = Excel(os.path.join(TeachingAffairs_path, "test_03_teacher.xlsx"),
                                     "select_teacher")
        read_teacher_excel = select_teacher_excel.read_excel_location("C2")
        teacher_list = SelectData(str(read_teacher_excel))
        teacher_json = teacher_list.select_list()
        cls.teacherId = jsonpath(teacher_json, "$..id")[0]  # 获取教师id

        # ---获取学生id
        select_student_excel = Excel(os.path.join(TeachingAffairs_path, "test_03_student.xlsx"),
                                     "select_student")
        read_student_excel = select_student_excel.read_excel_location("C2")
        student_list = SelectData(str(read_student_excel))
        student_json = student_list.select_list()
        cls.studentId = jsonpath(student_json, "$..id")[0]  # 获取学生id

    def setUp(self):
        """单条用例执行前执行的函数"""
        new_time = time.strftime("%Y%m%d_%H:%M:%S")
        # 随机计划名称
        self.name = "py自动化项目" + new_time

    # ====================================================================
    #                               新增项目
    # ====================================================================
    @data(*add_project_case)
    # @unittest.skipUnless(False, "跳过")
    def test_add_project(self, case):
        """新增项目用例"""
        url = conf.get("url", "url_ip") + case["url"]
        # 准备数据
        if "#name#" in case["data"]:
            case["data"] = case["data"].replace("#name#", str(self.name + "_" + str(case["id"])))  # 名称
        if "#planid#" in case["data"]:
            case["data"] = case["data"].replace("#planid#", str(self.planId))  # 已经发布的计划id
        if "#teacherId#" in case["data"]:
            case["data"] = case["data"].replace("#teacherId#", str(self.teacherId))  # 教师id
        if "#students#" in case["data"]:
            case["data"] = case["data"].replace("#students#", str(self.studentId))  # 学生id
        # 准备数据
        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_project.write_excel(row=case["id"] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            HandleLog.log.exception(e)
            raise e
        else:
            self.add_project.write_excel(row=case["id"] + 1, column=7, value="通过")
            # 将创建使用的数据写入到excel表格中
            self.add_project.write_excel(row=case["id"] + 1, column=9, value=case["data"])
            HandleLog.log.info("用例{},执行通过".format(case["title"]))


# ====================================================================
#                               发布项目
# ====================================================================
@ddt
class Test04publishInternshipProject(unittest.TestCase):
    publish_project = Excel(os.path.join(Internship_path, "test_05_internship_project.xlsx"),
                            "publish_internship_project")  # 发布项目Excel
    publish_project_case = publish_project.read_excel()

    @classmethod
    def setUpClass(cls):
        # 获取登陆token
        cls.token = LoginToken.login_token()

    def setUp(self):
        # ---获取项目id
        select_project_excel = Excel(os.path.join(Internship_path, "test_05_internship_project.xlsx"),
                                     "select_internship_project")
        read_project_excel = select_project_excel.read_excel_location("C2")
        read_project_excel_d = select_project_excel.read_excel_location("E2")  # 读取params
        project_list = SelectData(str(read_project_excel), json.loads(read_project_excel_d))
        project_json = project_list.select_list()
        self.projectId = jsonpath(project_json, "$..id")[0]  # 获取待发布项目id

    # ====================================================================
    #                               发布项目
    # ====================================================================
    @data(*publish_project_case)
    def test_publish_plan(self, case):
        """发布项目用例"""
        url = conf.get("url", "url_ip") + case["url"]

        # 准备数据
        if "#ids#" in case["data"]:
            case["data"] = case["data"].replace("#ids#", str(self.projectId))  # 项目id
        data = json.loads(case["data"])
        expected = json.loads(case["expected"])
        # 调用接口
        herders = {}
        herders["Authorization"] = self.token
        # 调用接口
        self.request = requests.request(url=url, method=case["method"], json=data, headers=herders)
        requests.request(url=url, method=case["method"], json=data, headers=herders)
        res = self.request.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.publish_project.write_excel(row=case["id"] + 1, column=7, value="不通过")
            HandleLog.log.error("用例标题{},不通过".format(case['title']))
            HandleLog.log.exception(e)
            raise e
        else:
            self.publish_project.write_excel(row=case["id"] + 1, column=7, value="通过")
            # 将创建使用的数据写入到excel表格中
            self.publish_project.write_excel(row=case["id"] + 1, column=9, value=case["data"])
            HandleLog.log.info("用例{},执行通过".format(case["title"]))
'''
#
# if __name__ == '__main__':
#     Test01addInternshipPlan.test_add_plan()