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
2
votes
2 answers

pytest: monkeypatch while using hypothesis

Within a unit test, I'm using monkeypatch in order to change entries in a dict. from hypothesis import given, strategies test_dict = {"first": "text1", "second": "text2"} given(val=strategies.text()) def…
Andi
  • 3,196
  • 2
  • 24
  • 44
2
votes
1 answer

Strategy for subsequences/slices of given sequence (sub-lists/sub-tuples/sub-strings/etc.)

How can I write a strategy for subsequences of given sequence? For example given a list elements = list(range(5)) I want a strategy sub_elements = *strategy*(elements) which generates [] [0] [0, 2] [1, 3, 4] [0, 1, 2, 3, 4] simple approach with…
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
2
votes
1 answer

hypothesis repeats the same values

I'm new to hypothesis and tried this simple code: @settings(max_examples=5) @given(st.integers(), st.integers(), st.integers(), st.integers()) def test_foo(a,b,c,d): print(a,b,c,d) As you can see, test_foo receives 4 different integers. I…
UdiM
  • 480
  • 3
  • 19
2
votes
1 answer

Hypothesis, django, and generating external IDs

This is the first time I have tried to use Hypothesis with Django. I apologize in advance for only being able to share code snippets. This is a very small piece of an extensive code base. I have a Model with (among lots of other fields) a 1:1…
James
  • 375
  • 2
  • 14
2
votes
1 answer

Optimizing strategies usage (data generation)

I would like to optimize data generation speed for my unit tests. It seems strategies like from_regex and dictionaries take a long time to generate examples. Below a sample I wrote to try to benchmark examples generation: from hypothesis import…
Damien Flament
  • 1,465
  • 15
  • 27
2
votes
1 answer

Pandas Index example with repeated entries using hypothesis

I want to generate a pandas.Index with repeated entries, like this. >>> pd.Index(np.random.choice(range(5), 10)) Int64Index([3, 0, 4, 1, 1, 3, 4, 3, 2, 0], dtype='int64') So I wrote the following strategy: from hypothesis.extra.pandas import…
suvayu
  • 4,271
  • 2
  • 29
  • 35
2
votes
1 answer

booleans().example() always returns True

To reproduce: In [1]: from hypothesis import strategies as st In [2]: bool_st = st.booleans() In [3]: all(bool_st.example() for _ in range(1000)) Out[3]: True Why does st.booleans().example() always return True? My understanding is that the…
David Sanders
  • 4,069
  • 1
  • 24
  • 38
2
votes
1 answer

How can I refactor this recursive policy expression Strategy to parameterize its length?

Context Firstly, thanks for hypothesis. It's both extremely powerful and extremely useful! I've written a hypothesis strategy to produce monotonic (ANDS and ORs) policy expressions of the form: (A and (B or C)) This can be thought of as a tree…
doughgle
  • 827
  • 1
  • 9
  • 18
2
votes
2 answers

Python-Hypothesis: specifying and managing NaN values

I'm trying to use Hypothesis to generate a set of dataframes that I'll merge together. I want each individual column to be allowed to have NaN values, and I want to allow Hypothesis to generate some wacky examples. But I mostly want to focus on…
c74
  • 75
  • 4
2
votes
2 answers

pip install hypothesis[pandas] says hypothesis3.82.1 does not provide the extra 'pandas'

When I ran pip install hypothesis[pandas] I got the following: Collecting hypothesis[pandas] Using cached…
Justin H
  • 163
  • 1
  • 1
  • 7
2
votes
1 answer

Generate two lists of equal length in fixed_dictionaries using Hypothesis

I'm trying to generate sample data using the fixed_dictionaries strategy where two of the keys have lists as values that must be of equal length, e.g.: {'ids': [1, 2, 3], 'words': ['foo', 'bar', 'baz']} How can I enforce this constraint? I thought…
tkocmathla
  • 901
  • 11
  • 24
2
votes
1 answer

python hypothesis unit test using faker

I have a problem with python unit test by using hypothesis additional package, Faker. I want to test login process of my website, I already have the unit test scenario, but I want to automated the scenario with hypothesis. This is my simple code for…
2
votes
2 answers

python testing using hypothesis

The package hypothesis provides a rich set of strategies to use, if one wants to test against known type of input arguments. Consider the following class and one of its test using hypothesis: from hypothesis.strategies import floats, integers,…
jake77
  • 1,892
  • 2
  • 15
  • 22
2
votes
1 answer

Python Hypothesis - building strategy once for many tests?

I have a composite, expensive-to-build but cheap-to-test strategy. I must do: @given(expensive_strategy()) def test_all(x): assert... assert... ... It takes ~4 seconds to build the examples and negligible time to run the asserts. Best…
Eric Kaschalk
  • 369
  • 1
  • 4
  • 8
1
vote
1 answer

In the Hypothesis testing library, what is the real difference between assume and filter?

Within the Hypothesis testing library for Python, there is the "assume" function, which "marks the example as bad, rather than failing the test". If there are too many "bad" examples generated in a row, Hypothesis, by default, will error out that…