skillfed

PyHamcrest

Hamcrest framework for matcher objects

pyhamcrest v2.1.0 6.4M downloads/30d#1,916 on PyPI804
Permissive license BSD License Copyright 2020 hamcrest.org All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are… (full text in the JSON record) AGING released

What it is and what it does

PyHamcrest is a matcher framework that lets you write test assertions as declarative rules rather than imperative comparisons. Instead of writing brittle equality checks, you compose matchers to express exactly what aspect of an object you're testing and to what precision. This helps tests stay readable and maintainable: they fail when behavior deviates from expectations but pass through minor, unrelated changes.

The library comes with matchers for objects, numbers, text, sequences, dictionaries, and logical combinations. You can also write custom matchers by subclassing BaseMatcher. PyHamcrest integrates naturally with unittest and other test frameworks. It has no runtime dependencies and supports Python 3.6 and later across CPython, PyPy, and Jython.

Use it for:

  • Write unit tests with readable, composable assertions that fail precisely when behavior changes.
  • Validate UI or form input by matching against patterns for strings and regular expressions.
  • Filter or validate data structures using reusable matcher logic across multiple test cases.
  • Create custom matchers for domain-specific validation rules to eliminate test code duplication.
  • Build flexible test suites that distinguish between critical behavior changes and minor implementation details.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

PyHamcrest provides a framework for writing declarative matcher objects that enable flexible, readable assertions in unit tests and data validation scenarios.

Yes. PyHamcrest is stable (Production/Stable status), widely used (6393524 monthly downloads), has no dependencies, and carries a permissive BSD License. The aging maintenance status (last release 2023-10-22) is not a blocker for a mature, focused library—it indicates stability rather than abandonment. Install it if you want more readable, maintainable test assertions.

Install

pyhamcrest on PyPI

pip

pip install pyhamcrest

uv

uv add pyhamcrest

poetry

poetry add pyhamcrest

Installing PyHamcrest

Before you install

Low install friction with no runtime dependencies. Maintenance status is aging—last release was 2023-10-22 and last commit 2026-02-12, but the repository remains active and not archived.

License in practice

BSD License (permissive). You can use, modify, and distribute PyHamcrest freely in commercial and private projects, provided you retain the copyright notice and disclaimer.

Quickstart

pip install PyHamcrest

from PyHamcrest import assert_that, equal_to
import unittest

class MyTest(unittest.TestCase):
    def test_example(self):
        assert_that(5, equal_to(5))

Verify before relying

  • Whether the import path is 'hamcrest' or 'PyHamcrest' in current versions

Package facts

License BSD License Copyright 2020 hamcrest.org All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are… (full text in the JSON record) (permissive)
Python support supports the current Python release (>=3.6)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance aging — 1,027 days since the last release
Last repo commit
First released
Downloads 6,393,524/month — #1,916 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pyhamcrest-2.1.0-py3-none-any.whl

Keywords: hamcrest, matchers, pyunit, test, testing, unit, unittest, unittesting

Development Status :: 5 - Production/StableEnvironment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: JythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Software DevelopmentTopic :: Software Development :: Quality AssuranceTopic :: Software Development :: Testing

Tags

matcher framework for testingflexible test assertionshamcrest matchers pythonreadable unit test assertionscomposable test matchersdata validation matcherstest assertion library
testingmatchersassertions

More Software Development packages