I have a working function that obtains a value, not defined elsewhere in my script
def get_requests(t, endpoint): response = requests.get(f'{base_url}{endpoint}', headers={"Authorization": f'Bearer {t}', "Content-Type": 'application/json'}) try: corr_value = response.json()[0]["powerPlant"]["id"] except KeyError: print("Unable to get powerPlant id") print("Powerplant ID: ", corr_value) return response Usage of the function is a pytest:
def test_get_powerplants(): # Act: response = get_requests(token, '/powerplant/all') # Assertion: assert response.status_code == 200 # Validation of status code print("Response: ", response.text) I want to use the variable "corr_value" from my function "get_requests" in another test:
def test_bids_generate(): # Act: response = post_requests(token, '/bids/generate', setpayload_bid_generate()) # Assertion: assert response.status_code == 200 # Validation of status code print("Response: ", response.text) Where setpayload_bid_generate() should get the corr_value in as a parameter and put it into the request body like this:
def setpayload_bid_generate(): myjson = {"powerPlantIds": {corr_value}} return myjson
However, the function setpayload_bid_generate() does not recognize corr_value in the above function.
I guess this has to do with scope. Should corr_value be defined outside all functions as a global value to be able to refer to it in different functions?