Questions tagged [python-hypothesis]

Hypothesis is a Python library for property-based testing; creating unit tests with pseudo-randomly generated data.

Questions about the testing library Hypothesis:

https://hypothesis.readthedocs.io/en/latest/

182 questions
5
votes
1 answer

Suppressing HealthCheck.too_slow for a composite hypothesis strategy

I have a number of unit tests that leverage a @composite strategy that I wrote. The strategy is quite slow (it generates complex objects) and from time to time one of the tests fails the too_slow health check. When that happens, I take a deep sigh…
Arek' Fu
  • 826
  • 8
  • 24
5
votes
2 answers

Python Hypothesis: How to compose generators that are dependent on each other?

I have a generator using python Hypothesis like the following: @st.composite def generate_network_fault_only(draw): fault = { "impaired": st.just(True), # need to detect if all faults are None to switch this back. "limit":…
4
votes
1 answer

Exception handling and testing with pytest and hypothesis

I'm writing tests for a statistical analysis with hypothesis. Hypothesis led me to a ZeroDivisionError in my code when it is passed very sparse data. So I adapted my code to handle the exception; in my case, that means log the reason and reraise…
suvayu
  • 4,271
  • 2
  • 29
  • 35
4
votes
1 answer

Hypothesis equivalent of QuickCheck frequency generator?

As a learning project I am translating some Haskell code (which I'm unfamiliar with) into Python (which I know well)... The Haskell library I'm translating has tests which make use of QuickCheck property-based testing. On the Python side I am using…
Anentropic
  • 32,188
  • 12
  • 99
  • 147
4
votes
1 answer

How do I set the minimum and maximum length of dataframes in hypothesis?

I have the following strategy for creating dataframes with genomics data: from hypothesis.extra.pandas import columns, data_frames, column import hypothesis.strategies as st def mysort(tp): key = [-1, tp[1], tp[2], int(1e10)] return [x…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
4
votes
1 answer

test isolation between pytest-hypothesis runs

I just migrated a pytest test suite from quickcheck to hypothesis. This worked quite well (and immediately uncovered some hidden edge case bugs), but one major difference I see is related to test isolation between the two property…
Bart Van Loon
  • 1,430
  • 8
  • 18
4
votes
1 answer

Generate string data from regex

I would like to be able to take a regex and generate conforming data using the python hypothesis library. For example given a regex of regex = re.compile('[a-zA-Z]') This would match any english alpha characters. An example generator for this…
4
votes
2 answers

Random sampling with Hypothesis

In Hypothesis, there is an corresponding sampled_from() strategy to random.choice(): In [1]: from hypothesis import find, strategies as st In [2]: find(st.sampled_from(('ST', 'LT', 'TG', 'CT')), lambda x: True) Out[2]: 'ST' But, is there a way to…
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
3
votes
3 answers

How to enforce relative constraints on hypothesis strategies?

Say I have 2 variables a and b where it is given that b > a, how then can I enforce this relative constraint on the hypothesis strategies? from hypothesis import given, strategies as st @given(st.integers(), st.integers()) def test_subtraction(a,…
3
votes
1 answer

Multiple strategies for same function parameter in python hypothesis

I am writing a simple test code in Python using the Hypothesis package. It there a way to use multiple strategies for the same function parameter? As an example, use integers() and floats() to test my values parameter without writing two separate…
Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
3
votes
1 answer

How can I generate a hypothesis strategy to generate a list that contains at least one of each element it samples from?

I am looking for a way to build a Hypothesis strategy such that each element in a given list is present in the generated list. e.g. Assuming that we have values = [0, 1, 2, 3, 5, 8] we want a strategy that generates lists from given values such…
tschlich
  • 43
  • 1
  • 5
3
votes
1 answer

Using given with parametrize

I was wondering if it is possible to use given with parameters comes from pytest's parametrize function. Example: import pytest from hypothesis import given from hypothesis import strategies as st @st.composite def my_strategy(draw, attribute): …
UdiM
  • 480
  • 3
  • 19
3
votes
2 answers

Hypothesis strategy generating inf when specifically asked not to

from functools import partial import hypothesis as h import hypothesis.strategies as hs import hypothesis.extra.numpy as hnp import numpy as np floats_notnull = partial(hs.floats, allow_nan=False, allow_infinity=False) complex_notnull =…
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
3
votes
1 answer

Hypothesis stateful testing with pytest.raises doesn't report sequence of steps

I want to write a hypothesis.stateful.RuleBasedStateMachine which asserts that an exception is raised under certain circumstances. pytest provides the raises context manager for writing tests about exceptions. If I use pytest.raises inside a…
jacg
  • 2,040
  • 1
  • 14
  • 27
3
votes
2 answers

Sort dataframes generated by hypothesis when row tuples have different dtypes

I want to create dataframes where End is larger than Start. This I do with: from hypothesis.extra.pandas import columns, data_frames, column import hypothesis.strategies as st positions = st.integers(min_value=0, max_value=int(1e7)) strands =…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
1
2
3
12 13