As of 2.7 (per the documentation) you get setUpClass and tearDownClass which execute before and after the tests in a given class are run, respectively. Alternatively, if you have a group of them in one file, you can use setUpModule and tearDownModule (documentation).

EDIT: Note that setUpClass and tearDownClass must be declared using @classmethod. Another answer to this question includes sample code.

Otherwise your best bet is probably going to be to create your own derived TestSuite and override run(). All other calls would be handled by the parent, and run would call your setup and teardown code around a call up to the parent's run method.

Answer from David H. Clements on Stack Overflow
🌐
Python
docs.python.org › 3 › library › unittest.html
unittest — Unit testing framework
It is recommended that you use TestCase implementations to group tests together according to the features they test. unittest provides a mechanism for this: the test suite, represented by unittest’s TestSuite class. In most cases, calling unittest.main() will do the right thing and collect all the module’s test cases for you and execute them.
Discussions

unit testing - Python unittests, statement before test cases - Stack Overflow
The setUp() method is executed before each individual test while the setUpClass() method is executed before all the tests in this class run. import unittest class MyTests(unittest.TestCase): @classmethod def setUpClass(cls): print("Starting all the tests.") def setUp(self): print("Starting ... More on stackoverflow.com
🌐 stackoverflow.com
Python unittest startTestRun to execute setup only once before all tests - Stack Overflow
I have several test files in different directories. \tests \subtestdir1 -__init__.py -test1.py \subtestdir2 -__init__.py -test2.py -__init__.py -test... More on stackoverflow.com
🌐 stackoverflow.com
How to a run specific code before & after each unit test in Python - Stack Overflow
Following is the structure of my tests in a file. Class setup test01 test02 test03 teardown I have a requirement to run specific code before and after each test. For before, I could in... More on stackoverflow.com
🌐 stackoverflow.com
Is there a Python unittest function that runs once at the start and end of a test suite? - TestMu AI Community
Is there a function in Python unittest that is executed at the beginning and end of a test suite, rather than before and after every individual test? I am looking for a way to execute setup and teardown once for all tests in a scenario, rather than for each individual test. More on community.testmu.ai
🌐 community.testmu.ai
0
January 16, 2025
🌐
DZone
dzone.com › testing, deployment, and maintenance › testing, tools, and frameworks › python unit testing: one time initialization
Python Unit Testing: One Time Initialization - DZone
March 17, 2021 - The fact that we can keep code that is common to all test methods in setUp and tearDown is the most logical design. The only part about this design is the fact that setUp and tearDown are called before each test method execution.
🌐
CodingNomads
codingnomads.com › python-test-setup-teardown
Python Test `setup()` and `tearDown()`
As mentioned further up, the .setUp() method will run before every test, which means that these two variables will be available for each test to use. The corresponding .tearDown() method can also be useful, and it executes after each test case.
🌐
Python Tutorial
pythontutorial.net › home › python unit testing › python test fixtures
Python Test Fixtures
July 9, 2022 - The setUpModule() function runs before all test methods in the test module. The tearDownModule() function runs after all methods in the test module. See the following example: import unittest def setUpModule(): print('Running setUpModule') def tearDownModule(): print('Running tearDownModule') ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-write-unit-tests-for-instance-methods-in-python
How to Write Unit Tests for Instance Methods in Python
January 31, 2023 - The tearDownClass() class method is executed after all the tests have run. The setUp() and tearDown() methods are executed before and after each test, respectively. Let’s add the setUpClass() and tearDownClass() class methods to the TestBook ...
Find elsewhere
🌐
Codecademy
codecademy.com › learn › learn-intermediate-python-3 › modules › unit-testing › cheatsheet
Learn Intermediate Python 3: Unit Testing Cheatsheet | Codecademy
In the code example, 'This runs once before all tests' will be printed once before all the tests in the class, and 'This runs once after all tests' will be printed after all the tests. ... Learn Intermediate Python 3 and practice leveraging Python’s unique features to build powerful, ...
🌐
Dataquest
dataquest.io › home › blog › a beginner's guide to unit tests in python
A Beginner's Guide to Unit Tests in Python
April 8, 2025 - Now that we understand the basics of unit testing with the unittest module, let's optimize our code a bit. You probably have noticed that inside each test we initialized an object of the Calculations class, which will be tested. However, we can avoid that by creating a setUp method. The TestCase class already has a setUp method that runs before each test.
🌐
CodinGame
codingame.com › playgrounds › 10614 › python-unit-test-with-unittest
Python Unit Test with unittest
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
🌐
iO Flood
ioflood.com › blog › python-unittest
Python Unittest Library: Unit Testing Guide
January 30, 2024 - Are you finding it difficult to handle unit testing in Python? You’re not alone. Many developers find themselves in a bind when it comes to implementing unit tests in their Python code. But, consider Python’s unittest module as your safety net – it’s designed to catch bugs before they ...
🌐
Real Python
realpython.com › python-unittest
Python's unittest: Writing Unit Tests for Your Code – Real Python
April 29, 2024 - The preparations before the test run are known as setup, while the tasks that you perform after the test run are called teardown. The setup process may involve the creation of temporary files, objects, databases, dataframes, network connections, ...
🌐
Codecademy
codecademy.com › learn › learn-intermediate-python-3-exceptions-and-unit-testing › modules › unit-testing-course › cheatsheet
Learn Intermediate Python 3: Exceptions and Unit Testing: Unit Testing Cheatsheet | Codecademy
In the code example, 'This runs once before all tests' will be printed once before all the tests in the class, and 'This runs once after all tests' will be printed after all the tests.
🌐
Hands-on
hands-on.cloud › home › uncategorized › easy python unit tests: setup, teardown, fixtures, and more
Easy Python Unit Tests: Setup, Teardown, Fixtures, And More
February 25, 2024 - In unit testing, ‘setup’ refers to preparation before a test or a group of tests can run. Python’s unittest module provides a setUp() method, where you can write the preparation code.
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › writing › tests
Testing Your Code — The Hitchhiker's Guide to Python
Then, when developing a function inside a module, run this function’s tests frequently, ideally automatically when you save the code. Always run the full test suite before a coding session, and run it again after.
🌐
Testmuai
testmuai.com › testmu ai › learning hub › unit testing in python
Unit Testing in Python: Comprehensive Guide
July 17, 2026 - You can run the tests as before, just running the below command: python -m unittest test_register_account_password.py · All the tests inside the test_register_account_password will be run.
🌐
TestMu AI Community
community.testmu.ai › ask a question
Is there a Python unittest function that runs once at the start and end of a test suite? - TestMu AI Community
January 16, 2025 - Is there a function in Python unittest that is executed at the beginning and end of a test suite, rather than before and after every individual test? I am looking for a way to execute setup and teardown once for all tests in a scenario, rather than for each individual test.