Issue
I had a working test project where I loaded the data in conftest.py
and was able to use that data in all my tests in that directory. For some reason, it's stopped working.
conftest.py
:
def pytest_configure(config):
global test_data
test_data = pd.read_excel("file.xlsx", index_col=None, sheet_name='Sheet1')
test_temp1.py
:
from conftest import *
def input_data():
print(test_data)
Error:
E NameError: name 'test_data' is not defined
I have tried declaring the variables outside pytest_configure()
, but the value initalized in pytest_configure
has scope only inside the function. Not sure if there were any recent updates that could have caused this. Any idea or suggestion on a cleaner way to do this?
Solution
Maybe the change comes from this deprecation. You can define kind of global variables in the pytest's namespace.
## conftest.py
def pytest_configure(config):
pytest.test_data = pd.read_excel("file.xlsx", index_col=None, sheet_name='Sheet1')
## test file
from pytest import test_data
def test_input_data():
print(test_data)
Notes: use of fixtures
If you can perform some refactoring, the way to deal with data that are loaded in Pytest is called fixture. So you could define your test data as a fixture and use (request it) in your tests.
import pytest
@pytest.fixture
def test_data():
return pd.read_excel("file.xlsx", index_col=None, sheet_name='Sheet1')
def test_input_data(test_data):
print(test_data)
You can even choose the scope of the fixture to perform this data loading according to your needs, see fixture scopes.
Answered By - Romain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.