pytest 参数化和allure注解(持续更新)

前提条件:我的用例是写在xlsx里面的。(接口和测试读取方法一样)

 

首先,读取xlsx用例文件

import xlrd,json

def getCase(filepath,index):
    try:
        file = xlrd.open_workbook(filepath)
        me = file.sheets()[index]
        nrows = me.nrows
        listdata = []
        for i in range(1, nrows):
            dict_canshu = {}
            if me.cell(i, 1).value=='Y':
                dict_canshu['id'] = me.cell(i, 0).value
                dict_canshu.setdefault('active',me.cell(i, 1).value)
                dict_canshu.setdefault('apiname', me.cell(i, 2).value)
                dict_canshu.setdefault('apihost',me.cell(i,3).value)
                dict_canshu.setdefault('url',me.cell(i, 4).value)
                dict_canshu.setdefault('method', me.cell(i, 5).value)
                dict_canshu.setdefault('redata', me.cell(i, 6).value)
                dict_canshu.setdefault('assert', me.cell(i, 7).value)
                dict_canshu.setdefault('sql', me.cell(i, 8).value)
                dict_canshu.setdefault('desc', me.cell(i, 9).value)
                listdata.append(dict_canshu)
        return listdata
    except Exception as e:
        log.error('获取测试用例数据失败,原因:%s'%e)

进入正题

import pytest,allure,Path,json
from interface.data.readCase import getCase
from interface.case.BaseThread import BaseThread
from common.httpHandle import httpHandle

class Test_api_case(object):
    @classmethod
    def setup_class(cls):
        cls.http=httpHandle()
        cls.req=None
        cls.result = None
    @pytest.mark.parametrize(('casedata'),getCase(Path.apicasPath(),0))
    def test_case(self,casedata):
        allure.dynamic.title(casedata['apiname'])
        allure.dynamic.description(casedata['apiname'])
        self.req=casedata
        self.thread = BaseThread(casedata,self.http)
        self.thread.start()

    def teardown(cls):
        cls.result = cls.thread.join()
        print('请求参数:', cls.req)
        print('响应结果:', cls.result)
        cls.j=json.loads(cls.result)
        cls.code=cls.j['statusCode']
        assert  cls.code==200

有不明白的,加群QQ 833077481交流

 

你可能感兴趣的:(自动化测试)