Answer the question
In order to leave comments, you need to log in
How to create a paratremized test in Pytest using different parameters?
I'm trying to write a parameterized test where 5 requests are sent and for each request there should be a check for compliance with the expected parameters (the expected parameters are different for each request
I wrote the code, but if you specify the value UA in the expected_response_text variable, it doesn't work. Checks do not fall, only if you write it manually or In general, ah nid help :(
import pytest
import requests
class TestTask3:
UA = [
(
"Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"Mobile",
"No",
"Android"
),
(
"Mozilla/5.0 (iPad; CPU OS 13_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/91.0.4472.77 Mobile/15E148 Safari/604.1",
"Mobile",
"Chrome",
"iOS"
),
(
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"Googlebot",
"Unknown",
"Unknown"
),
(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.100.0",
"Web",
"Chrome",
"No"
),
(
"Mozilla/5.0 (iPad; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
"Mobile",
"No",
"iPhone"
)
]
answer = [{'platform':'Mobile', 'browser':'No', 'device':'Android'}]
bad_value = "Unknown"
@pytest.mark.parametrize('agent, platform, browser, device', UA)
def test_get_user_agent(self, agent, platform, browser, device):
url = "https://playground.learnqa.ru/ajax/api/user_agent_check"
UA = {"user-agent": agent}
response = requests.get(url, headers=UA)
parsed_response = response.json()
expected_response_text=[]
for i in UA:
expected_response_text.append(i)
expected_response_text = {'user_agent': 'Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30','platform': 'Mobile', 'browser': 'No', 'device': 'Android'}
actual_response_text = parsed_response
assert actual_response_text == expected_response_text, "{agent} contains {answer} parameter with {bad_value} value"
Answer the question
In order to leave comments, you need to log in
you have some mess in your code:
- here you go through the dictionary, add the key to the expected_response_text sheet, then reassign the hardcode dictionary to it - what do you think should happen here?
expected_response_text=[]
for i in UA:
expected_response_text.append(i)
expected_response_text = {'user_agent': 'Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 ( KHTML, like Gecko) Version/4.0 Mobile Safari/534.30','platform': 'Mobile', 'browser': 'No', 'device': 'Android'}
@pytest.mark.parametrize('agent, platform, browser, device', UA)
def test_get_user_agent(self, agent, platform, browser, device):
url = "https://playground.learnqa.ru/ajax/api/user_agent_check"
UA = {"user-agent": agent}
response = requests.get(url, headers=UA)
parsed_response = response.json()
expected_response_text = {
'user_agent': agent,
'platform': platform,
'browser': browser,
'device': device
}
print(expected_response_text)
actual_response_text = parsed_response
print(actual_response_text)
assert not (result := compare(expected_response_text, actual_response_text)), result
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question