You can use pytest_generate_tests hook for parametrizing with dynamic data.

First create a fixture that can be called by the test function to get the test data.

# in conftest.py
@pytest.fixture()
def test_data(request):
    return request.param

Now you can parametrize it so that it is called for each test data set:

#in conftest.py
def pytest_generate_tests(metafunc):
    testdata = get_test_data('test_data.json')
    metafunc.parametrize('test_data', testdata, indirect=True)

The testdata passed to the parametrize function has to be a list. So you need to modify the input data a bit before passing it. I modified the get_test_data function a bit.

#in conftest.py
def get_test_data(filename):
    folder_path = os.path.abspath(os.path.dirname(__file__))
    folder = os.path.join(folder_path, 'TestData')
    jsonfile = os.path.join(folder, filename)
    with open(jsonfile) as file:
        data = json.load(file)

    valid_data = [(item, 1) for item in data['valid_data']]
    invalid_data = [(item, 0) for item in data['invalid_data']]

    # data below is a list of tuples, with first element in the tuple being the 
    # arguments for the API call and second element specifies if this is a test 
    # from valid_data set or invalid_data. This can be used for putting in the
    # appropriate assert statements for the API response.
    data = valid_data + invalid_data

    return data

And now your test function could look like:

#in test_API.py
def test_(test_data):
    response = database_api.get_user_info(test_data[0])

    # Add appropriate asserts. 
    # test_data[1] == 1 means 200 response should have been received
    # test_data[1] == 0 means 400 response should have been received

Answer from yeniv on Stack Overflow
🌐
PyPI
pypi.org › project › pytest-json-report
pytest-json-report · PyPI
A pytest plugin to report test results as JSON files
      » pip install pytest-json-report
    
Published   Mar 15, 2022
Version   1.5.0
🌐
PyPI
pypi.org › project › pytest-json
pytest-json · PyPI
A formatted example of the jsonapi output can be found in example_jsonapi.json · Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request. Distributed under the terms of the MIT license, “pytest-json” is free and open source software
      » pip install pytest-json
    
Published   Jan 18, 2016
Version   0.4.0
🌐
GitHub
github.com › mattcl › pytest-json
GitHub - mattcl/pytest-json: WIP: generate json reports from pytest runs · GitHub
pytest-json is a plugin for py.test that generates JSON reports for test results
Starred by 15 users
Forked by 5 users
Languages   Python
🌐
Pytest with Eric
pytest-with-eric.com › pytest-best-practices › pytest-read-json
5 Easy Ways To Read JSON Input Data In Pytest | Pytest with Eric
January 22, 2026 - Here we’ve used the pathlib module to parse the file but you can also use the json module. The benefit here is we decouple the input from the test module, but you’ll run into test errors if the input file is unavailable. Another way (and one I’m a big fan of) is to provide your input data as a Pytest fixture.
🌐
GitHub
github.com › numirias › pytest-json-report
GitHub - numirias/pytest-json-report: 🗒️ A pytest plugin to report test results as JSON
This pytest plugin creates test reports as JSON.
Starred by 153 users
Forked by 45 users
Languages   Python 100.0% | Python 100.0%
Top answer
1 of 2
5

You can use pytest_generate_tests hook for parametrizing with dynamic data.

First create a fixture that can be called by the test function to get the test data.

# in conftest.py
@pytest.fixture()
def test_data(request):
    return request.param

Now you can parametrize it so that it is called for each test data set:

#in conftest.py
def pytest_generate_tests(metafunc):
    testdata = get_test_data('test_data.json')
    metafunc.parametrize('test_data', testdata, indirect=True)

The testdata passed to the parametrize function has to be a list. So you need to modify the input data a bit before passing it. I modified the get_test_data function a bit.

#in conftest.py
def get_test_data(filename):
    folder_path = os.path.abspath(os.path.dirname(__file__))
    folder = os.path.join(folder_path, 'TestData')
    jsonfile = os.path.join(folder, filename)
    with open(jsonfile) as file:
        data = json.load(file)

    valid_data = [(item, 1) for item in data['valid_data']]
    invalid_data = [(item, 0) for item in data['invalid_data']]

    # data below is a list of tuples, with first element in the tuple being the 
    # arguments for the API call and second element specifies if this is a test 
    # from valid_data set or invalid_data. This can be used for putting in the
    # appropriate assert statements for the API response.
    data = valid_data + invalid_data

    return data

And now your test function could look like:

#in test_API.py
def test_(test_data):
    response = database_api.get_user_info(test_data[0])

    # Add appropriate asserts. 
    # test_data[1] == 1 means 200 response should have been received
    # test_data[1] == 0 means 400 response should have been received

2 of 2
1

I just wrote a package called parametrize_from_file to solve exactly this problem. Here's how it would work for this example:

import parametrize_from_file

# If the JSON file has the same base name as the test module (e.g. "test_api.py"
# and "test_api.json"), this parameter isn't needed.
path_to_json_file = ...

@parametrize_from_file(path_to_json_file, 'valid_data')
def test_valid_data(id, name):
    request = dict(id=id, name=name)
    response = database_api.get_user_info(request)
    assert response.status_code == 200

