Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.026s · 19.3MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.020s · 20MB
18MB installed
● package 18MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
TestCase
✓ from unittest2 import TestCase
✗ import unittest2; TestCase = unittest2.TestCase
This example demonstrates how to define a test class inheriting from `unittest2.TestCase`, implement `setUp` and `tearDown` methods, and write test methods using various assertion methods. The `unittest2.main()` call runs the tests when the script is executed directly.
import unittest2 as unittest
class MyTests(unittest.TestCase):
def setUp(self):
# Setup resources before each test method
self.data = [1, 2, 3]
def test_addition(self):
self.assertEqual(sum(self.data), 6)
def test_empty_list(self):
with self.assertRaises(TypeError):
sum(None)
def tearDown(self):
# Clean up resources after each test method
del self.data
if __name__ == '__main__':
unittest.main()
Debug
Known issues
breakingUnittest2 is primarily a backport for Python 2.x and early Python 3.x versions. For Python 3.2+ users, all features provided by unittest2 are integrated directly into the standard library's `unittest` module, rendering unittest2 largely unnecessary and potentially conflicting.fixFor Python 3.2 and newer, use the built-in `unittest` module directly. No separate installation is needed. For Python 2.7, while unittest2 offers more features, the standard `unittest` module may suffice depending on requirements.
affects: < 3.2
gotchaCommand-line test discovery (`python -m unittest`) is a Python 2.7+ feature. For Python versions older than 2.7, unittest2 provides a `unit2` script (or `unit2.py` on Windows) for command-line test execution and discovery. Direct `python -m unittest2` will not work.fixUse the `unit2` script provided by unittest2 (e.g., `unit2 discover`) or call `unittest2.main()` from within your test file. If migrating to Python 2.7+, switch to `python -m unittest` or remove unittest2 entirely and use the standard library.
affects: < 2.7
gotchaThe default value of `TestCase.longMessage` in unittest2 is `True`, which differs from `unittest` in Python 2.7 (where it defaults to `False` for backward compatibility). This can affect the verbosity of failure messages.fixIf consistent `longMessage` behavior is required, explicitly set `self.longMessage = False` (or `True`) within your `TestCase` or `setUp` method.
affects: All unittest2 versions compared to Python 2.7's unittest
deprecatedThere was a separate `unittest2py3k` distribution for Python 3 on PyPI, which used the package name `unittest2`. This specific distribution is obsolete. If you are developing for Python 3, you should use the standard library's `unittest` module.fixDo not install `unittest2py3k` or `unittest2` for Python 3. Always use `import unittest` from the Python standard library.
affects: Python 3.x with unittest2py3k
Upgrade
Version history
1.1.0latest on PyPI · released Jun 30, 2015
Audit
Dependencies
No dependency data recorded yet.