[Python] How can I set session details in conftest.py for yield along with fixture in pytest?

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    My aim is to create a pretest and post test in conftest.py file which will run after each test case in my test suite. i.e. I was trying to run the methods (login_page() and login()) before all tests and the methods (logout()) after all tests.

    I have tried using the below code snippet

    @pytest.fixture(scope="session", autouse=True)
    def pretest():
    login_page()
    login()
    yield driver
    logout()


    I have observed that, while my pre-tests (login_page() & login()) are running perfectly, before all test cases, post-tests (logout()) are not working as intended and was running only after all my selected test cases were executed.

    To try a different approach, I had tried using the below code snippet in conftest.py as well

    @pytest.fixture(scope="session", autouse=True)
    def pretest():
    login_page()
    login()
    @pytest.yield_fixture(scope="session", autouse=True)
    def posttest():
    logout()


    The above method was simply throwing some errors and was not launching the test as such.

    I had also tried the below code snippet in conftest.py file

    @pytest.yield_fixture(scope="session", autouse=True)
    def pretest():
    login_page()
    login()
    yield driver
    logout()

    Continue reading...

Compartilhe esta Página