ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

requests+xls+alluer+pytest 接口自动化[数据驱动]形式

2022-02-07 16:32:36  阅读:188  来源: 互联网

标签:__ alluer data param 用例 pytest requests xls


 1.编写读取xls函数方法。file_utils.py

# 读取xls
def pares_xls_file(filepath, sheetname):
    xls_sheet = xlrd.open_workbook(filepath)  # r"D:\untitled\API_Project\data\驱动原件.xls"
    work_sheet = xls_sheet.sheet_by_name(sheetname)  # 'JH21207-134'
    row = work_sheet.nrows  # 总行数
    # 遍历所有xls数据
    test_data = []
    for x in range(1, row):
        testcase_data = []
        for y in range(0, 8):
            testcase_data.append(work_sheet.cell(x, y).value)
        test_data.append(testcase_data)
    return test_data

2.调用遍历方法,使用@pytest.mark.parametrize进行参数化,然后根据表格状态判断用例是否执行,最后再断言返回信息。test_excel.py

# 用例层
class TestLoginCase(object):
    # 调用遍历用例方法
    test_data = pares_xls_file("D:/untitled/API_Project/data/驱动原件.xls", 'JH21207-134')
    # allure 中提取字段展示为title
    @allure.title("{CASE_NAME}--用例状态:{execute}")
    @pytest.mark.parametrize('CASE_ID,CASE_NAME,execute,url,method,topic_data,code,result', test_data)
    def test_001(self, CASE_ID, CASE_NAME, execute, url, method, topic_data, code, result):
        """
        :param CASE_ID: 用例ID
        :param CASE_NAME: 用例名称
        :param execute: 用例状态
        :param url: 请求URL
        :param method: 请求方式
        :param topic_data: 入参
        :param code:出参
        :param result:执行结果
        """
        if execute == "有效":
            print("此条用例选择的是{}执行接口测试。".format(execute))
            if method == 'POST':
                headers = {"Content-Type": "application/json"}
                res = requests.post(url, headers=headers, params=json.loads(topic_data))
                assert res.status_code == 200
                myLogger.info(('返回结果是{}'.format(res.text)))
            else:
                headers = {"Content-Type": "application/json"}
                res = requests.get(url, headers=headers, params=json.loads(topic_data))
                assert res.status_code == 200
                myLogger.info(('返回结果是{}'.format(res.text)))
        else:
            print("此条用例选择的是{}因此不执行。".format(execute))


if __name__ == "__main__":
    pytest.main()

3.pytest.ini配置文件

[pytest]

addopts = -sv --alluredir report --clean-alluredir

testpaths = case/text_excel.py

pytest_classes = Test*

pytest_functions = test_*

4.执行用例层 run_api.py

import os
from shutil import copy
import pytest


if __name__ == '__main__':
    pytest.main()
    os.system('allure generate report --clean')
    os.system('allure serve report')

标签:__,alluer,data,param,用例,pytest,requests,xls
来源: https://blog.csdn.net/weixin_45056456/article/details/122810518

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有