Skip to content

Fixture scopes

Overview

Type:

There are 5 types of fixtures in the pytest, which included

flowchart LR
fixtures[Fixtures] --> function & classes[class] & module & package & session

Lifecycle:

Fixtures are created when first requested by a test, and are destroyed based on their scope:

function: the default scope, the fixture is destroyed at the end of the test.

class: the fixture is destroyed during teardown of the last test in the class.

module: the fixture is destroyed during teardown of the last test in the module.

package: the fixture is destroyed during teardown of the last test in the package.

session: the fixture is destroyed at the end of the test session.

Order:

Scopes are ordered from lower to higher, that is:

          ->>> higher ->>>
Function < Class < Module < Package < Session
          <<<- lower  <<<-