Outline updated November 2026

Intermediate14 hours delivered over 2 daysLive, online

Request this course

Python Unit Testing with pytest and unittest

Class Duration

14 hours of live training delivered over 2 days.

Student Prerequisites

  • Working knowledge of Python (writing functions, classes, and modules)
  • Comfortable running scripts and installing packages from the command line
  • Experience with unit testing in another language is helpful but not required

Target Audience

  • Python developers who want a disciplined, professional approach to unit testing
  • Teams standardizing on pytest, maintaining existing unittest suites, or migrating from one to the other

Description

This two-day course teaches Python unit testing with pytest and with unittest, the standard library framework many codebases still use. It opens with test-driven development and the anatomy of a good test. The pytest modules cover fixtures, parametrization, markers, subtests, native TOML configuration, and strict mode. Students also run unittest suites under pytest, mock with unittest.mock and monkeypatch, and write property-based tests with Hypothesis. The course closes with plugins, code coverage, static analysis, and tests in CI/CD pipelines.

Learning Outcomes

  • Apply the principles of Test-Driven Development, including the Three Laws, Red-Green-Refactor, and the FIRST rules.
  • Write, organize, and run tests with pytest, using fixtures, parametrization, markers, and subtests.
  • Configure pytest with native TOML files and strict mode.
  • Write unittest test cases and run unittest suites under pytest.
  • Assert on return values, exceptions, exception groups, and warnings, and write custom assertion helpers.
  • Isolate code under test with unittest.mock, pytest-mock, and monkeypatch.
  • Find edge cases with property-based tests written in Hypothesis.
  • Extend pytest with plugins for coverage, parallel execution, and async code, and with local hooks.
  • Measure code coverage and run tests and static analysis in CI/CD pipelines.

Training Materials

All students receive comprehensive courseware covering all topics in the course. Courseware is distributed via GitHub in the form of documentation and extensive code samples. Students practice the topics covered through challenging hands-on lab exercises.

Software Requirements

Students will need a free, personal GitHub account to access the courseware. Students will need Python, the ability to install Python packages, a modern web browser, Visual Studio Code, and the ability to install Visual Studio Code Extensions. The course is taught on Python 3.15 with pytest 9.

Training Topics

Overview of Unit Testing and TDD

  • What is test-driven development (TDD)?
  • Benefits and challenges of TDD
  • The three laws of TDD
  • Red, green, refactor
  • Clean tests
  • One concept per test
  • The FIRST rules
    • Fast
    • Independent
    • Repeatable
    • Self-validating
    • Timely
  • Arrange, act, assert

Kinds of Testing

  • Unit tests
  • Integration tests
  • End-to-end tests
  • Automated vs. manual testing
  • Mocks, fakes, stubs, and spies

Python Unit Testing Frameworks

  • unittest vs. pytest
  • How pytest runs unittest tests
  • The src layout and test folders
  • Import modes

pytest Fundamentals

  • Installing pytest with uv
  • Test discovery and naming conventions
  • Running tests
    • Console
    • Visual Studio Code
  • Selecting tests by keyword and node ID
  • Last-failed and fail-fast runs
  • Plain assert and assertion rewriting
  • Asserting exceptions with pytest.raises
  • Exception groups with pytest.RaisesGroup
  • Approximate comparisons with pytest.approx
  • Asserting warnings with pytest.warns
  • Custom assertion helpers

Configuring pytest

  • Native TOML configuration
  • [tool.pytest] in pyproject.toml
  • The pytest.toml file
  • Strict mode
  • Strict markers and strict xfail
  • The skip, skipif, and xfail markers
  • Custom markers
  • Subtests

Fixtures

  • What is a fixture?
  • Requesting fixtures
  • Sharing fixtures with conftest.py
  • Fixture scope
  • Setup and teardown with yield
  • Fixture factories
  • Parametrized fixtures
  • Built-in fixtures such as tmp_path and caplog

Parametrized Tests

  • @pytest.mark.parametrize
  • Test IDs and readable output
  • Stacking and indirect parametrization

Unit Testing with unittest

  • TestCase classes and test methods
  • setUp, setUpClass, and setUpModule
  • Cleanup with addCleanup
  • unittest assertion methods
  • assertRaises, assertLogs, and assertWarns
  • Data-driven tests with subTest
  • Test discovery and test suites
  • The unittest command line
  • Shared base test classes
  • Running unittest suites under pytest
  • Migrating incrementally

Mocking

  • What is mocking?
  • Why Mock?
  • Mock and MagicMock
  • patch and autospec
  • Mocking with pytest-mock
  • Patching environment and attributes with monkeypatch
  • Mocking file I/O
  • Mocking network calls
  • Mocking coroutines with AsyncMock
  • Avoiding over-mocking

Property-Based Testing with Hypothesis

  • Example-based vs. property-based tests
  • Writing properties with @given
  • Built-in and composite strategies
  • Shrinking and reproducing failures
  • Settings, profiles, and the example database
  • Stateful testing with rule-based state machines
  • Hypothesis with pytest and unittest

pytest Plugins

  • Finding and evaluating plugins
  • pytest-cov for coverage
  • pytest-xdist for parallel runs
  • pytest-asyncio for async tests
  • pytest-randomly for test order independence
  • Local plugins and hooks in conftest.py

Code Coverage

  • What is code coverage?
  • Why measure coverage?
  • Line and branch coverage with coverage.py
  • Terminal, HTML, and XML coverage reports
  • Coverage thresholds
  • Limits of coverage

CI/CD and Testing

  • Continuous integration, delivery, and deployment
  • Unit testing tasks
    • Pull request pipeline
    • Build pipeline
  • Test result reporting with JUnit XML
  • CI/CD platforms
    • Azure DevOps
    • GitHub Actions
    • GitLab CI
    • Jenkins
  • Code analysis
    • Linting with Ruff
    • Type checking with mypy or Pyright
    • Static analysis

Private team training

Bring this course to your team

Tell us about your team and we will tailor this outline to your tools, stack, and schedule. Courses can also be combined into a longer program.

  1. Tell us what you needYour tools, stack, team size, and timeline.
  2. We send a proposalA course or program, an outline adjusted to your team, and possible dates.
  3. Your team trains liveOnline or on-site, with courseware for every student.

Prefer to talk? Call +1 434-509-6890 or email help@cloudcontraptions.com.

We reply within 1 business day.

Training partner?