Claude Code for unittest: Python Standard Test Framework — Claude Skills 360 Blog
Blog / AI / Claude Code for unittest: Python Standard Test Framework
AI

Claude Code for unittest: Python Standard Test Framework

Published: August 29, 2028
Read time: 5 min read
By: Claude Skills 360

Python’s unittest module provides a test framework with test cases, assertions, test suites, mocking, and a test runner. import unittest. TestCase: class MyTest(unittest.TestCase) — each test_* method is a test; setUp() runs before each; tearDown() runs after. Assertions: assertEqual(a, b), assertNotEqual, assertTrue(x), assertFalse(x), assertIsNone(x), assertIsNotNone(x), assertIn(a, b), assertNotIn, assertRaises(Exc, fn, *args), assertAlmostEqual(a, b, places=7), assertRegex(text, pattern), assertDictEqual, assertListEqual, assertCountEqual (order-independent list equality). self.fail(msg) — unconditional failure. Assertions accept optional msg= for custom failure messages. Mocking: from unittest.mock import Mock, MagicMock, patch, call. Mock: m = Mock(); m(1, 2) → Mock; m.return_value = 42; m.side_effect = exc_or_list; m.assert_called_once_with(1, 2). patch: @patch("module.ClassName") or with patch("module.func") as mock_fn. subTest: with self.subTest(i=i): ... — loop over cases without full test failure on first bad case. Decorators: @unittest.skip("reason"), @unittest.skipIf(cond, "msg"), @unittest.expectedFailure. Running: python -m unittest discover -s tests -p "test_*.py". Claude Code generates test cases, fixture factories, mock patch helpers, assertion libraries, and CI test runners.

CLAUDE.md for unittest

## unittest Stack
- Stdlib: import unittest; from unittest.mock import Mock, patch, MagicMock
- Run:    python -m unittest discover -s tests
- Assert: self.assertEqual(a, b) / assertRaises / assertAlmostEqual
- Mock:   @patch("mymod.requests.get") / Mock(return_value=...)
- Sub:    with self.subTest(n=n): self.assertEqual(fn(n), expected[n])
- Skip:   @unittest.skipIf(sys.platform=="win32", "unix only")

unittest Test Suite Pipeline

# tests/testutil.py — base classes, factories, matchers, mock helpers, runner
from __future__ import annotations

import json
import os
import sys
import tempfile
import unittest
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Callable
from unittest.mock import MagicMock, Mock, call, patch


# ─────────────────────────────────────────────────────────────────────────────
# 1. Base test case with helpers
# ─────────────────────────────────────────────────────────────────────────────

class BaseTestCase(unittest.TestCase):
    """
    Extended TestCase with convenience assertions and temp-file helpers.
    """

    # ── Approximate and tolerance assertions ─────────────────────────────────

    def assertApprox(self, actual: float, expected: float, rel: float = 1e-6) -> None:
        """Assert actual ≈ expected to relative tolerance rel."""
        if expected == 0:
            self.assertAlmostEqual(actual, 0, delta=rel)
        else:
            self.assertLess(abs(actual - expected) / abs(expected), rel,
                            msg=f"{actual} not ≈ {expected} (rel={rel})")

    def assertBetween(self, value: Any, low: Any, high: Any) -> None:
        """Assert low <= value <= high."""
        self.assertGreaterEqual(value, low, msg=f"{value} < {low}")
        self.assertLessEqual(value, high, msg=f"{value} > {high}")

    def assertSubset(self, subset: Any, superset: Any) -> None:
        """Assert every key/item in subset is in superset."""
        for item in subset:
            self.assertIn(item, superset, msg=f"{item!r} not in {superset!r}")

    def assertDictSubset(self, subset: dict, full: dict) -> None:
        """Assert all key:value pairs from subset appear in full dict."""
        for k, v in subset.items():
            self.assertIn(k, full, msg=f"key {k!r} not in dict")
            self.assertEqual(full[k], v, msg=f"dict[{k!r}]: {full.get(k)!r} != {v!r}")

    def assertRaisesMessage(self, exc_type: type, message: str, fn: Callable, *args: Any, **kwargs: Any) -> None:
        """Assert fn raises exc_type and the exception message contains message."""
        with self.assertRaises(exc_type) as ctx:
            fn(*args, **kwargs)
        self.assertIn(message, str(ctx.exception),
                      msg=f"Exception message {str(ctx.exception)!r} does not contain {message!r}")

    def assertNoException(self, fn: Callable, *args: Any, **kwargs: Any) -> Any:
        """Assert fn does not raise; return the result."""
        try:
            return fn(*args, **kwargs)
        except Exception as exc:
            self.fail(f"Unexpected exception: {type(exc).__name__}: {exc}")

    # ── Temporary file helpers ────────────────────────────────────────────────

    def tmp_file(self, content: str = "", suffix: str = ".txt", encoding: str = "utf-8") -> Path:
        """
        Create a temporary file with content; auto-deleted after the test.

        Example:
            path = self.tmp_file("hello\\nworld\\n", suffix=".log")
        """
        tf = tempfile.NamedTemporaryFile(mode="w", suffix=suffix, encoding=encoding,
                                         delete=False)
        tf.write(content)
        tf.close()
        self.addCleanup(lambda: Path(tf.name).unlink(missing_ok=True))
        return Path(tf.name)

    def tmp_dir(self) -> Path:
        """Create a temporary directory; auto-deleted after the test."""
        d = tempfile.mkdtemp()
        import shutil
        self.addCleanup(lambda: shutil.rmtree(d, ignore_errors=True))
        return Path(d)

    def tmp_json(self, obj: Any) -> Path:
        """Write obj as JSON to a temp file and return the path."""
        return self.tmp_file(json.dumps(obj, indent=2), suffix=".json")


