Debian Codegraph @

hypothesis-python/docs/tutorial/adding-notes.rst

raw
1 Adding notes
2 ============
3
4 When a test fails, Hypothesis will normally print output that looks like this:
5
6 .. code::
7
8 Falsifying example: test_a_thing(x=1, y="foo")
9
10 Sometimes you want to add some additional information to a failure, such as the output of some intermediate step in your test. The |note| function lets you do this:
11
12 .. code-block:: pycon
13
14 >>> from hypothesis import given, note, strategies as st
15 >>> @given(st.lists(st.integers()), st.randoms())
16 ... def test_shuffle_is_noop(ls, r):
17 ... ls2 = list(ls)
18 ... r.shuffle(ls2)
19 ... note(f"Shuffle: {ls2!r}")
20 ... assert ls == ls2
21 ...
22 >>> try:
23 ... test_shuffle_is_noop()
24 ... except AssertionError:
25 ... print("ls != ls2")
26 ...
27 Falsifying example: test_shuffle_is_noop(ls=[0, 1], r=RandomWithSeed(1))
28 Shuffle: [1, 0]
29 ls != ls2
30
31 |note| is like a print statement that gets attached to the falsifying example reported by Hypothesis. It's also reported by :ref:`observability <observability>`, and shown for all examples (if |settings.verbosity| is set to |Verbosity.verbose| or higher).
32
33 .. note::
34
35 |event| is a similar function which tells Hypothesis to count the number of test cases which reported each distinct value you pass, for inclusion in :ref:`test statistics <statistics>` and :ref:`observability reports <observability>`.