KapaA
KapaA

Reputation: 307

passing variable to pytest_sessionfinish

I am looking for a way to pass some variable from session start to session end in pytest.

More specifically I am using fixture scooped session where I create a serial-com object eg.:

@pytest.fixture(scope="session")
def init_setup(request):
# Create serial_com object

After this step I run some tests.

Finally I have pytest_sessionfinish(session, exitsstatus):, in here i would like to close my com object i created eg.:

def pytest_sessionfinish(session, exitstatus):
# close comport obj.

The problem here is I dont know if it is possible to store my comport obj. in one of these two arguments?

If not, is there a better way of doing this .ie. having a method to clean up your objects that you have created during the test setup phase (not during the test, but during the setup)

Upvotes: 2

Views: 1725

Answers (1)

Jack Thomson
Jack Thomson

Reputation: 490

Another way you could do it is via a yield. This will return your serial object and then allow you to do a teardown afterwards.:

Try something like this:

@pytest.fixture(scope="session")
def init_setup(request):
    # Create my serial object here
    yield myserialobject
    myserialobject.destroy()

Upvotes: 4

Related Questions