I have a list of url pairs, and I created this parametrized test for them:
url_list = [
['example1.com/x', 'example2.com/x'],
['example1.com/y', 'example2.com/y'],
['example1.com/z', 'example2.com/z'],
['example1.com/v', 'example2.com/v'],
['example1.com/w', 'example2.com/w'],
]
@pytest.mark.parametrize('url1, url2', url_list)
def test_urls(url1: str, url2: str):
response1 = requests.get(url1)
response2 = requests.get(url2)
assert response1.status_code == response2.status_code
body1 = response1.json()
body2 = response2.json()
for field in ['one', 'two', 'three']:
assert body1[field] == body2[field]
The problem is that if the value for 'one' is bad, we don't test values for 'two' and 'three'.
I was thinking about adding another parametrization, something like this:
@pytest.mark.parametrize('field', ['one', 'two', 'three'])
@pytest.mark.parametrize('url1, url2', url_list)
def test_urls(url1: str, url2: str):
response1 = requests.get(url1)
response2 = requests.get(url2)
assert response1.status_code == response2.status_code
body1 = response1.json()
body2 = response2.json()
assert body1[field] == body2[field]
The problem with that is that the same request will be performed multiple times.
I could use global variables that would store the request responses between tests, but that feels ugly.
Is there anything within pytest that would help me?
Or a design pattern / a pythonic way of doing this?
I've been using python for a few years, but this is my first time using pytest.