Unittest2

1.1.0 · abandoned · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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()

view raw JSON →