pyton用pytest框架怎么用json文件参数化,麻烦大佬看一下,不胜感激(如截图)

image
把read_json作为类方法,然后在参数化的时候进行类方法调用试试呢?



参数化函数加self和不加都报错

上面定义read_json的函数加self了吗

class InterfaceTest(unittest.TestCase):
@classmethod
def setUpClass(self):
pass
def read_json():
data_list =
# 创建空的列表为后面参数化所需格式准备
# test_data =
# # 获取json文件的绝对路径
# data_path = os.path.join(self.file_path, json_file)
# 打开json文件,模式为只读
with open(
r"D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\automated_main\view\ui_automation\ui_test_task\extend\test_data_list.json") as f:
# 用json.load() 方法,来处理文件,就是json转dict
json_data = json.load(f)
data = list(json_data.values())
# print(data[0])
# print(data[1])
# print(data[2])
# print(data[3])
for i in range(0, len(json_data.values())):
data_list.append(data[i])
# print(read_json())
return data_list

@pytest.mark.parametrize(
    'element_input,x_coordinates,y_coordinates,element_operation,ui_elements_id,waiting_time,ui_task_id,ui_result_id,ui_test_case_name,ui_test_script',
    read_json())
def test_run_cases(self, element_input, x_coordinates, y_coordinates, element_operation, ui_elements_id,
                   waiting_time, ui_task_id, ui_result_id, ui_test_case_name, ui_test_script):

    print(element_input,x_coordinates,y_coordinates,element_operation,ui_elements_id,waiting_time,ui_task_id,ui_result_id,ui_test_case_name,ui_test_script)

if name == ‘main’:
pytest.main([’-s’, ‘-q’])

{
“登录大数据运维平台”: [{
“ui_result_id”: 138,
“ui_elements_id”: 1,
“element_operation”: “1”,
“element_input”: “//”,
“waiting_time”: “1”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 1,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 2,
“element_operation”: “2”,
“element_input”: “18886056204”,
“waiting_time”: “3”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 2,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 3,
“element_operation”: “2”,
“element_input”: “ZYY19930207”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 3,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 4,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 4,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}],
“大屏个人中心进入页面”: [{
“ui_result_id”: 138,
“ui_elements_id”: 5,
“element_operation”: “19”,
“element_input”: “”,
“waiting_time”: “8”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “大屏个人中心进入页面”,
“ui_task_id”: 1,
“case_steps”: 1,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 6,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “5”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “大屏个人中心进入页面”,
“ui_task_id”: 1,
“case_steps”: 2,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}],
“资产管理页面”: ,
“运维管理”: [{
“ui_result_id”: 138,
“ui_elements_id”: 8,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “1”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 1,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 9,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 2,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 10,
“element_operation”: “21”,
“element_input”: “”,
“waiting_time”: “5”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 3,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 11,
“element_operation”: “2”,
“element_input”: “smartbox/gy/by/042”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 4,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 12,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “10”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 5,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 13,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “15”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 6,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 15,
“element_operation”: “6”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 7,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 14,
“element_operation”: “8”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 8,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 16,
“element_operation”: “22”,
“element_input”: “更多>”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 9,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}]
}

[{
‘ui_result_id’: 138,
‘ui_elements_id’: 1,
‘element_operation’: ‘1’,
‘element_input’: ‘//’,
‘waiting_time’: ‘1’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 1,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}, {
‘ui_result_id’: 138,
‘ui_elements_id’: 2,
‘element_operation’: ‘2’,
‘element_input’: ‘18886056204’,
‘waiting_time’: ‘3’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 2,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}, {
‘ui_result_id’: 138,
‘ui_elements_id’: 3,
‘element_operation’: ‘2’,
‘element_input’: ‘ZYY19930207’,
‘waiting_time’: ‘2’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 3,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}, {
‘ui_result_id’: 138,
‘ui_elements_id’: 4,
‘element_operation’: ‘3’,
‘element_input’: ‘’,
‘waiting_time’: ‘2’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 4,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}]

这是代码和josn文件

image

你这里因为是在类中创建的,需要实例self

我加了,下面参数哪里报错了,我就去掉了

你可以把read_json弄成类方法试试,用@classmethod装饰器


还是报错,上

昨天搞了大半天还是不行 :crazy_face:上面的代码帮我看一下呗,谢谢

