I am new to Pytest, below is my query. I'm trying to add a negative automation test cases to the following API using Pytest, I have an API request data as below
variables.py
-----------------
myrequest_data = {
"name" : "Test User",
"age" : "20",
"city" : "Bengaluru"
}
negative_values = ["123456", "@$%$Dev", "xsdcsv", "-4565432", "45f"]
key_list = ["name", "age", "city"]
And below is my test case file,
test_person_api.py
--------------------
class TestPersonAPI:
@pytest.mark.parametrize("request_key", variables.key_list)
@pytest.mark.parametrize("negative_values", variables.negative_values)
def test_person_api_negative_verification(self, request_key, negative_values):
headers = {'Content-Type': 'application/json'}
url = "http://person-api-request-url/endpoint"
variables.myrequest_data[request_key] = negative_values
response = requests.put(url, data=json.dumps(req), headers=headers)
print("Response:----->>" + response.text)
assert response.status_code == 400
So my code is supposed to update the negative value for each json key from my variable myrequest_data on each execution, for example,
TC1[123456-name]
TC2[123456-age]
TC3[123456-city]
TC4[@$%$Dev-name]
TC5[@$%$Dev-age]
TC6[@$%$Dev-city]
TC7[xsdcsv-name]
TC8[xsdcsv-age]
TC9[xsdcsv-city]
.
.
.
[and so on...]
But, for the above test case myrequest_data variable is updated and saved on each execution, so the variable is not freshly retreived from the file.
Like in the below example console log, for every test case executed the previously set json key-value is still available. By the end of TC3 all the keys have the negative value 12345 set to them.
TC1 -
payload --->> { "name": "123456", "age": "20", "city": "Bengaluru" }
TC2 -
payload --->> { "name": "123456", "age": "123456", "city": "Bengaluru" }
TC3 -
payload --->> { "name": "123456", "age": "123456", "city": "123456" }
[and so on...]
Whereas, i need a new data for each execution, below is the desired result i want.
On each execution the payload should be retrieved from the variable as it is and the negative value 12345 should be set to the respective keys only.
TC1 -
payload --->> { "name": "123456", "age": "20", "city": "Bengaluru" }
TC2 -
payload --->> { "name": "Test User", "age": "123456", "city": "Bengaluru" }
TC3 -
payload --->> { "name": "Test User", "age": "20", "city": "123456" }
[and so on...]
Please help me with this issue, i have tried a lot to resolve this