0

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?

1
  • You're already using global variables to run this (ill-advised) so why not continue to do that ? Commented Apr 20, 2024 at 15:25

1 Answer 1

0

It's not possible to access a variable in function outside said function (maybe it's doable with a hacky solution using execution frames, but this is a bad idea). You said that you are using pytest framework. It has support for something called fixtures. By default they are ran for each test individually, but their scope can be changed. Example:

import pytest @pytest.fixture(scope="module") def fix(): print("Fixtured") return ["aaa"] def test_a(fix): print(fix) fix.append("bbb") def test_b(fix): print(fix) 

When running pytest -rxP (flags are for output), I get this output:

==================================== PASSES ==================================== ____________________________________ test_a ____________________________________ ---------------------------- Captured stdout setup ----------------------------- Fixtured ----------------------------- Captured stdout call ----------------------------- ['aaa'] ____________________________________ test_b ____________________________________ ----------------------------- Captured stdout call ----------------------------- ['aaa', 'bbb'] 

This shows that here, an object is shared between tests. Note that if test a fails, test b should not be ran. See this answer on how to implement it. The final code would look like this:

import pytest @pytest.fixture(scope="module") def shared(): return [] @pytest.mark.dependency() def test_a(shared): shared.append(some_function()) @pytest.mark.dependency(depends=["test_a"]) def test_b(shared): some_other_function(shared[0]) 

Note that normally this example is against the idea of unit tests (small test that don't have complex relations between them), but I see why this might be a bad idea here, where the test is required to make a request. Sending a request twice, where they don't have any difference sounds bad.

Sign up to request clarification or add additional context in comments.

3 Comments

But how do I transfer a value obtained in one _test to the next _test using fixtures?
@MagnusJensen See the last code example: test_a transfers a result of some_function() to a shared fixture, which, test_b can access.
Also, note that using pytest.mark.dependency requires that pytest-dependency is installed together with pytest, as it's an extension and not a feature in pytest.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.