How to pass a variable between tests where the variable gets updated within each test. #11583
Replies: 2 comments 1 reply
-
Hi @PatrickFlaherty, Adding a variable to a module makes it global, this is how Python works and not related to pytest. However it is recommended for tests to not depend on one another, so it is not recommended in general for tests to generate and pass information to one another. |
Beta Was this translation helpful? Give feedback.
-
As mentioned above, it is not advisable for tests to rely on each other. However, Here's a basic example to illustrate this, using a test class: import pytest
class TestSequence:
shared_data = {} # Class attribute to store shared data
@pytest.mark.dependency(name="test_one")
def test_one(self):
TestSequence.shared_data['value'] = 'data from test_one'
@pytest.mark.dependency(depends=["test_one"], name="test_two")
def test_two(self):
assert self.shared_data['value'] == 'data from test_one'
# Update the variable
TestSequence.shared_data['value'] = 'data from test_two'
@pytest.mark.dependency(depends=["test_two"])
def test_three(self):
assert self.shared_data['value'] == 'data from test_two' If a test fails, dependent tests will be skipped. Notice: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How to pass a variable between tests where the variable gets updated within each test within a module?
I see I can use a namespace within the pytest framework.
If for example I use pytest.my_variable, does this become a global variable such that I need to worry about using the same variable name in other tests running in parallel?
Thanks in advance for any input,
Patrick
Beta Was this translation helpful? Give feedback.
All reactions