Use pytest to test multiple options

Viewed 663

I am attempting to test a web API. Let's say that an endpoint accepts multiple parameters:

  • type: with possible values of "big", "small", "medium"
  • color: with possible values of "black", "white", "red"
  • shipping: with possible values of "1", "2", "7"

I want to test all combinations of these to ensure the API is returning correct results. At first I thought I could build 3 fixtures:

valid_types = ["big", "small", "medium"]
valid_colors = ['black', 'white', 'red']
valid_shipping = ['1', '2', '7']

@pytest.fixture(params=valid_types)
def valid_type(request):
    return request.param


@pytest.fixture(params=valid_colors)
def valid_color(request):
    return request.param


@pytest.fixture(params=valid_shipping)
def valid_ship(request):
    return request.param

But, I'm not sure how I can create the permutations for all of this. My test should operate like this:

def test_api_options(valid_type, valid_color, valid_ship):
    url_query = "?type={}&color={}&ship={}".format(valid_type, valid_color, valid_ship)
    r = requests.get("{}{}".format(base_url, url_query)

The test should run for each permutation and generate a new url with the available options for each. How can I do this with pytest?

2 Answers
Related