Sarmad Nawaz
Sarmad Nawaz

Reputation: 1

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. 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).

So in order to create these using static data, I implemented @pytest.mark.parameterized as follows. 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:-

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. 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.

filename = test_data.json

The json data in test_data.json looks as follows

[
  {
    "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? Thanks!

Upvotes: 0

Views: 2603

Answers (1)

Lesiak
Lesiak

Reputation: 25956

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.

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. For example, to read Json from a string

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

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.

Upvotes: 1

Related Questions