How to see the output of Python's hypothesis library

Viewed 1482

When using the hypothesis library and performing unit testing, how can I see what instances the library is trying on my code?

Example

from hypothesis import given
import hypothesis.strategies as st

@given(st.integers())
def silly_example(some_number):
    assert some_number > 0

The question is: how do I print / see the some_number variable, generated by the library?

4 Answers

See here - either the note function and --hypothesis-verbosity=verbose, or the event function and --hypothesis-show-statistics should do the trick.

You could put a print statement or logging statement before the assert:

import logging
from hypothesis import given
import hypothesis.strategies as st

log_filename = 'debug.log'
logging.basicConfig(filename=log_filename, level=logging.DEBUG)
logger = logging.getLogger(__name__)

@given(st.integers())
def silly_example(some_number):
    logger.debug('silly_example(%s) called', some_number)
    assert some_number > 0

By using logging instead of print statements, you can turn off all logging simply by changing the logging level. If you change logging.DEBUG to logging.INFO:

logging.basicConfig(filename=log_filename, level=logging.INFO)

then logger.debug will no longer emit records.

--hypothesis-verbosity=debug helps to see the output.

--hypothesis-verbosity=debug and --hypothesis-verbosity=verbose both works for me - but I have to get pytest not to capture the output as well, with a -s in front. Looks like this:

python -m pytest -s --hypothesis-verbosity=debug tests
Related