Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

unittest

Python’s unittest module provides a standard framework for automating unit tests, sharing setup and cleanup, grouping tests, and reporting results. Tests are commonly organized as TestCase instances and run through the module’s command-line interface.

How it works

The module models tests as TestCase instances, usually created by subclassing TestCase and adding methods whose names begin with test. Assertions check expected behavior, while the command-line runner can execute modules, classes, methods, or discovered tests and report results.

Syntax

python -m unittest [options] [module[.Class[.method]] ...]

Parameters

NameTypeDescription
targetmodule, class, or method nameA module, TestCase class, or individual test method target passed to the command-line runner.

Returns

A test run report; failures and errors are reported by the runner.

Key points

  • The framework supports automated execution and reusable setup or cleanup around tests.
  • Tests can be grouped into collections while remaining independent from how results are reported.
  • A test case represents one scenario whose behavior is arranged and checked.
  • TestCase instances are the concrete objects used to represent individual tests.
  • Use -v with the command-line runner when diagnostic output needs to be more detailed.

Common mistakes

  • A test must be represented by a TestCase instance, typically produced from a TestCase subclass.
  • Use the module runner rather than relying only on direct function calls when executing test suites.

Example: A minimal unittest test

import unittest


def add(left, right):
    return left + right


class AddTests(unittest.TestCase):
    def test_adds_numbers(self):
        self.assertEqual(add(2, 3), 5)


if __name__ == "__main__":
    unittest.main()

Defines a TestCase subclass, checks an expected result, and runs the test module directly.

When to use it

Unit-level regression tests

Validate a small function or class behavior with explicit expected values.

Targeted command-line execution

Run a complete test module or selected dotted test target from a shell.

Suite organization

Organize multiple test cases into reusable collections for a larger suite.

Interview questions

QHow are tests represented?

A TestCase is the basic unit of a unittest test. Create one by subclassing unittest.TestCase and define test methods containing assertions.

Follow-up: How can one test be run without running its whole module? Pass the dotted module, class, and method name to python -m unittest.

QHow do you run selected tests?

The command supports module names, TestCase class names, and individual test method names. Add -v to request more detailed output.

Follow-up: What is the command-line shortcut for discovery? Running python -m unittest without a target is equivalent to unittest discover.

QWhat capabilities does unittest provide beyond assertions?

unittest supports test automation, shared setup and shutdown code, grouping tests into collections, and separating tests from the reporting framework.

Follow-up: Which mechanism finds tests automatically? Test discovery can locate tests across a project, while explicit targets restrict execution to chosen tests.

  • unittest.TestCase
  • unittest.TestSuite
  • unittest.TestLoader
  • unittest.main
  • unittest discover

Source: unittest — Unit testing framework. Checked 2026-10-16.