@parametrize_from_file(path_to_json_file, 'invalid_data')
def test_invalid_data(id, name):
    request = dict(id=id, name=name)
    response = database_api.get_user_info(request)
    assert response.status_code == 400

You could simplify this code a bit by reorganizing the JSON file slightly:

# test_api.json
{
    "test_id_name_requests": [
       {
           "request": {
               "id": "1234",
               "name": "John"
           },
           "status_code": 200
       },
       {
           "request": {
               "id": "2234",
               "name": "Mary"
           },
           "status_code": 200
       },
       {
           "request": {
               "id": "3234",
               "name": "Kenny"
           },
           "status_code": 200
       },
       {
           "request": {
               "id": "1234",
               "name": "Mary"
           },
           "status_code": 400
       },
       {
           "request": {
               "id": "2234",
               "name": "Kenny"
           },
           "status_code": 400
       },
       {
           "request": {
               "id": "3234",
               "name": "John"
           },
           "status_code": 400
       },
    ],
}

With this file, only one test function is needed and no arguments need to be given to the @parametrize_from_file decorator:

# test_api.py
import parametrize_from_file

@parametrize_from_file
def test_id_name_requests(request, status_code):
    response = database_api.get_user_info(request)
    assert response.status_code == status_code
🌐
GitHub
github.com › numirias › pytest-json-report › blob › master › tests › test_jsonreport.py
pytest-json-report/tests/test_jsonreport.py at master · numirias/pytest-json-report
🗒️ A pytest plugin to report test results as JSON. Contribute to numirias/pytest-json-report development by creating an account on GitHub.
Author   numirias
🌐
Carreau
carreau.github.io › pytest-json-report-viewer
Pytest Json-report viewer
You can generate report with pytest --json-report --json-report-file=report.json
Find elsewhere
🌐
Medium
medium.com › grammofy › testing-your-python-api-app-with-json-schema-52677fe73351
Testing Your Python API App with JSON Schema | by Paul Götze | Grammofy | Medium
August 21, 2020 - Let’s suppose we have a simple JSON response for a user endpoint GET /users/:id: Here is an example how you would naively test this response by checking the presence of the properties (client would be a pytest fixture, e.g.
🌐
Stack Overflow
stackoverflow.com › questions › 73428927 › pytest-execution-result-in-json-format
python - Pytest execution result in json format? - Stack Overflow
this is a testing file an I use pytest test_example.py -v --junitxml="result.xml" which gives me result like this:
🌐
Anaconda.org
anaconda.org › anaconda › pytest-json
Pytest Json :: Anaconda.org
Generate JSON test reports · Conda · Files · Labels · Badges · License: MIT · Home: https://github.com/mattcl/pytest-json · 2996 total downloads · Last upload: 1 year and 7 months ago · linux-32 v0.4.0 · linux-64 v0.4.0 · linux-ppc64le v0.4.0 · noarch v0.4.0 ·
🌐
GitHub
github.com › ericsalesdeandrade › pytest-read-json-example
GitHub - ericsalesdeandrade/pytest-read-json-example: 5 Easy Ways To Read JSON Input Data In Pytest
This project explains 5 simple ways to read JSON data in Pytest.
Forked by 3 users
Languages   Python 100.0% | Python 100.0%
🌐
Anaconda.org
anaconda.org › conda-forge › pytest-json-report
pytest-json-report - conda-forge | Anaconda.org
Install pytest-json-report with Anaconda.org. A pytest plugin to report test results as JSON files
🌐
Snyk
snyk.io › advisor › python packages › pytest-json
pytest-json - Python Package Health Analysis | Snyk
October 21, 2021 - Learn more about pytest-json: package health score, popularity, security, maintenance, versions and more.
🌐
GitHub
github.com › jondot › pytest-jest
GitHub - jondot/pytest-jest: A pytest plugin to report test results as JSON files
A pytest plugin to report test results as JSON files - jondot/pytest-jest
Author   jondot
🌐
automation hacks
automationhacks.io › home › 2020 › 12 › 25
Python API test automation framework (Part 5) Working with JSON and JsonPath - automation hacks
December 25, 2020 - Also, we use json.load() and give it a file to read from directly and return a python object that we could · Alright, so this helps us get a python object. Here is how we can use this in our test. Below is the complete test file. I know it looks huge 😏 Let’s unpack the changes. @pytest.fixture def create_data(): payload = read_file('create_person.json') random_no = random.randint(0, 1000) last_name = f'Olabini{random_no}' payload['lname'] = last_name yield payload def test_person_can_be_added_with_a_json_template(create_data): create_person_with_unique_last_name(create_data) response = r
🌐
Blogger
hanxue-it.blogspot.com › 2017 › 10 › pytest-testing-and-comparing-json-response-using-pytest-flask.html
Hanxue and IT: Pytest: Testing and Comparing JSON Response Using Pytest-Flask
October 18, 2017 - Let's say we have a Flask API endpoint that returns this JSON { "message": "Staff name and password pair not match", "errors": { ...