接口框架项目实战-pytest(六)csv数据驱动

csv 数据驱动 为了解决数据量大 导致yaml文件重复太多

yaml_util.py

bash 复制代码
import os

import jsonpath
import yaml

from pytestdemo.common.base_util import get_path
from pytestdemo.common.csv_util import analysis_parameters


def read_config_file(one_node,two_node):
    with open(f'{get_path()}/common/config.yml',encoding="utf-8") as f:
        value=yaml.load(f,yaml.FullLoader)
        # print(value[one_node][two_node])
        return value[one_node][two_node]
def read_extract_file(node_name):
    with open(f'{get_path()}/common/extract.yml',encoding="utf-8") as f:
        value=yaml.load(f,yaml.FullLoader)
        return value[node_name]
def write_extract_file(data):
    with open(f'{get_path()}/common/extract.yml',encoding="utf-8",mode="a") as f:
        yaml.dump(data,f,allow_unicode=True)

def clean_extract_file():
    with open(f'{get_path()}/common/extract.yml',encoding="utf-8",mode="w") as f:
       f.truncate()
def read_testcase_yaml(file_path):
    print(f'{get_path()}{file_path}')
    with open(f'{get_path()}{file_path}',encoding="utf-8") as f:
        value = yaml.load(f, yaml.FullLoader)
        if len(value)==1:
            if jsonpath.jsonpath(value,"$..parameters"):
                return analysis_parameters(value)
        return value
if __name__ == '__main__':
    # print (read_config_file("base","base_php_url"))
    print(read_testcase_yaml("\\testcases\\get_token.yml"))

csv_util.py

bash 复制代码
import csv
import json

from pytestdemo.common.base_util import get_path


def read_csv(file_path):
    data_list=[]
    with open(f'{get_path()}{file_path}',encoding="utf-8") as f:
        reader = csv.reader(f)
        for r in reader:
            data_list.append(r)
    return data_list
def analysis_parameters(case_data):
    keys=dict(*case_data).keys()
    case_data_str=json.dumps(case_data)
    list=[]
    if "parameters" in keys:
        for p_key,p_value in dict(*case_data)["parameters"].items():
            p_list=read_csv(p_value)
            for r in range(1,len(p_list)):
                temp_case_data_str=case_data_str
                for c in range(0,len(p_list[r])):
                    temp_case_data_str=temp_case_data_str.replace("$csv{"+p_list[0][c]+"}",p_list[r][c])
                list.extend(json.loads(temp_case_data_str))
    return list
# if __name__ == '__main__':
#     print(read_csv("/testcases/data/get_token.csv"))
    # data=read_testcase_yaml("/testcases/get_token.yml")
    # analysis_parameters(data)

conftest.py

bash 复制代码
@pytest.fixture(scope="session",autouse=True)
def clean_extract():
    clean_extract_file()

test_demo4.py

bash 复制代码
import random

import pytest

from pytestdemo.common.requests_util import RequestsUtil
from pytestdemo.common.yaml_util import write_extract_file, read_testcase_yaml


class Test4Api:
    access_token=""
    @pytest.mark.parametrize("data",read_testcase_yaml("/testcases/get_token.yml"))
    def test_get_token(self,data):
        RequestsUtil("base","base_wx_url").analysis_yml_send_reqeust(data)
      
    @pytest.mark.parametrize("data", read_testcase_yaml("/testcases/get_tags.yaml"))
    def test_get_tags(self,data):
        RequestsUtil("base", "base_wx_url").analysis_yml_send_reqeust(data)
    

get_token.yml

bash 复制代码
- name: $csv{name}
  parameters:
    data_file: /testcases/data/get_token.csv
  request:
    method: get
    url: /cgi-bin/token
    params:
      grant_type: $csv{grant_type}
      appid: $csv{appid}
      secret: $csv{secret}
  extract:
      access_token: '"access_token":"(.*?)"'
  validate:
      - equals: {status_code: 200}
      - contains: $csv{assert_str}

get_token.csv

bash 复制代码
name,grant_type,appid,secret,assert_str
"获取鉴权码","client_credential","wx6b11b3efd1cdc2901","106a9c6157c4db5f6029918738f9529d2","access_token"
"appid必填检验","client_credential","","106a9c6157c4db5f6029918738f9529d2","errcode"

get_tags.yaml

bash 复制代码
- name: 获取标签
  request:
    method: get
    url: /cgi-bin/tags/get?access_token={{access_token}}
  validate:
      equals: {status_code: 200}
- name: 获取标签222
  request:
    method: get
    url: /cgi-bin/tags/get?access_token={{access_token}}
  validate:
      equals: {status_code: 200}
相关推荐
一半烟火以谋生6 小时前
Python + Pytest + Allure 自动化测试报告教程
开发语言·python·pytest
一半烟火以谋生1 天前
pytest conftest.py 使用教程
pytest
我的xiaodoujiao2 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 19--测试框架Pytest基础 3--前后置操作应用
python·学习·测试工具·pytest
鱼鱼说测试2 天前
pytest+yaml+allure接口自动化测试框架
pytest
ThreeAu.4 天前
pytest 实战:用例管理、插件技巧、断言详解
python·单元测试·pytest·测试开发工程师
我的xiaodoujiao5 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 18--测试框架Pytest基础 2--插件和参数化
python·学习·测试工具·pytest
小小测试开发5 天前
pytest 库用法示例:Python 测试框架的高效实践
开发语言·python·pytest
know__ledge6 天前
Pytest+requests进行接口自动化测试8.0(Allure进阶 + 文件上传接口 + 单接口多用例)
pytest
川石课堂软件测试6 天前
CSS中常用的几种定位。
开发语言·css·python·网络协议·http·html·pytest
啊森要自信7 天前
【GUI自动化测试】Python 自动化测试框架 pytest 全面指南:基础语法、核心特性(参数化 / Fixture)及项目实操
开发语言·python·ui·单元测试·pytest