Skip to content

setUpModule and tearDownModule in __init__.py not called by unittest #138653

@pratt-fds

Description

@pratt-fds

Bug report

Bug description:

When structuring unittest tests across multiple files in a complex directory structure, module level setup and teardown functions, as described in the unittest documentation, are not called by the unittest framework. Running the same tests using pytest as the runner causes the setup and teardown functions to behave as expected.

File structure:

tests/
├── __init__.py
├── bar
│   ├── __init__.py
│   └── test_bar.py
├── base_class.py
└── foo
    ├── __init__.py
    └── test_foo.py
# tests/__init__.py content
def setUpModule():
    print("Setting up the tests module")

def tearDownModule():
    print("Tearing down the tests module")

# tests/base_class.py content
import unittest

class BaseTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        print("Setting up class resources in BaseTestCase")

    @classmethod
    def tearDownClass(cls):
        print("Tearing down class resources in BaseTestCase")
        super().tearDownClass()

# tests/foo/__init__.py content
def setUpModule():
    print("Setting up the tests.foo module")

def tearDownModule():
    print("Tearing down the tests.foo module")

# tests/foo/test_foo.py content
from tests.base_class import BaseTestCase

class FooTests(BaseTestCase):
    def test_foo(self):
        self.assertTrue(True)

# tests/bar/__init__.py file is empty

# tests/bar/bar.py content
from tests.base_class import BaseTestCase

def setUpModule():
    print("Setting up the tests.bar module")

def tearDownModule():
    print("Tearing down the tests.bar module")

class BarTests(BaseTestCase):
    def test_bar(self):
        self.assertTrue(True)

Actual output when running using unittest:

Received test ids from temp file.
Setting up the tests.bar module
Setting up class resources in BaseTestCase
test_bar (tests.bar.test_bar.BarTests) ... ok
Tearing down class resources in BaseTestCase
Tearing down the tests.bar module
Setting up class resources in BaseTestCase
test_foo (tests.foo.test_foo.FooTests) ... ok
Tearing down class resources in BaseTestCase
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Expected output when running using unittest:

Received test ids from temp file.
Setting up the tests module
Setting up the tests.bar module
Setting up class resources in BaseTestCase
test_bar (tests.bar.test_bar.BarTests) ... ok
Tearing down class resources in BaseTestCase
Tearing down the tests.bar module
Setting up the tests.foo module
Setting up class resources in BaseTestCase
test_foo (unittest_class_based.foo.test_foo.FooTests) ... ok
Tearing down class resources in BaseTestCase
Tearing down the tests.foo module
Tearing down the tests module
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

CPython versions tested on:

3.10

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytestsTests in the Lib/test dir

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions