I am trying to play around playwright/Pytest API testing.
I need to read data from xlsx file and returning dictionary in the form for TC_NAME,BODY,Result.
I have used Custom decorator method to make it available for parameterization
commonlib.py
def dict_parametrizes(test_data):
"""Custom decorator to parametrize a test function from a dictionary with custom IDs."""
def decorator(test_func):
test_cases = [(tc_name, data["Body"], data["expected"]) for tc_name, data in test_data.items()]
test_ids = [tc_name for tc_name, _, _ in test_cases] # Create a list of IDs
@pytest.mark.parametrize(
"TC_NAME, BODY, EXPECTED",
test_cases,
ids=test_ids # Use the custom IDs
)
def wrapper(TC_NAME, BODY, EXPECTED, *args, **kwargs):
return test_func(TC_NAME, BODY, EXPECTED, *args, **kwargs)
return wrapper
return decorator
def extract_data_from_excel(file, Worksheet, TC_NAME, BODY, "EXPECTED):
#implementation
return testdats_dictionary
def get_user_information(api_request_context, access_token):
#implementation
return userinfo
Now I did preparation of data using @dict_parametrizes(test) and used @pytest.mark.parameter(dic_data) for parameterisation and used it for test method for the API call.
in my Test file I have written
test.py
import json
import sys
import os
from typing import Generator
from playwright.sync_api import Playwright, APIRequestContext
import pytest
from commonlib import get_auth_token, get_user_information,extract_data_from_excel,dict_parametrizes
#Context Creation
def api_request_context(playwright: Playwright, env) -> Generator[APIRequestContext, None, None]:
...
#setup environment keeping some global values required during testing
@pytest.fixture(scope="session", autouse=True)
def setup_env(api_request_context, env):
global access_token
global userinfo
access_token = get_auth_token(api_request_context, env) # type: ignore
userinfo = get_user_information(api_request_context, access_token) # type: ignore
#Test Case -1
test = extract_data_from_excel("testdata.xlsx", "Solutions", "TC_NAME", "BODY", "EXPECTED")
@pytest.mark.tags("all","regression")
@dict_parametrizes(test) #parameterisation
def test_TC(TC_NAME,BODY,EXPECTED,api_request_context:APIRequestContext,setup_env) -> None:
Some how I am not able to figure it how to handle it here .I need to pass the api_request_context as parameter so that I can send the API request.
I have also tried to pass the api_request_context with @dict_parametrizes(test,api_request_context)
and def dict_parametrizes(test_data,api_request_context):
"""Custom decorator to parametrize a test function from a dictionary with custom IDs."""
def decorator(test_func):
test_cases = [(tc_name, data["Body"], data["expected"]) for tc_name, data in test_data.items()]
test_ids = [tc_name for tc_name, _, _ in test_cases] # Create a list of IDs
@pytest.mark.parametrize(
"TC_NAME, BODY, EXPECTED",
test_cases,
ids=test_ids # Use the custom IDs
)
def wrapper(TC_NAME, BODY, EXPECTED, *args, **kwargs):
return test_func(TC_NAME, BODY, EXPECTED,api_request_context, *args, **kwargs)
return wrapper
return decorator
and my new test method become like -
def test_TC(TC_NAME,BODY,EXPECTED,api_request_context)
but I'm not getting the APIcontext properly
Now I need to test with with multiple values for a single API endpoint with @pytest.mark.parametrize so that in my result I get every call as separate Test in my result.
test_TC(TC_NAME1)---- PASS
test_TC(TC_NAME2)---- PASS
test_TC(TC_NAME3)---- PASS