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
1
vote
1 answer

generate an array of random numbers with hypothesis in python

I have a sorting algorithm that seems to work fine but I would like to test it more with random samples. So to test it, I'm trying to generate random arrays with hypothesis in Python, I would like the arrays to be of various lengths and contain any…
user206904
  • 504
  • 4
  • 16
1
vote
1 answer

Python Hypothesis package: can I ensure that certain values are used?

Problem Statement Below is a toy example that is close to what I am trying to do. @given( idx_start=integers(min_value=0, max_value=100000), idx_window=integers(min_value=0, max_value=100000), ) def test_calc_max(conftest_df, idx_start,…
Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
1
vote
3 answers

How do I control a random seed with Pytest and Hypothesis?

I have a test which executes a function which uses random things. I would like to use hypothesis (or something else ?) to run it several times and know, when it fails, which random seed was used. How can I do that? My goal is to test my code several…
bux
  • 7,087
  • 11
  • 45
  • 86
1
vote
2 answers

How to execute Python functions using Hypothesis' composite strategy?

I am trying to execute a function decorated with Hypothesis' @strategy.composite decorator. I know I can test functions using the @given decorator, such as - from hypothesis import given from hypothesis import strategies as…
1
vote
1 answer

How to implement dependant columns in hypothesis dataframes

I am using hypothesis dataframes to implement a dataframe in which start_time and end_time are two columns. Here is a chunck: import hypothesis.strategies as st import logging import datetime from hypothesis import given from…
GGJON
  • 325
  • 1
  • 13
1
vote
1 answer

Hypothesis does not seem to honor the maxfail argument of pytest

In spite of specifying maxfail=1, hypothesis seems to continue generating examples and running them and failing much later. Is there a workaround? Here is a small example: from hypothesis.stateful import invariant, rule,…
sureshvv
  • 4,234
  • 1
  • 26
  • 32
1
vote
3 answers

How to build a strategy to create array of tuples with pairs of identical values?

I'd like to generate a strategy for NumPy testing with an output like: array([[-2, -2], [-3, -3], [5, 5], [-1, -1]], dtype=int16) What I tried was: import numpy as np from hypothesis.strategies import integers from…
1
vote
1 answer

@composite vs flatmap in complex strategies

hypothesis allows two different ways to define derived strategies, @composite and flatmap. As far as I can tell the former can do anything the latter can do. However, the implementation of the numpy arrays strategy, speaks of some hidden costs #…
Marten
  • 1,336
  • 10
  • 16
1
vote
2 answers

Hypothesis search strategy for dictionaries with different types of values

I am trying to generate dictionaries containing different python types as values using the hypothesis module. For lists I can do this simply using the expression from hypothesis import given import hypothesis.strategies as st @given( st.lists( …
DragonTux
  • 732
  • 10
  • 22
1
vote
2 answers

Pytest/Hypothesis not matching string as expected

I am testing a validate function I am using with Sanic. def validate(val): try: if isinstance(val, bool): # Here because the int(val) line will success with int(True)/int(False) raise ValueError() if isinstance(val,…
NewGuy
  • 3,273
  • 7
  • 43
  • 61
1
vote
1 answer

How to see the "Bundle" output of Hypothesis Python library? (Stateful testing)

When using the hypothesis library and performing stateful testing, how can I see or output the Bundle "services" the library is trying on my code? Example import hypothesis.strategies as st from hypothesis.strategies import integers from…
Aniket
  • 11
  • 4
1
vote
1 answer

xfail single hypothesis case using pytest?

Using pytest, I set specific parameters to fail as expected. My testing suite uses hypothesis though. These is a bug that we know about that we don't intend to fix in this version of the API. It's already documented and the time it will takes to…
NewGuy
  • 3,273
  • 7
  • 43
  • 61
1
vote
1 answer

Declare relationship between two arguments generated by hypothesis

I am using hypothesis for testing, and I wanted to establish a relationship between two arguments of a test. I am aware of assume, but that seems quite wasteful when I know the constraints beforehand. Here's a minimal example: from datetime import…
1
vote
1 answer

Why strategies.permutations does not provide the smallest set of permutations?

I wrote a test to verify a given function behaves correctly for any given permutation of a list specified as input. Using the hypothesis python package I've tried to construct this test case. However, the list of permutations generated contains many…
Jir
  • 2,985
  • 8
  • 44
  • 66
1
vote
2 answers

Recursive strategies with additional parameters in Hypothesis

Using recursive, I can generate simple ASTs, e.g. from hypothesis import * from hypothesis.strategies import * def trees(): base = integers(min_value=1, max_value=10).map(lambda n: 'x' + str(n)) @composite def extend(draw, children): …
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487