File: tutorial.rst

package info (click to toggle)
pyhamcrest 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: python: 4,081; makefile: 114; sh: 15
file content (253 lines) | stat: -rw-r--r-- 10,946 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
PyHamcrest Tutorial
===================

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.


My first PyHamcrest test
------------------------

We'll start by writing a very simple PyUnit test, but instead of using PyUnit's
:py:meth:`~unittest.TestCase.assertEqual` method, we'll use PyHamcrest's
:py:func:`~hamcrest.core.matcher_assert.assert_that` construct and the standard
set of matchers::

    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 :py:func:`~hamcrest.core.matcher_assert.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::

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

As a convenience, :py:func:`~hamcrest.core.matcher_assert.assert_that` can also
be used to verify a boolean condition::

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

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


Asserting Exceptions
--------------------

There's a utility function and matcher available to help you test that
your code has the expected behavior in situations where it should raise
an exception.  The :py:func:`~hamcrest.core.core.raises.calling`
function wraps a callable, and then allows you to set arguments to be
used in a call to the wrapped callable.  This, together with the
:py:func:`~hamcrest.core.core.raises.raises` matcher lets you assert
that calling a method with certain arguments causes an exception to be
thrown. It is also possible to provide a regular expression pattern to
the :py:func:`~hamcrest.core.core.raises.raises` matcher allowing you
assure that the right issue was found::

    assert_that(calling(parse, bad_data), raises(ValueError))

    assert_that(calling(translate).with_args(curse_words), raises(LanguageError, "\w+very naughty"))

    assert_that(broken_function, raises(Exception))

    # This will fail and complain that 23 is not callable
    # assert_that(23, raises(IOError))


Asserting exceptions from async methods
---------------------------------------

An async method does not directly return the result or raise an exception but
instead returns a Future-object that represent the async operation that can
later be resolved with the `await` keyword. The
:py:func:`~hamcrest.core.core.future.resolved` utility function can be used to
wait for a future to be done but without retrieving the value or raising the
exception. The :py:func:`~hamcrest.core.core.future.future_raising` matcher can
be used with any future object but combined lets you assert that calling some
async method, and waiting for the result, causes an exception to be raised.

This is best used together with an async test runner like IsolatedAsyncioTestCase or pytest-asyncio::

    async def parse(input: str):
        ...

    class Test(unittest.IsolatedAsyncioTestCase):

        async def testParse(self):
            future = parse("some bad data")
            assert_that(await resolved(future), future_raising(ValueError))

But it's possible to use with an async unware runner by explicitly running the event loop in the test::

    class Test(unittest.TestCase):
        def test_parse(self):
            async def test():
                future = parse("some bad data")
                assert_that(await resolved(future), future_raising(ValueError))

            asyncio.get_event_loop().run_until_complete(test())


Predefined matchers
-------------------

PyHamcrest comes with a library of useful matchers:

* Object

  * :py:func:`~hamcrest.core.core.isequal.equal_to` - match equal object
  * :py:func:`~hamcrest.library.object.haslength.has_length` - match
    ``len(item)``
  * :py:func:`~hamcrest.library.object.hasproperty.has_property` - match value
    of property with given name
  * :py:func:`~hamcrest.library.object.hasproperty.has_properties` - match an
     object that has all of the given properties.
  * :py:func:`~hamcrest.library.object.hasstring.has_string` - match
    ``str(item)``
  * :py:func:`~hamcrest.core.core.isinstanceof.instance_of` - match object type
  * :py:func:`~hamcrest.core.core.isnone.none`,
    :py:func:`~hamcrest.core.core.isnone.not_none` - match ``None``, or not
    ``None``
  * :py:func:`~hamcrest.core.core.issame.same_instance` - match same object
  * :py:func:`~hamcrest.core.core.raises.calling`,
    :py:func:`~hamcrest.core.core.raises.raises` - wrap a method call and assert
    that it raises an exception

* Number

  * :py:func:`~hamcrest.library.number.iscloseto.close_to` - match number close
    to a given value
  * :py:func:`~hamcrest.library.number.ordering_comparison.greater_than`,
    :py:func:`~hamcrest.library.number.ordering_comparison.greater_than_or_equal_to`,
    :py:func:`~hamcrest.library.number.ordering_comparison.less_than`,
    :py:func:`~hamcrest.library.number.ordering_comparison.less_than_or_equal_to`
    - match numeric ordering

* Text

  * :py:func:`~hamcrest.library.text.stringcontains.contains_string` - match
    part of a string
  * :py:func:`~hamcrest.library.text.stringendswith.ends_with` - match the end
    of a string
  * :py:func:`~hamcrest.library.text.isequal_ignoring_case.equal_to_ignoring_case`
    - match the complete string but ignore case
  * :py:func:`~hamcrest.library.text.isequal_ignoring_whitespace.equal_to_ignoring_whitespace`
    - match the complete string but ignore extra whitespace
  * :py:func:`~hamcrest.library.text.stringstartswith.starts_with` - match the
    beginning of a string
  * :py:func:`~hamcrest.library.text.stringcontainsinorder.string_contains_in_order`
    - match parts of a string, in relative order

* Logical

  * :py:func:`~hamcrest.core.core.allof.all_of` - ``and`` together all matchers
  * :py:func:`~hamcrest.core.core.anyof.any_of` - ``or`` together all matchers
  * :py:func:`~hamcrest.core.core.isanything.anything` - match anything, useful
    in composite matchers when you don't care about a particular value
  * :py:func:`~hamcrest.core.core.isnot.is_not` - negate the matcher
  * :py:func:`~hamcrest.core.core.isnot.not_` - alias of
    :py:func:`~hamcrest.core.core.isnot.is_not` for better readability of negations.

* Sequence

  * :py:func:`~hamcrest.library.collection.issequence_containinginorder.contains`
    - exactly match the entire sequence
  * :py:func:`~hamcrest.library.collection.issequence_containinginanyorder.contains_inanyorder`
    - match the entire sequence, but in any order
  * :py:func:`~hamcrest.library.collection.issequence_containing.has_item` -
    match if given item appears in the sequence
  * :py:func:`~hamcrest.library.collection.issequence_containing.has_items` -
    match if all given items appear in the list, in any order
  * :py:func:`~hamcrest.library.collection.isin.is_in` - match if item appears
    in the given sequence
  * :py:func:`~hamcrest.library.collection.issequence_onlycontaining.only_contains`
    - match if sequence's items appear in given list
  * :py:func:`~hamcrest.library.collection.is_empty.empty`
    - match if the sequence is empty

* Dictionary

  * :py:func:`~hamcrest.library.collection.isdict_containingentries.has_entries`
    - match dictionary with list of key-value pairs
  * :py:func:`~hamcrest.library.collection.isdict_containing.has_entry` - match
    dictionary containing a key-value pair
  * :py:func:`~hamcrest.library.collection.isdict_containingkey.has_key` -
    match dictionary with a key
  * :py:func:`~hamcrest.library.collection.isdict_containingvalue.has_value` -
    match dictionary with a value

* Decorator

  * :py:func:`~hamcrest.core.core.described_as.described_as` - give the matcher
    a custom failure description
  * :py:func:`~hamcrest.core.core.is_.is_` - decorator to improve readability -
    see :ref:`sugar`, below

The arguments for many of these matchers accept not just a matching value, but
another matcher, so matchers can be composed for greater flexibility. For
example, ``only_contains(less_than(5))`` will match any sequence where every
item is less than 5.


.. _sugar:

Syntactic sugar
---------------

PyHamcrest strives to make your tests as readable as possible. For example, the
:py:func:`~hamcrest.core.core.is_.is_` matcher is a wrapper that doesn't add
any extra behavior to the underlying matcher. The following assertions are all
equivalent::

    assert_that(theBiscuit, equal_to(myBiscuit))
    assert_that(theBiscuit, is_(equal_to(myBiscuit)))
    assert_that(theBiscuit, is_(myBiscuit))

The last form is allowed since ``is_(value)`` wraps most non-matcher arguments
with :py:func:`~hamcrest.core.core.isequal.equal_to`. But if the argument is a
type, it is wrapped with
:py:func:`~hamcrest.core.core.isinstanceof.instance_of`, so the following are
also equivalent::

    assert_that(theBiscuit, instance_of(Biscuit))
    assert_that(theBiscuit, is_(instance_of(Biscuit)))
    assert_that(theBiscuit, is_(Biscuit))

(Note that PyHamcrest's ``is_`` matcher is unrelated to Python's ``is``
operator. The matcher for object identity is
:py:func:`~hamcrest.core.core.issame.same_instance`.)