@classmethod def setUpClass(cls): cls.shared = SharedResource() cls.shared.setup()
Testing Patterns

Meaning

Defines a class method that initializes a shared resource before any test methods in the class run.

Primary Function

Initializes shared test resources at the class level before any test methods execute.

Communicative Purpose

Signals that the following block prepares shared state for a test suite.

Pattern

@classmethod\ndef setUpClass(cls):\n cls.shared = SharedResource()\n cls.shared.setup()

Core Structure

@classmethod def setUpClass(cls): cls.shared = <Resource>(); cls.shared.setup()

Função primária

Initializes shared test resources at the class level before any test methods execute.

Propósito comunicativo

Signals that the following block prepares shared state for a test suite.

Situações de gatilho

When you need to set up expensive or shared resources (e.g., database connections, file handles) once per test class rather than per test.

Contextos

Inside a unittest.TestCase subclass, typically placed at the top of the class definition before any test methods.

Padrão

@classmethod\ndef setUpClass(cls):\n cls.shared = SharedResource()\n cls.shared.setup()

Estrutura central

@classmethod def setUpClass(cls): cls.shared = <Resource>(); cls.shared.setup()

Colocados típicos

  • unittest.TestCase
  • test methods
  • tearDownClass
  • setUp
  • tearDown

Substituições comuns

  • Using setUp per test (more isolated but slower)
  • using module-level setup with setUpModule (runs once per module)
  • using pytest fixtures with scope='session' (more flexible reuse).

Erros comuns

Forgot @classmethod decorator causing AttributeError on cls; used self instead of cls creating instance attribute instead of class attribute; forgot to call super().setUpClass() in subclass causing parent setup to be skipped; assumed shared resource resets between tests leading to state leakage.

Similar / contraste

setUp (runs before each test method); tearDownClass (cleans up after all tests); setUpModule (module-level setup once per module); pytest fixtures with scope='session' (flexible reusable setup).

Interferências

Coming from JUnit: may use @BeforeClass instead of @classmethod def setUpClass(cls): — use @classmethod decorator and first argument cls.; Coming from pytest: may expect fixture with autouse instead of unittest style — use @classmethod setUpClass for class-level setup in unittest.

Família do chunk

  • setUp
  • tearDown
  • setUpClass
  • tearDownClass
  • setUpModule
  • tearDownModule

Nuance

Avoid using setUpClass for resources that vary per test or require isolation, as shared state can cause test interdependence; Sharing expensive resources reduces setup time but risks hidden coupling and difficult debugging if state leaks; Remember to call super().setUpClass() in subclasses to preserve parent setup, otherwise parent initialization may be skipped.

Efeito pragmático

Using setUpClass ensures expensive setup runs once, speeding up test suites and providing consistent preconditions, which reduces flaky tests caused by inconsistent setup.

Dica de memória

Think of setUpClass as the stage crew that builds the set once before the play begins, so every actor (test) can perform without rebuilding the set.

Nota

If the shared resource raises an exception during setup, the entire test class is skipped or marked as error, preventing any tests from running.

Upgrade path

Use pytest fixtures with scope='session' for more flexible and reusable test setup.

Tipo de construção: Class method definition with attribute assignment and method call.Tag de espaçamento: Medium-term

Log in to save chunks.