Naveen Reddy Marthala
Naveen Reddy Marthala

Reputation: 3123

pytest - call a function that generates inputs to fill in inputs for pytest.mark.parametrize for desired number of tests

I have a function that can generate the input for one test and I want to invoke that function for some random number of times to send inputs to my testing_function with @pytest.mark.parametrize

## part that generates inputs needed
import numpy as np

def generate_proper_list():
    return np.random.uniform(low=0.1, high=8.0, size=(np.random.randint(low=1,high=5,size=1)[0],4))

### I would like to run this test for desired number of time, like for 4 tests, 5 tests etc,. 
## by invoking above function to generate inputs for each test.
@pytest.mark.parametrize('rand_nums', <some-thing>)
def test_proper_random(rand_nums)
    payload = {'input':rand_nums}
    response = client.post(..., data=json.dumps(payload).encode('utf-8'),...)
    assert response.status_code == 200

How do I do this?

Upvotes: 1

Views: 1517

Answers (1)

Teejay Bruno
Teejay Bruno

Reputation: 2159

A fixture can be used to supply your test function with the random input data, then you want to use parametrize to execute the function n amount of times.

import numpy as np

@pytest.fixture
def random_input():
    return np.random.uniform(low=0.1, high=8.0, size=(np.random.randint(low=1,high=5,size=1)[0],4))


@pytest.mark.parametrize("n_times", range(5))
def test_proper_random(n_times, random_input)
    payload = {'input':random_input}
    response = client.post(..., data=json.dumps(payload).encode('utf-8'),...)
    assert response.status == 200

Upvotes: 1

Related Questions