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
13
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
  • 1,238
  • 1
  • 14
  • 29
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
  • 462,703
  • 120
  • 1,088
  • 1,195
9
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
  • 453
  • 3
  • 18
8
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
  • 486
  • 1
  • 3
  • 13
8
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
  • 462,703
  • 120
  • 1,088
  • 1,195
7
votes
4 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
  • 3,973
  • 7
  • 46
  • 84
7
votes
1 answer

How do I use composite strategies in hypothesis (hypothesis.errors.InvalidArgument: Expected SearchStrategy but got function)

This example is a variation of the one in the docs: import hypothesis.strategies as st from hypothesis import given @st.composite def s(draw): x = draw(st.text(), min_size=1) y = draw(st.text(alphabet=x)) return (x, y) @given(s1=s,…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
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
  • 9,165
  • 1
  • 37
  • 55
6
votes
1 answer

Use pytest fixtures in test with hypothesis

As the documentation and this article state, it should be possible to use hypothesis strategies and pytest fixtures in the same test. But executing this example code of the article: from hypothesis import given, strategies as st from pytest import…
Dennis
  • 71
  • 5
6
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
  • 3,973
  • 7
  • 46
  • 84
6
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
  • 665
  • 1
  • 6
  • 19
6
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
  • 4,234
  • 1
  • 26
  • 32
5
votes
1 answer

How do you parametrize a pytest class with Hypothesis @given?

In order to test how my database behaves when I add two very similar data rows, I need to setup a new database for each combination of parameters. I'm also using Hypothesis' strategies to generate "similar" data rows. The test work flow should go…
Lucidnonsense
  • 1,195
  • 3
  • 13
  • 35
5
votes
1 answer

How to change max number of test cases generated by hypothesis?

The famous property-based testing framework hypothesis is capable to generate massive test case. But is there any way to restrict the quantity of test case generated by hypothesis in order to make testing period shorter? For example feeding specific…
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,643
  • 1
  • 17
  • 23
1
2 3
12 13