python使用pytest接口自动化测试的使用

class Test_zhu(object):

    #类初始化,此处在测试执行中,全局只会执行一次(读取配置文件)

    @classmethod

    def setup_class(self):

        self.text_data = config_txt()

   #方法初始化,此处在测试执行中,每条用例都会先执行一遍

    def setup_method(self):

        time.sleep(0.4)

        self.time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

   #request接口请求的逻辑

    def request_id(self, GATEWAY_URL, http_body):

        HTTP_BODY = eval(http_body)

        TIME_STAMP = str(int((time.time() + 5* 1000))

        zhuCE = uuid.uuid1().hex

        msg = self.text_data['zhuLY_ID'+ "\n" + TIME_STAMP + "\n" + zhuCE + "\n" + self.text_data['zhu_KEY'+ "\n" + \

              self.text_data["HTTP_URI"+ "\n" + json.dumps(HTTP_BODY)

        HTTP_HEADER = {

            "zhu_ID"self.text_data['zhu_ID'],

            "zhuLY_ID"self.text_data['zhuLY_ID'],

            "zhuCE": zhuCE,

            "zhu_KEY"self.text_data['zhu_KEY'],

            "TIMESTAMP": TIME_STAMP,

            "SIGNATURE": hash_hmac(self.text_data['zhu_SECRET'], msg, hashlib.sha1)}

        print('**请求参数:{}.'.format(HTTP_BODY))

        response_one = requests.post(GATEWAY_URL, json=HTTP_BODY, headers=HTTP_HEADER, verify=True)

        print('**响应结果:{}.'.format(response_one.text))

        return eval(response_one.text)

    #parametrize为数据驱动,此处获取了excle用例里的数据;apitest定个标签,后续冒烟啥的可以选择性的跑(暂时无用);

    #excel_case(case_id="all")说明,all为全部用例执行或者指定用例执行case_0001

    @pytest.mark.parametrize('case_id,case_name, case_body, case_assert', excel_case(case_id="all"))

    @pytest.mark.apitest

    def test_zhu_case(self, case_id, case_name, case_body, case_assert):

        print("**\n执行编号:" + case_id + "\n**用例名称:" + case_name)

        case_assert_a = json.loads(case_assert)

        alertText = self.request_id(self.text_data['GATEWAY_URL'], case_body)

        retcode = case_assert_a.get('code'if case_assert_a.get('retcode'== None else case_assert_a.get('retcode')

        alertText_a = alertText.get('code'if alertText.get('retcode'== None else alertText.get('retcode')

        #此处为断言,判断用例通过还是不通过

        assert alertText_a == retcode

上一篇:通过docker容器安装zabbix6.4.12图文详解(监控服务器docker容器)


下一篇:docker常用指令