def setup_module(module): module.cache = {}
Testing Patterns

Meaning

Defines a pytest module-level setup function that initializes a cache dictionary on the module object for sharing state across tests in the same module.

Primary Function

Initialize module-level state (e.g., a cache) before any test functions in the module run.

Communicative Purpose

Signal to pytest that this function should be called to set up shared fixtures for the test module.

Pattern

def setup_module(module):\n module.cache = {}

Core Structure

Function definition named setup_module taking a module parameter and assigning an empty dictionary to an attribute of the module.

Função primária

Initialize module-level state (e.g., a cache) before any test functions in the module run.

Propósito comunicativo

Signal to pytest that this function should be called to set up shared fixtures for the test module.

Situações de gatilho

When pytest collects tests and discovers a setup_module function in a test module, it calls this function with the module object before executing any test in that module.

Contextos

Used in pytest test modules to set up shared fixtures, caches, or state that should be initialized once per test module.

Padrão

def setup_module(module):\n module.cache = {}

Estrutura central

Function definition named setup_module taking a module parameter and assigning an empty dictionary to an attribute of the module.

Colocados típicos

  • pytest teardown_module
  • test functions
  • module-level fixtures
  • pytest fixtures

Substituições comuns

  • Using pytest.fixture with scope='module' for more flexible setup/teardown
  • using setup_module to assign other objects like a database connection
  • using class-level setup_method for class-scoped setup.

Erros comuns

Forgetting the module parameter causes a TypeError: missing 1 required positional argument; assigning to a local variable instead of the module attribute results in the cache not being shared across tests; placing teardown logic in setup_module leads to resources never being cleaned up.

Similar / contraste

setup_module vs teardown_module: setup_module runs before tests, teardown_module runs after; setup_module vs setup_module: same hook but for package-level initialization; setup_module vs fixture with scope='module': fixture provides both setup and teardown via yield; setup_module vs class-level setup_method: module-level vs class-level setup.

Interferências

Coming from unittest: may expect setUpModule to be a classmethod; in pytest setup_module; in pytest setup_module is a plain function receiving the module object → define it as a plain function with a module parameter. Coming from JUnit: may expect @BeforeAll static method; in pytest use setup_module function → define a plain function named setup_module.

Família do chunk

  • setup_module
  • teardown_module
  • setup_module
  • teardown_module
  • fixture

Nuance

Avoid using setup_module for per-test state; prefer fixtures for isolation. Performance impact is negligible as it runs once per module. If the module is reloaded or imported multiple times, setup_module may run repeatedly, potentially re-initializing state.

Efeito pragmático

Ensures expensive resources (e.g., database connections, caches) are initialized once per test module, reducing test suite runtime and preventing redundant setup.

Dica de memória

Think of setup_module as the stagehand who sets up the stage props once before the play begins, so every scene (test) can use them without rebuilding.

Nota

The module object passed is the actual module; attributes set on it are accessible to all test functions in that module via the module attribute (e.g., module.cache).

Upgrade path

Use pytest.fixture with scope='module' to encapsulate both setup and teardown logic.

Tipo de construção: Function definition with a single parameter and an attribute assignment on the parameter.Tag de espaçamento: Medium-term

Log in to save chunks.