MOOC(7)- case依赖、读取json配置文件进行多个接口请求-完整的测试类,含依赖测试(15)

时间:2022-12-01 22:34:26

ddt、依赖测试、断言、测试数据写回

# -*- coding: utf-8 -*-
# @Time : 2020/2/12 23:07
# @File : test_class_15.py
# @Author: Hero Liu
# 相当于案例中的runmethod.py
import unittest
from day_20200208_mooc.base.interface_request_class_5 import RunMain
from day_20200208_mooc.tools.do_excel_openpyxl_14 import DoExcel
from ddt import ddt, data, unpack
from day_20200208_mooc.tools.do_mock_18 import mock_test
from day_20200208_mooc.tools.data_depend_17 import DependData

test_data = DoExcel().get_data("../data/test_data.xlsx", "case")

@ddt
class TestRequest(unittest.TestCase):
    def setUp(self):
        print("case starting...")

    def tearDown(self):
        print("case end.")

    @data(*test_data)
    @unpack
    def test_api(self, case_id, module_title, url, method, header,
                 case_depend, case_depend_data, depend_data_belong_new_key,
                 data, expected):
        print("正在执行第{}条用例:{}".format(case_id, module_title))
        print("请求数据是:", data)
        # 如果存在依赖case_id, 默认就有依赖数据和依赖字段
        # 获取依赖case数据,执行依赖测试,根据依赖测试响应结果返回依赖字段,
        # 根据依赖字段发起对当前case发起请求
        if case_depend:
            r = DependData(case_depend, case_id, "../data/test_data.xlsx", "case")
            res = r.run_present_case()
            print("单独的响应数据:", res)
        else:
            res = mock_test(RunMain().run_main, url, method, data, data, header)
        print("响应数据是1:", res)
        test_result = None
        try:
            print("data[timestamp]: ", data["timestamp"])
            print("res[timestamp]: ", res["timestamp"])
            self.assertEqual(data["timestamp"], res["timestamp"])
            test_result = "PASS"
        except AssertionError as e:
            test_result = "FAILED"
            print("there is an error: {}".format(e))
            raise e
        finally:
            DoExcel().write_back("../data/test_data.xlsx", "case", case_id+1, 12, str(res))
            DoExcel().write_back("../data/test_data.xlsx", "case", case_id+1, 13, test_result)