Registry / testing / unittest2

unittest2

JSON →
library1.1.0pypypi✓ verified 25d ago

Unittest2 is a backport of the enhanced features from Python 2.7's `unittest` module to earlier Python versions (primarily Python 2.4-2.6, but later extending to 2.x and early 3.x versions). It provided modern `unittest` capabilities like improved assertion methods, test discovery, class/module-level fixtures, and `assertRaises` as a context manager for users on older Python environments. The project's last release was in 2015, making it largely obsolete for modern Python development.

pip install unittest2
INSTALL
IMPORT
SIG · UNITTEST2
U
unittest2
testingpythonv1.1.0
Install
1.7s avg
Import
115ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.026s · 19.3MB
glibc
py 3.103.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.
fix
For 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.
fix
Use 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.
fix
If 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.
fix
Do 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.

Agent activity
26 hits · last 30 days
node
24
Amazon
1
Resources