简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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