Country or region and language

    Choose your country or region and your language.

    Suggested for you

      All countries and regions

        Choose your character

        It shows in the top bar and on your profile. Change it any time.

        Sign in or join
        Python for software engineers course

        Pytest fixtures and cleanup

        Opening Python for software engineers
        Your turn

        Pytest fixtures and cleanup

        Fresh state for every test

        A fixture is a function pytest calls for each test that names it as a parameter. A function-scoped fixture builds a new value every time, so one test cannot leave a record behind for the next. Put the setup in the fixture instead of a list at module scope, and assert both the starting state and the state after the test changes it.

        Cleanup that survives a failure

        When the fixture owns a resource, yield it: the code after yield runs as teardown, even when an assertion fails. tmp_path gives each test its own directory. The pytest fixture guide covers scopes and factories. In these exercises you write the tests yourself. The checker runs them against a correct module and two faulty ones, and your module only counts when an assertion catches each fault. A skipped test or a setup error does not count.

        Guides on this topic

        Your Work Gym account

        Save your progress and pick up on another device. Your browser's progress comes with you.

        Your account can start a 5-day free trial: every course, unit, project and exam. Card needed; cancel any time and pay nothing.

        New accounts get 6 hours of full access: every course, unit, project and exam. The clock starts when you go past the free lessons.

        Plan your practice from a job ad or syllabus

        Have a referral code?

        or

        You can keep practicing without an account.

        Change your plan

        Checking the price

        Help and feedback

        Ask a question, tell us what is not working, or share an idea. We reply by email.

        What is it about?

        We use it only to reply to you.