-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Closed as not planned
Closed as not planned
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytestsTests in the Lib/test dirTests in the Lib/test dir
Description
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
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytestsTests in the Lib/test dirTests in the Lib/test dir
Projects
Status
Done