简体   繁体   English

使用json文件实现参数化以创建多个测试迭代

[英]Implement parameterized using json file to create multiple test iterations

For a file test_data.json, I want to create the test iterations in python. 对于文件test_data.json,我想在python中创建测试迭代。 The data in this file only includes respective values to be passed to specific methods. 该文件中的数据仅包含要传递给特定方法的相应值。 Meanwhile, assertion part is being handled separately (json file data is not required here). 同时,断言部分将单独处理(此处不需要json文件数据)。

So in order to create these using static data, I implemented @pytest.mark.parameterized as follows. 因此,为了使用静态数据创建这些文件,我实现了@ pytest.mark.parameterized,如下所示。 This implementation is working perfectly fine as it created 5 test iterations as 1,2,3,4,5 and execution is being done without any errors:- 此实现工作得很好,因为它创建了5个测试迭代,分别为1,2,3,4,5,并且执行完成时没有任何错误:-

import pytest 

@pytest.mark.parametrize('key_value',
                         [
                             '1',
                             '2',
                             '3',
                             '4',
                             '5',
                         ]
                         )
def test_method(key_value):
     go_to_key(key_value)

Now given the data within json file I am using will be pulled in in realtime and could vary from time to time. 现在,鉴于我正在使用的json文件中的数据将被实时提取,并且可能会不时变化。 So I need to use parameterized in a way to read the json file and then build test iterations based on the key_values being pulled in. 所以我需要使用参数化的方式来读取json文件,然后根据要引入的key_values构建测试迭代。

filename = test_data.json 文件名= test_data.json

The json data in test_data.json looks as follows test_data.json中的json数据如下所示

[
  {
    "key": "1"
  },
  {
    "key": "2"
  },
  {
    "key": "3"
  },
  {
    "key": "4"
  },
  {
    "key": "5"
  }
]

While using parameterized, I came across following snippet but it still does not provide any clear implementation for test iterations:- 在使用参数化时,我遇到了以下代码段,但仍未为测试迭代提供任何清晰的实现:

@parameterized(
    param.explicit(*json.loads(line))
    for line in open("testcases.jsons")
)
def test_from_json_file(...):
    ...

Can someone kindly review and share any suggestions for implementation of creating test iterations using json file in above mentioned code context? 有人可以在上述代码上下文中友善地查看并分享有关使用json文件创建测试迭代的实现的任何建议吗? Thanks! 谢谢!

In @pytest.mark.parametrize , the values for different cases can come not only as a list literal, but can be a result of a method call. @pytest.mark.parametrize ,不同情况的值不仅可以作为列表文字使用,而且可以是方法调用的结果。

Thus: 从而:

@pytest.mark.parametrize('key_value', [
    '1',
    '2',
    '3'
])
def test_is_key_digit(key_value: str):
    assert key_value.isdigit()

Can be easily transformed into: 可以轻松转换为:

def load_test_cases():
    return ['1', '2', '3']


@pytest.mark.parametrize('key_value', load_test_cases())
def test_is_key_digit(key_value: str):
    assert key_value.isdigit()

load_test_cases can use any code to generate the values you need. load_test_cases可以使用任何代码来生成所需的值。 For example, to read Json from a string 例如,从字符串读取Json

def load_test_cases():
    return json.loads('["1", "2", "3"]')

As a side note, you write: 作为旁注,您可以编写:

the data within json file I am using will be pulled in in realtime and could vary from time to time 我正在使用的json文件中的数据将被实时提取,并且可能会不时变化

this sounds like a bad idea. 这听起来像个坏主意。 It is a good practice to make your tests reproducible, and making them rely on some file generated by an outside system is in clear opposition to that goal. 最好使测试具有可重复性,并使测试依赖于外部系统生成的某些文件显然与该目标相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM