{"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":"python","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":[{"wrong":"import unittest2; TestCase = unittest2.TestCase","symbol":"TestCase","correct":"from unittest2 import TestCase"}],"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,"search_vec":"'-2.6':23 '2':28 '2.4':22 '2.7':12 '2015':67 '3':32 'assert':42 'assertrais':49 'backport':5,78 'capabl':39 'class/module-level':46 'context':52 'develop':75 'discoveri':45 'earli':31 'earlier':17 'enhanc':8 'environ':59 'extend':26 'featur':9 'fixtur':47 'improv':41 'larg':70 'last':63 'later':25 'like':40 'make':68 'manag':53 'method':43 'modern':37,73 'modul':15 'obsolet':71 'older':57 'primarili':20 'project':61 'provid':36 'python':11,18,21,58,74 'python2':79 'releas':64 'test':44,76 'unittest':14,38,77 'unittest2':1,2 'user':55 'version':19,34 'x':29,33","created_at":"2026-04-12T03:49:52.348025+00:00","updated_at":"2026-04-12T03:49:52.348025+00:00","problems":{"verify_error":"Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\n  File \"/tmp/tmp5wrwpqmp/venv/lib/python3.12/site-packages/unittest2/__init__.py\", line 40, in <module>\n    from unittest2.collector import collector\n  File \"/tmp/tmp5wrwpqmp/venv/lib/python3.12/site-packages/unittest2/collect"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.1.0","cli_name":"","cli_version":null,"type":"library","homepage":"http://pypi.python.org/pypi/unittest2","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/unittest2/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-07-03","last_verified":"2026-08-29","next_check":"2026-07-10","install_tag":null}}