Claude Code for Nox: Python Test Automation — Claude Skills 360 Blog
Blog / AI / Claude Code for Nox: Python Test Automation
AI

Claude Code for Nox: Python Test Automation

Published: December 13, 2027
Read time: 5 min read
By: Claude Skills 360

Nox automates testing across Python versions and environments. pip install nox. Create noxfile.py. import nox. Session: @nox.session(python=["3.11","3.12"]) def tests(session): session.install("-e",".[dev]"); session.run("pytest", *session.posargs). Install: session.install("pytest", "pytest-cov"). Run: session.run("ruff", "check", "."). Posargs: nox -- --tb=short -k auth passes through to pytest. Parametrize: @nox.session @nox.parametrize("db", ["postgres","sqlite"]). Reuse venv: @nox.session(reuse_venv=True). Tag: @nox.session(tags=["lint"]). Notify: session.notify("docs"). nox.options.sessions = ["tests", "lint"] — default sessions. nox.options.reuse_existing_virtualenvs = True. Run: nox -s tests. List: nox --list. Skip install: nox -s tests --no-install. Specific py: nox -s tests-3.12. Select tags: nox -t lint. Skip venv: nox -s tests --no-venv. Error on POSIX: session.error("message") — marks session as failed. Notify: session.notify("other_session") — runs another session after this one. Chdir: session.chdir("subdir"). Log: session.log("msg"). Warn: session.warn("msg"). External: session.run("make", "docs", external=True) — allow non-Python executables. nox --no-error-on-missing-interpreters — skip missing Python versions in CI. nox --envdir /tmp/nox-envs — custom venv location. Claude Code generates Nox test matrix configurations, lint pipelines, and CI-ready noxfiles.

CLAUDE.md for Nox

## Nox Stack
- Version: nox >= 2024.0
- Config: noxfile.py at project root
- Session: @nox.session(python=["3.11","3.12"], reuse_venv=True)
- Install: session.install("pkg") | session.install("-e", ".[dev]")
- Run: session.run("pytest", "--cov", *session.posargs)
- posargs: nox -s tests -- --tb=short -k auth → passed as session.posargs
- Tags: @nox.session(tags=["lint"]) | nox -t lint
- CI: nox -s lint tests | --no-error-on-missing-interpreters

Nox Test Automation Pipeline

# noxfile.py — test and lint automation with Nox
# Usage:
#   nox          — run default sessions (lint + tests)
#   nox -s tests — run just tests
#   nox -s lint  — run just linting
#   nox -s tests -- -k auth --tb=short  — pass args to pytest
#   nox -l       — list all sessions
from __future__ import annotations

import nox

# ── Global options ────────────────────────────────────────────────────────────

nox.options.sessions         = ["lint", "type_check", "tests"]
nox.options.reuse_existing_virtualenvs = True
nox.options.error_on_missing_interpreters = False

PYTHON_VERSIONS = ["3.11", "3.12"]
SOURCES         = ["src", "tests", "noxfile.py"]


# ── 1. Linting and formatting ─────────────────────────────────────────────────

@nox.session(python="3.12", tags=["lint"])
def lint(session: nox.Session) -> None:
    """
    Run Ruff linter and formatter check.
    Fix issues automatically with: nox -s lint -- --fix
    """
    session.install("ruff")
    fix = "--fix" in session.posargs
    args = ["--fix"] if fix else []
    session.run("ruff", "check", *args, *SOURCES, external=False)
    session.run("ruff", "format", "--check" if not fix else "", *SOURCES)


@nox.session(python="3.12", tags=["lint"])
def format_check(session: nox.Session) -> None:
    """Check code formatting without modifying files."""
    session.install("ruff", "black", "isort")
    session.run("black", "--check", "--diff", *SOURCES)
    session.run("isort", "--check-only", "--diff", *SOURCES)


@nox.session(python="3.12", tags=["lint", "security"])
def security(session: nox.Session) -> None:
    """Run Bandit security linter and pip-audit for dependency CVEs."""
    session.install("bandit[toml]", "pip-audit")
    session.run("bandit", "-r", "src", "-c", "pyproject.toml", "-q")
    session.run("pip-audit", "--strict")


# ── 2. Type checking ──────────────────────────────────────────────────────────

@nox.session(python="3.12", tags=["lint", "typecheck"])
def type_check(session: nox.Session) -> None:
    """
    Run mypy static type checking.
    Install the project in editable mode so mypy can find all modules.
    """
    session.install("-e", ".[dev]")
    session.install("mypy", "types-requests", "types-PyYAML")
    session.run(
        "mypy",
        "src",
        "--ignore-missing-imports",
        "--strict",
        "--show-error-codes",
        *session.posargs,
    )


# ── 3. Tests ──────────────────────────────────────────────────────────────────

@nox.session(python=PYTHON_VERSIONS, tags=["test"])
def tests(session: nox.Session) -> None:
    """
    Run the full test suite with coverage.
    Pass pytest args after '--': nox -s tests -- -k auth --tb=short
    """
    session.install("-e", ".[dev]")
    session.install("pytest", "pytest-cov", "pytest-xdist", "pytest-timeout")
    session.run(
        "pytest",
        "tests/",
        "--cov=src",
        "--cov-report=term-missing",
        "--cov-report=xml:coverage.xml",
        "--cov-fail-under=80",
        "-q",
        "--timeout=60",
        *session.posargs,
    )


