{"id":4309,"library":"unittest2","title":"Unittest2","description":"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.","status":"abandoned","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/garbas/unittest2","tags":["testing","unittest","backport","python2"],"install":[{"cmd":"pip install unittest2","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"For compatibility with unittest in newer Python versions, it's common to import unittest2 as unittest: `import unittest2 as unittest`. Then use `unittest.TestCase`.","wrong":"import unittest # for older Python versions requiring unittest2 features","symbol":"TestCase","correct":"from unittest2 import TestCase"},{"note":"The `python -m unittest` command-line interface for test discovery was introduced in Python 2.7. For older Python versions using unittest2, use the `unit2` script or `unittest2.main()` in a test file.","wrong":"python -m unittest","symbol":"main","correct":"import unittest2; unittest2.main()"}],"quickstart":{"code":"import unittest2 as unittest\n\nclass MyTests(unittest.TestCase):\n    def setUp(self):\n        # Setup resources before each test method\n        self.data = [1, 2, 3]\n\n    def test_addition(self):\n        self.assertEqual(sum(self.data), 6)\n\n    def test_empty_list(self):\n        with self.assertRaises(TypeError):\n            sum(None)\n\n    def tearDown(self):\n        # Clean up resources after each test method\n        del self.data\n\nif __name__ == '__main__':\n    unittest.main()","lang":"python","description":"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."},"warnings":[{"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.","message":"Unittest2 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.","severity":"breaking","affected_versions":"< 3.2"},{"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.","message":"Command-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.","severity":"gotcha","affected_versions":"< 2.7"},{"fix":"If consistent `longMessage` behavior is required, explicitly set `self.longMessage = False` (or `True`) within your `TestCase` or `setUp` method.","message":"The 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.","severity":"gotcha","affected_versions":"All unittest2 versions compared to Python 2.7's unittest"},{"fix":"Do not install `unittest2py3k` or `unittest2` for Python 3. Always use `import unittest` from the Python standard library.","message":"There 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.","severity":"deprecated","affected_versions":"Python 3.x with unittest2py3k"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}