# ─────────────────────────────────────────────────────────────────────────────
# 2. Parameterized test helpers
# ─────────────────────────────────────────────────────────────────────────────

def parametrize(cases: list[tuple]) -> Callable:
    """
    Decorator for generating separate test methods from a list of (args...) tuples.
    The test method receives the tuple values as positional arguments.

    Example:
        @parametrize([(2, 4), (3, 9), (4, 16)])
        def test_square(self, n, expected):
            self.assertEqual(n * n, expected)
    """
    def decorator(fn: Callable) -> Callable:
        def wrapper(self: unittest.TestCase) -> None:
            for i, args in enumerate(cases):
                with self.subTest(case=i, args=args):
                    if isinstance(args, tuple):
                        fn(self, *args)
                    else:
                        fn(self, args)
        wrapper.__name__ = fn.__name__
        return wrapper
    return decorator


# ─────────────────────────────────────────────────────────────────────────────
# 3. Mock factory helpers
# ─────────────────────────────────────────────────────────────────────────────

def mock_response(
    status_code: int = 200,
    json_data: Any = None,
    text: str = "",
    headers: dict | None = None,
    raise_exc: Exception | None = None,
) -> Mock:
    """
    Build a Mock mimicking requests.Response.

    Example:
        mock_resp = mock_response(200, json_data={"id": 1, "name": "Alice"})
        with patch("myapp.requests.get", return_value=mock_resp):
            result = myapp.get_user(1)
    """
    m = Mock()
    m.status_code = status_code
    m.text = text
    m.headers = headers or {}
    m.ok = 200 <= status_code < 300
    if raise_exc:
        m.json.side_effect = raise_exc
        m.raise_for_status.side_effect = raise_exc
    else:
        m.json.return_value = json_data or {}
        m.raise_for_status.return_value = None
    return m


def mock_open_file(content: str, encoding: str = "utf-8") -> MagicMock:
    """
    Return a MagicMock suitable for patching builtins.open with text content.

    Example:
        with patch("builtins.open", mock_open_file("key=value\\nfoo=bar")):
            config = load_config("config.ini")
    """
    from unittest.mock import mock_open
    return mock_open(read_data=content)


def make_mock_db(rows: list[dict]) -> MagicMock:
    """
    Build a MagicMock db cursor that returns rows from fetchall()/fetchone().

    Example:
        db = make_mock_db([{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}])
        db.cursor().fetchall()  # [{"id": 1, ...}, {"id": 2, ...}]
    """
    cursor = MagicMock()
    cursor.fetchall.return_value = rows
    cursor.fetchone.return_value = rows[0] if rows else None
    cursor.rowcount = len(rows)
    db = MagicMock()
    db.cursor.return_value.__enter__ = Mock(return_value=cursor)
    db.cursor.return_value.__exit__ = Mock(return_value=False)
    db.cursor.return_value = cursor
    return db


# ─────────────────────────────────────────────────────────────────────────────
# 4. Context helpers
# ─────────────────────────────────────────────────────────────────────────────

@contextmanager
def env_var(key: str, value: str):
    """
    Context manager to temporarily set an environment variable.

    Example:
        with env_var("DATABASE_URL", "sqlite:///:memory:"):
            app.init_db()
    """
    old = os.environ.get(key)
    os.environ[key] = value
    try:
        yield
    finally:
        if old is None:
            del os.environ[key]
        else:
            os.environ[key] = old


