Integration with PyUnit and Other Libraries

assert_that

hamcrest.core.assert_that.assert_that(actual, matcher[, reason])

Asserts that actual value satisfies matcher. (Can also assert plain boolean condition.)

Parameters:
  • actual – The object to evaluate as the actual value.
  • matcher – The matcher to satisfy as the expected condition.
  • reason – Optional explanation to include in failure description.

assert_that passes the actual value to the matcher for evaluation. If the matcher is not satisfied, an exception is thrown describing the mismatch.

assert_that is designed to integrate well with PyUnit and other unit testing frameworks. The exception raised for an unmet assertion is an AssertionError, which PyUnit reports as a test failure.

With a different set of parameters, assert_that can also verify a boolean condition:

hamcrest.core.assert_that.assert_that(assertion[, reason])
Parameters:
  • assertion – Boolean condition to verify.
  • reason – Optional explanation to include in failure description.

This is equivalent to the assertTrue method of unittest.TestCase, but offers greater flexibility in test writing by being a standalone function.

match_equality

hamcrest.library.integration.match_equality.match_equality(matcher: hamcrest.core.matcher.Matcher) → hamcrest.library.integration.match_equality.EqualityWrapper

Wraps a matcher to define equality in terms of satisfying the matcher.

match_equality allows Hamcrest matchers to be used in libraries that are not Hamcrest-aware. They might use the equality operator:

assert match_equality(matcher) == object

Or they might provide a method that uses equality for its test:

library.method_that_tests_eq(match_equality(matcher))

One concrete example is integrating with the assert_called_with methods in Michael Foord’s mock library.