# pyhamcrest

> Hamcrest framework for matcher objects

- **URL**: https://www.freshcrate.ai/projects/pyhamcrest
- **Author**: Simon Brunning
- **Category**: Frameworks
- **Latest version**: `2.1.0` (2026-04-21)
- **License**: non-standard
- **Source**: https://github.com/hamcrest/PyHamcrest/blob/main/CHANGELOG.rst
- **Homepage**: https://pypi.org/project/PyHamcrest/
- **Language**: Python
- **GitHub**: 804 stars, 116 forks
- **Registry**: pypi (`pyhamcrest`)
- **Tags**: `hamcrest`, `matchers`, `pypi`, `pyunit`, `test`, `testing`, `unit`, `unittest`, `unittesting`

## Description

PyHamcrest
==========

| |docs| |status| |version| |downloads|

.. |docs| image:: https://readthedocs.org/projects/pyhamcrest/badge/?version=latest
    :target: https://pyhamcrest.readthedocs.io/en/latest/?badge=latest
    :alt: Documentation Status

.. |status| image:: https://github.com/hamcrest/PyHamcrest/workflows/CI/badge.svg
    :alt: CI Build Status
    :target: https://github.com/hamcrest/PyHamcrest/actions?query=workflow%3ACI

.. |version| image:: http://img.shields.io/pypi/v/PyHamcrest.svg?style=flat
    :alt: PyPI Package latest release
    :target: https://pypi.python.org/pypi/PyHamcrest

.. |downloads| image:: http://img.shields.io/pypi/dm/PyHamcrest.svg?style=flat
    :alt: PyPI Package monthly downloads
    :target: https://pypi.python.org/pypi/PyHamcrest


Introduction
============

PyHamcrest is a framework for writing matcher objects, allowing you to
declaratively define "match" rules. There are a number of situations where
matchers are invaluable, such as UI validation, or data filtering, but it is in
the area of writing flexible tests that matchers are most commonly used. This
tutorial shows you how to use PyHamcrest for unit testing.

When writing tests it is sometimes difficult to get the balance right between
overspecifying the test (and making it brittle to changes), and not specifying
enough (making the test less valuable since it continues to pass even when the
thing being tested is broken). Having a tool that allows you to pick out
precisely the aspect under test and describe the values it should have, to a
controlled level of precision, helps greatly in writing tests that are "just
right." Such tests fail when the behavior of the aspect under test deviates
from the expected behavior, yet continue to pass when minor, unrelated changes
to the behaviour are made.

Installation
============

Hamcrest can be installed using the usual Python packaging tools. It depends on
distribute, but as long as you have a network connection when you install, the
installation process will take care of that for you.

For example:

.. code::

 pip install PyHamcrest

My first PyHamcrest test
========================

We'll start by writing a very simple PyUnit test, but instead of using PyUnit's
``assertEqual`` method, we'll use PyHamcrest's ``assert_that`` construct and
the standard set of matchers:

.. code:: python

 from hamcrest import assert_that, equal_to
 import unittest


 class BiscuitTest(unittest.TestCase):
     def testEquals(self):
         theBiscuit = Biscuit("Ginger")
         myBiscuit = Biscuit("Ginger")
         assert_that(theBiscuit, equal_to(myBiscuit))


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

The ``assert_that`` function is a stylized sentence for making a test
assertion. In this example, the subject of the assertion is the object
``theBiscuit``, which is the first method parameter. The second method
parameter is a matcher for ``Biscuit`` objects, here a matcher that checks one
object is equal to another using the Python ``==`` operator. The test passes
since the ``Biscuit`` class defines an ``__eq__`` method.

If you have more than one assertion in your test you can include an identifier
for the tested value in the assertion:

.. code:: python

 assert_that(theBiscuit.getChocolateChipCount(), equal_to(10), "chocolate chips")
 assert_that(theBiscuit.getHazelnutCount(), equal_to(3), "hazelnuts")

As a convenience, assert_that can also be used to verify a boolean condition:

.. code:: python

 assert_that(theBiscuit.isCooked(), "cooked")

This is equivalent to the ``assert_`` method of unittest.TestCase, but because
it's a standalone function, it offers greater flexibility in test writing.


Predefined matchers
===================

PyHamcrest comes with a library of useful matchers:

* Object

  * ``equal_to`` - match equal object
  * ``has_length`` - match ``len()``
  * ``has_property`` - match value of property with given name
  * ``has_properties`` - match an object that has all of the given properties.
  * ``has_string`` - match ``str()``
  * ``instance_of`` - match object type
  * ``none``, ``not_none`` - match ``None``, or not ``None``
  * ``same_instance`` - match same object
  * ``calling, raises`` - wrap a method call and assert that it raises an exception

* Number

  * ``close_to`` - match number close to a given value
  * ``greater_than``, ``greater_than_or_equal_to``, ``less_than``,
    ``less_than_or_equal_to`` - match numeric ordering

* Text

  * ``contains_string`` - match part of a string
  * ``ends_with`` - match the end of a string
  * ``equal_to_ignoring_case`` - match the complete string but ignore case
  * ``equal_to_ignoring_whitespace`` - match the complete string but ignore extra whitespace
  * ``matches_regexp`` - match a regular expression in a string
  * ``starts_with`` - match the beginning of a string
  * ``string_contains_in_order`` - match parts of a string, in relative order

* Logical

  * ``all_of`` - ``and`` together all matchers

## Recent releases

| Version | Date | Urgency | Changes |
| --- | --- | --- | --- |
| `2.1.0` | 2026-04-21 | Low | Imported from PyPI (2.1.0) |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |
| `V2.1.0` | 2023-10-22 | Low | ## What's Changed * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/208 * deprecated numpy alias by @aekoroglu in https://github.com/hamcrest/PyHamcrest/pull/218 * [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/hamcrest/PyHamcrest/pull/219 * Fix breakages due to Mypy chages by @brunns in https://github.com/hamcrest/PyHamcrest/pull/221 * Add explicit installation instruction example by @rgalanakis in https: |

## Citation

- HTML: https://www.freshcrate.ai/projects/pyhamcrest
- Markdown: https://www.freshcrate.ai/projects/pyhamcrest.md
- Dependencies JSON: https://www.freshcrate.ai/api/projects/pyhamcrest/deps

_Generated by freshcrate.ai. Indexes pypi releases for AI-agent ecosystem packages._
