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/

128 questions
11
votes
3 answers

Generating list of lists with custom value limitations with Hypothesis

The Story: Currently, I have a function-under-test that expects a list of lists of integers with the following rules: number of sublists (let's call it N) can be from 1 to 50 number of values inside sublists is the same for all sublists…
alecxe
  • 414,977
  • 106
  • 935
  • 1,083
8
votes
3 answers

Python hypothesis: Ensure that input lists have same length

I'm using hypothesis to test a function that takes two lists of equal length as input. import hypothesis.strategies as st from hypothesis import assume, given @given(st.lists(ints, min_size=1), st.lists(ints, min_size=1), ) def…
Vermillion
  • 899
  • 1
  • 8
  • 20
7
votes
3 answers

How to see the output of Python's hypothesis library

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…
Newskooler
  • 2,594
  • 4
  • 29
  • 54
7
votes
1 answer

pytest fails with ModuleNotFoundError and name of unused plugin

I maintain an open source library, xarray, which runs integration tests on Travis-CI using pytest. We install the scientific Python using stack conda. Earlier today, our two out of our five test builds (Python 3.5 and 3.6, but not Python 2.7 or 3.4)…
shoyer
  • 7,407
  • 22
  • 43
7
votes
1 answer

Skipping falsifying examples in Hypothesis

The Story: I'm currently in the process of unit-testing a function using hypothesis and a custom generation strategy trying to find a specific input to "break" my current solution. Here is how my test looks like: from solution import answer #…
alecxe
  • 414,977
  • 106
  • 935
  • 1,083
5
votes
2 answers

Hypothesis - reuse @given between tests

I've been using hypothesis for some time now. I'm wondering how I could reuse @given parts. Some of the ones I have are like 20 lines and I copy the whole @given part above a couple of the test cases. A simple example of a test @given( …
idetyp
  • 123
  • 6
5
votes
1 answer

How to parametrize Hypothesis strategy in @given

I'm testing a REST API, which can process different requests, e.g., dated and dateless. A request has a field called request_type. I'm wondering what's the best way to write test in hypothesis: I can write two testes, one for dated, and the other is…
Bewang
  • 373
  • 2
  • 16
5
votes
2 answers

Why does my simple, finite hypothesis test never stop?

I am running a test suite with hypothesis-4.24.6 and pytest-5.0.0. My test has a finite set of possible inputs, but hypothesis never finishes testing. I have reduced it to the following minimal example, which I run as pytest test.py from hypothesis…
tahsmith
  • 1,463
  • 15
  • 22
5
votes
1 answer

Generate List of Random Objects with Hypothesis

I need to test a function in python that takes a list with any type of data, from integers to strings to any object a user makes up. Is there a way in hypothesis to generate a list with random objects? I know that I could generate a list of random…
jollyroger23
  • 397
  • 1
  • 2
  • 15
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…
4
votes
1 answer

How to create a datetime indexed pandas DataFrame with hypothesis library?

I am trying to create a pandas DataFrame with the hypothesis library for code testing purporses with the following code: from hypothesis.extra.pandas import columns, data_frames from hypothesis.extra.numpy import…
Newskooler
  • 2,594
  • 4
  • 29
  • 54
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,075
  • 3
  • 17
4
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
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
  • 414,977
  • 106
  • 935
  • 1,083
4
votes
2 answers

What does Flaky: Hypothesis test produces unreliable results mean?

I am using the hypothesis python package for testing. I am getting the following error: Flaky: Hypothesis test_visiting produces unreliable results: Falsified on the first call but did not on a subsequent one As far as I can tell, the test is…
sureshvv
  • 3,659
  • 1
  • 20
  • 30
1
2 3
8 9