@classmethod
def read_json(cls):
data_list = []
# 创建空的列表为后面参数化所需格式准备
# test_data = 
# # 获取json文件的绝对路径
# data_path = os.path.join(self.file_path, json_file)
# 打开json文件,模式为只读
with open(
r"D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\automated_main\view\ui_automation\ui_test_task\extend\test_data_list.json") as f:
# 用json.load() 方法,来处理文件,就是json转dict
json_data = json.load(f)
data = list(json_data.values())
for i in range(0, len(json_data.values())):
data_list.append(data[i])
return data_list

然后底下参数化的调用改成InterfaceTest.read_json()。这样试试看,注意缩进

不行呢 :cry:

我这个改动有点大,仅供参考。

import json
import unittest

import pytest


def read_json_data():
    data_list = []
    # 创建空的列表为后面参数化所需格式准备
    # test_data =
    # # 获取json文件的绝对路径
    # data_path = os.path.join(self.file_path, json_file)
    # 打开json文件,模式为只读
    with open(r".\test_data_list.json", encoding='utf-8') as f:
        # 用json.load() 方法,来处理文件,就是json转dict
        json_data = json.load(f)
        # data = json_data.values()
        data = list(json_data.values())
        for i in data[0]:
            data_list.append(list(dict(i).values()))
        # print(data)
        return data_list


# class TestInterface(unittest.TestCase):
class TestInterface:
    # @classmethod
    # def setUpClass(self):
    #     pass
    @pytest.mark.parametrize(
            "ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script", read_json_data())
    def test_run_cases(self, ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script):
        print(f"Data:{ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script}")



if __name__ == '__main__':
    pytest.main(['-s', '-q'])

Json数据我只截取了部分。

{
"登录大数据运维平台": [{
    "ui_result_id": 138,
    "ui_elements_id": 1,
    "element_operation": "1",
    "element_input": "//",
    "waiting_time": "1",
    "x_coordinates": "",
    "y_coordinates": "",
    "ui_test_case_name": "登录大数据运维平台",
    "ui_task_id": 1,
    "case_steps": 1,
    "ui_test_script": "D:\\zyy_file\\auto_platme\\AutomatedTestPlatform\\AutomatedTestPlatform\\web_log/test_log\\2022-07-27-18-16-44.py"
}, {
    "ui_result_id": 138,
    "ui_elements_id": 2,
    "element_operation": "2",
    "element_input": "18886056204",
    "waiting_time": "3",
    "x_coordinates": "",
    "y_coordinates": "",
    "ui_test_case_name": "登录大数据运维平台",
    "ui_task_id": 1,
    "case_steps": 2,
    "ui_test_script": "D:\\zyy_file\\auto_platme\\AutomatedTestPlatform\\AutomatedTestPlatform\\web_log/test_log\\2022-07-27-18-16-44.py"
}]
}

你这样写还是报错了

麻烦贴下当前的代码。

import json
import pytest

def read_json_data():
data_list =
with open(r"D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\automated_main\view\ui_automation\ui_test_task\extend\test_data_list.json", encoding=‘utf-8’) as f:
# 用json.load() 方法,来处理文件,就是json转dict
json_data = json.load(f)
# data = json_data.values()
data = list(json_data.values())
for i in data[0]:
data_list.append(list(dict(i).values()))
# print(data)
return data_list

class TestInterface:

@pytest.mark.parametrize(
        "ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script", read_json_data())
def test_run_cases(self, ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script):
    print(f"Data:{ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script}")

if name == ‘main’:
pytest.main([’-s’, ‘-q’])

