I have hundreds of test scripts that I am adding to a pytest framework I'm creating. Each script needs to open a connection to our device being tested, run a number of tasks and close. The connection is being created and closed via a fixture. Also want to add that a new connection needs to be made for each individual script that runs (i.e. module level and not scope or function level).
I have this working with a fixture in the same file as the tasks. Something like this.
my_test.py
...
@pytest.fixture(scope='module')
def setup(request):
global connection
with target.Connect() as connection:
yield connection
def teardown():
connection.close
request.addfinalizer(teardown)
@pytest.mark.usefixtures("setup")
def test_query_device_info():
global connection
connection.write('echo $PATH')
connection.write('echo $HOME')
...
As I have hundreds of tests I don't want to replicate this same code for each file so I need a common fixture that can be used by every test. I've tried adding this fixture to conftest.py and the connection is being created but fails when it gets to the connection.write command.
conftest.py
...
@pytest.fixture
def setup(request):
global connection
with target.Connect() as connection:
yield connection
def teardown():
connection.close
request.addfinalizer(teardown)
my_test.py
...
@pytest.mark.usefixtures("setup")
def test_query_device_info():
global connection
connection.write('echo $PATH')
How can I have this fixture in a common location that is accessible to all my tests and properly creates a connection that I can use in my scripts?
Note that these are being executed though the pyCharm IDE and not directly on the command line.