@contextmanager
def capture_stdout():
    """
    Capture stdout during a block; return the captured string.

    Example:
        with capture_stdout() as out:
            print("hello")
        assert out.getvalue() == "hello\\n"
    """
    import io
    buf = io.StringIO()
    old = sys.stdout
    sys.stdout = buf
    try:
        yield buf
    finally:
        sys.stdout = old


# ─────────────────────────────────────────────────────────────────────────────
# 5. Example test cases that demonstrate the utilities above
# ─────────────────────────────────────────────────────────────────────────────

class TestExamples(BaseTestCase):
    """
    Demonstration tests — run with: python -m unittest testutil.TestExamples
    """

    def setUp(self) -> None:
        self.data = [1, 2, 3, 4, 5]

    # ── Basic assertions ──────────────────────────────────────────────────────

    def test_basic_math(self) -> None:
        self.assertEqual(2 + 2, 4)
        self.assertApprox(1 / 3, 0.3333333, rel=1e-5)
        self.assertBetween(len(self.data), 1, 10)

    def test_raises(self) -> None:
        with self.assertRaises(ZeroDivisionError):
            _ = 1 / 0
        self.assertRaisesMessage(ValueError, "invalid literal",
                                 int, "not_a_number")

    # ── Parameterized ─────────────────────────────────────────────────────────

    @parametrize([(0, 0), (1, 1), (2, 4), (3, 9), (5, 25)])
    def test_square(self, n: int, expected: int) -> None:
        self.assertEqual(n * n, expected)

    # ── Temp files ────────────────────────────────────────────────────────────

    def test_tmp_file(self) -> None:
        path = self.tmp_file("hello\nworld\n", suffix=".txt")
        self.assertTrue(path.exists())
        self.assertEqual(path.read_text(), "hello\nworld\n")

    def test_tmp_json(self) -> None:
        data = {"name": "Alice", "scores": [10, 20, 30]}
        path = self.tmp_json(data)
        loaded = json.loads(path.read_text())
        self.assertDictEqual(loaded, data)

    # ── Mocking ───────────────────────────────────────────────────────────────

    def test_mock_response(self) -> None:
        resp = mock_response(200, json_data={"user": "Alice"})
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json(), {"user": "Alice"})
        self.assertTrue(resp.ok)

    def test_env_var(self) -> None:
        original = os.environ.get("TEST_KEY", "<unset>")
        with env_var("TEST_KEY", "testval"):
            self.assertEqual(os.environ["TEST_KEY"], "testval")
        # restored
        self.assertEqual(os.environ.get("TEST_KEY", "<unset>"), original)

    # ── Skip / expectedFailure ────────────────────────────────────────────────

    @unittest.skip("demonstrating skip decorator")
    def test_skipped(self) -> None:
        self.fail("should not run")

    @unittest.skipIf(sys.platform == "win32", "POSIX-only test")
    def test_posix_only(self) -> None:
        self.assertIn(os.sep, "/\\\\")

    @unittest.expectedFailure
    def test_known_broken(self) -> None:
        self.assertEqual(0.1 + 0.2, 0.3)  # float repr issue


# ─────────────────────────────────────────────────────────────────────────────
# Run
# ─────────────────────────────────────────────────────────────────────────────

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

For the pytest alternative — pytest (PyPI) offers plain assert instead of self.assert* methods, fixture injection via function parameters, parametrize via @pytest.mark.parametrize, richer failure diffs, plugin ecosystem (coverage, xdist, benchmark), and conftest.py for shared fixtures; unittest is stdlib with zero dependencies and is compatible with pytest as a runner — use pytest for new projects both for its ergonomics and for the plugin ecosystem; use unittest when you cannot add test dependencies, when working in an environment that already uses unittest test runners (like Django’s manage.py test), or when you want stdlib-only code. For the hypothesis alternative — hypothesis (PyPI) performs property-based testing by automatically generating diverse test inputs from strategy specifications (@given(st.lists(st.integers()))), shrinking failures to minimal examples; unittest.subTest provides parameterized assertions over manually defined cases — use hypothesis for algebraic properties, parser round-trips, serialization correctness, and any function where you can state a property that must hold for all inputs; use unittest.subTest for explicit example-based tests where you control the exact inputs. The Claude Skills 360 bundle includes unittest skill sets covering BaseTestCase with assertApprox/assertBetween/assertDictSubset/assertRaisesMessage, tmp_file()/tmp_dir()/tmp_json() auto-cleanup helpers, parametrize() subTest decorator, mock_response()/mock_open_file()/make_mock_db() mock factories, env_var()/capture_stdout() context helpers, and complete TestExamples demonstrating all patterns. Start with the free tier to try Python unit testing patterns and unittest pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free