文件:
{
“登录大数据运维平台”: [{
“ui_result_id”: 138,
“ui_elements_id”: 1,
“element_operation”: “1”,
“element_input”: “//”,
“waiting_time”: “1”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 1,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 2,
“element_operation”: “2”,
“element_input”: “18886056204”,
“waiting_time”: “3”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 2,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 3,
“element_operation”: “2”,
“element_input”: “ZYY19930207”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 3,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 4,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “登录大数据运维平台”,
“ui_task_id”: 1,
“case_steps”: 4,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}],
“大屏个人中心进入页面”: [{
“ui_result_id”: 138,
“ui_elements_id”: 5,
“element_operation”: “19”,
“element_input”: “”,
“waiting_time”: “8”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “大屏个人中心进入页面”,
“ui_task_id”: 1,
“case_steps”: 1,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 6,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “5”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “大屏个人中心进入页面”,
“ui_task_id”: 1,
“case_steps”: 2,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}],
“资产管理页面”: ,
“运维管理”: [{
“ui_result_id”: 138,
“ui_elements_id”: 8,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “1”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 1,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 9,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 2,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 10,
“element_operation”: “21”,
“element_input”: “”,
“waiting_time”: “5”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 3,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 11,
“element_operation”: “2”,
“element_input”: “smartbox/gy/by/042”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 4,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 12,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “10”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 5,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 13,
“element_operation”: “3”,
“element_input”: “”,
“waiting_time”: “15”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 6,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 15,
“element_operation”: “6”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 7,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 14,
“element_operation”: “8”,
“element_input”: “”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 8,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}, {
“ui_result_id”: 138,
“ui_elements_id”: 16,
“element_operation”: “22”,
“element_input”: “更多>”,
“waiting_time”: “2”,
“x_coordinates”: “”,
“y_coordinates”: “”,
“ui_test_case_name”: “运维管理”,
“ui_task_id”: 1,
“case_steps”: 9,
“ui_test_script”: “D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py”
}]
}

[{
‘ui_result_id’: 138,
‘ui_elements_id’: 1,
‘element_operation’: ‘1’,
‘element_input’: ‘//’,
‘waiting_time’: ‘1’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 1,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}, {
‘ui_result_id’: 138,
‘ui_elements_id’: 2,
‘element_operation’: ‘2’,
‘element_input’: ‘18886056204’,
‘waiting_time’: ‘3’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 2,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}, {
‘ui_result_id’: 138,
‘ui_elements_id’: 3,
‘element_operation’: ‘2’,
‘element_input’: ‘ZYY19930207’,
‘waiting_time’: ‘2’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 3,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}, {
‘ui_result_id’: 138,
‘ui_elements_id’: 4,
‘element_operation’: ‘3’,
‘element_input’: ‘’,
‘waiting_time’: ‘2’,
‘x_coordinates’: ‘’,
‘y_coordinates’: ‘’,
‘ui_test_case_name’: ‘登录大数据运维平台’,
‘ui_task_id’: 1,
‘case_steps’: 4,
‘ui_test_script’: ‘D:\zyy_file\auto_platme\AutomatedTestPlatform\AutomatedTestPlatform\web_log/test_log\2022-07-27-18-16-44.py’
}]

把你的read_json()方法写类外进行调用

import json

import pytest


def read_json():
    data_list = []
    # 创建空的列表为后面参数化所需格式准备
    # test_data =
    # # 获取json文件的绝对路径
    # data_path = os.path.join(self.file_path, json_file)
    # 打开json文件,模式为只读
    with open("1.json") as f:
        # 用json.load() 方法,来处理文件,就是json转dict
        json_data = json.load(f)
        data = list(json_data.values())
    # print(data[0])
    # print(data[1])
    # print(data[2])
    # print(data[3])
    for i in range(0, len(json_data.values())):
        data_list.append(data[i])
    # print(read_json())
    return data_list

class InterfaceTest():
    @classmethod
    def setUpClass(self):
        pass

    @pytest.mark.parametrize(
        "ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script",
        read_json())
    def test_run_cases(self, ui_result_id, ui_elements_id, element_operation, element_input, waiting_time,
                       x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script):
        print(
            f"Data:{ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script}")

你本地能行吗?我的按照你的做了还是报错

这是你自己对读取后的数据没有进行处理,仔细看一下data_list读取的list是不是包含了两层,去掉一层就可以了

import json

import pytest


def read_json():
    data_list = []
    with open("../1.json",encoding='utf-8') as f:
        data_json = json.loads(f.read())
        print(data_json, type(data_json))
        for i in data_json:
            data_list.append(data_json[i])
    print("____")
    print(data_list[0])
    print("____")
    return data_list[0]

class TestCase():
    @classmethod
    def setUpClass(self):
        read_json()

    @pytest.mark.parametrize(
        "ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script",
        read_json())
    def test_run_cases(self, ui_result_id, ui_elements_id, element_operation, element_input, waiting_time,
                       x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script):
        print(
            f"Data:{ui_result_id, ui_elements_id, element_operation, element_input, waiting_time, x_coordinates, y_coordinates, ui_test_case_name, ui_task_id, case_steps, ui_test_script}")

关闭