freshcrate
Home > Frameworks > pyhamcrest

pyhamcrest

Hamcrest framework for matcher objects

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

Release History

VersionChangesUrgencyDate
2.1.0Imported from PyPI (2.1.0)Low4/21/2026
V2.1.0## 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:Low10/22/2023
V2.0.3A whole pile of little updates and testing fixesLow12/12/2021
V2.0.2Release V2.0.2Low3/2/2020
V2.0.1Release V2.0.1Low2/26/2020
V2.0.0Release V2.0.0Low1/16/2020
V1.10.1Release V1.10.1Low1/15/2020
V1.10.0Release V1.10.0Low1/7/2020
V1.9.0# Changes - Drop formal support for 2.x < 2.7 - Drop formal support for 3.x < 3.4 # Fixes - Fix #62 - Return result of a deferred call Low10/1/2016
V1.8.0Release with V1.8.0 tag so that RTD will version it. Low11/12/2013
1.8.0Release PyHamcrest v1.8.0 Low11/12/2013

Dependencies & License Audit

Loading dependencies...

Similar Packages

pytest-subtestsunittest subTest() support and subtests fixture0.15.0
pyfakefsImplements a fake file system that mocks the Python file system modules.6.2.0
time-machineTravel through time in your tests.3.2.0
schemathesisProperty-based testing framework for Open API and GraphQL based apps4.15.2
robotframeworkGeneric automation framework for acceptance testing and robotic process automation (RPA)7.4.2