@nox.session(python="3.12", tags=["test"])
def tests_fast(session: nox.Session) -> None:
    """
    Fast unit-only test run (skip integration and slow tests).
    Use for rapid development feedback.
    """
    session.install("-e", ".[dev]")
    session.install("pytest", "pytest-xdist")
    session.run(
        "pytest",
        "tests/unit/",
        "-x",                     # stop on first failure
        "-q",
        "-n", "auto",             # parallel with pytest-xdist
        "-m", "not slow",
        *session.posargs,
    )


@nox.session(python="3.12", tags=["test"])
def integration(session: nox.Session) -> None:
    """
    Integration tests that require external services (DB, cache, etc.).
    Typically run only in CI or when INTEGRATION=1.
    """
    import os
    if not os.environ.get("INTEGRATION"):
        session.skip("Set INTEGRATION=1 to run integration tests")
    session.install("-e", ".[dev]")
    session.install("pytest", "pytest-asyncio")
    session.run(
        "pytest",
        "tests/integration/",
        "-v",
        "--timeout=120",
        *session.posargs,
    )


# ── 4. Documentation ──────────────────────────────────────────────────────────

@nox.session(python="3.12", tags=["docs"])
def docs(session: nox.Session) -> None:
    """Build Sphinx or MkDocs documentation."""
    session.install("-e", ".[docs]")
    session.run("mkdocs", "build", "--strict", external=True)


@nox.session(python="3.12", tags=["docs"], reuse_venv=True)
def docs_serve(session: nox.Session) -> None:
    """Serve documentation locally for development."""
    session.install("-e", ".[docs]")
    session.run("mkdocs", "serve", external=True)


# ── 5. Database / migration sessions ─────────────────────────────────────────

@nox.parametrize("db", ["postgresql", "sqlite"])
@nox.session(python="3.12", tags=["test"])
def tests_db(session: nox.Session, db: str) -> None:
    """
    Run tests against multiple database backends.
    Matrix: 3.12 × {postgresql, sqlite}
    """
    session.install("-e", ".[dev]")
    session.install("pytest", "pytest-cov")
    session.env["DATABASE_BACKEND"] = db
    if db == "postgresql":
        if not session.env.get("DATABASE_URL"):
            session.skip("Set DATABASE_URL=postgresql://... to run Postgres tests")
    session.run(
        "pytest",
        "tests/",
        "-m", f"db",
        "-q",
        f"--db={db}",
        *session.posargs,
    )


# ── 6. Coverage report ────────────────────────────────────────────────────────

@nox.session(python="3.12", tags=["coverage"])
def coverage(session: nox.Session) -> None:
    """
    Generate HTML coverage report from a previous test run.
    Requires tests to have written .coverage file first.
    """
    session.install("coverage[toml]")
    session.run("coverage", "html", "--fail-under=80")
    session.run("coverage", "report", "--show-missing")
    session.log("HTML coverage report generated in htmlcov/")


# ── 7. Release / packaging ────────────────────────────────────────────────────

@nox.session(python="3.12", tags=["release"])
def build(session: nox.Session) -> None:
    """Build source and wheel distributions."""
    session.install("build", "twine")
    session.run("python", "-m", "build")
    session.run("twine", "check", "dist/*")
    session.log("Artifacts in dist/")


@nox.session(python="3.12", tags=["release"])
def publish(session: nox.Session) -> None:
    """
    Upload to PyPI. Requires TWINE_TOKEN env var.
    Run with: nox -s publish
    """
    import os
    if not os.environ.get("TWINE_TOKEN"):
        session.error("Set TWINE_TOKEN env var to publish")
    session.install("twine")
    session.run("twine", "upload", "dist/*")


# ── 8. Dev environment bootstrap ─────────────────────────────────────────────

@nox.session(python="3.12", reuse_venv=True, tags=["dev"])
def dev(session: nox.Session) -> None:
    """
    Set up a complete local dev environment.
    Run once: nox -s dev
    Then activate: source .nox/dev/bin/activate
    """
    session.install("-e", ".[dev]")
    session.install(
        "pre-commit",
        "ipython",
        "ipdb",
    )
    session.run("pre-commit", "install", external=True)
    session.log("Dev environment ready. Run: source .nox/dev/bin/activate")

For the Makefile alternative — Makefiles require shell syntax and don’t handle Python virtualenv creation, dependency isolation between tasks, or cross-platform Windows support, while a noxfile.py session uses session.install() to create an isolated venv per task so nox -s lint never conflicts with nox -s tests dependencies, and @nox.session(python=["3.11","3.12"]) generates two parameterized sessions from one definition that CI can run in parallel with nox -s tests-3.11 tests-3.12. For the tox alternative — tox uses an .ini configuration format with limited Python expressiveness while Nox sessions are regular Python functions where if not os.environ.get("INTEGRATION"): session.skip(...) conditionally skips integration tests, @nox.parametrize("db", ["postgresql","sqlite"]) generates a cross-product matrix the way pytest.mark.parametrize does, and session.posargs forwards nox -- -k auth directly to pytest without escaping shell arguments. The Claude Skills 360 bundle includes Nox skill sets covering session decorator with python and tags, session.install editable project, session.run with posargs, lint/type-check/tests/docs session patterns, @nox.parametrize matrix testing, skip and error conditional logic, reuse_venv for fast iteration, coverage HTML report, build and publish sessions, and dev bootstrap session. Start with the free tier to try test automation 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