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
0
votes
1 answer

How to combine the @fixture and the @given decorators in Python hypothesis testing?

I'm working with pytest and hypothesis for property-based testing in Python. As a minimal example, suppose I want to test that my Matrix class constructor correctly assign the number of rows and columns to a Matrix instance. I want to do it in two…
0
votes
2 answers

How to make custom Hypothesis strategy to supply custom objects?

Suppose I have a class Thing class Thing: def __init__(self, x, y): ... And suppose I have a function which acts on a list of things. def do_stuff(list_of_things): ... I would like to write unit tests for do_stuff involving…
Galen
  • 1,128
  • 1
  • 14
  • 31
0
votes
1 answer

Create a pd.DataFrame with a minimum number of rows using hypothesis

I'm using the hypothesis library and I would like to create a pd.DataFrame with three columns. Each column may contain integer values, either +1, 0, or -1. The values doesn't need to be unique. Also, I would like to get at least ten rows. With the…
Andi
  • 3,196
  • 2
  • 24
  • 44
0
votes
1 answer

Generating an interval set in hypothesis

I have some code that works with intervals, which are really just python dicts with the following structure: { "name": "some utf8 string", "start": 0.0, # 0.0 <= start < 1.0 "end": 1.0, # start < end <= 1.0 "size": 1.0, # size…
breadjesus
  • 1,979
  • 3
  • 13
  • 8
0
votes
1 answer

How to generate complex Hypothesis data frames with internal row and column dependencies?

Is there an elegant way of using hypothesis to directly generate complex pandas data frames with internal row and column dependencies? Let's say I want columns such…
curlew77
  • 393
  • 5
  • 15
0
votes
1 answer

How can I generate instances of a class with inter-dependent attributes?

Assume this simple case: (reposted and lightly edited from the Hypothesis mailing list) @dataclass class A: names: list[str] ages: dict[str, float] How could I write a strategy which generates objects of type A but with the restriction that…
Zac Hatfield-Dodds
  • 2,455
  • 6
  • 19
0
votes
1 answer

Why use recursive() instead of deferred()?

I needed a strategy for arbitrary JSON values and after reading about the gotchas of using composite() for recursive data came up with this json_primitives = st.one_of( st.none(), st.booleans(), st.integers(), …
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
0
votes
1 answer

Hypothesis, using "one_of" with Pandas dtypes in the "data_frames" strategy

I would like to construct a Pandas series that is any of several dtypes. I was hoping to do something like this: from hypothesis import given import hypothesis.strategies as hs import hypothesis.extra.numpy as hs_np import hypothesis.extra.pandas as…
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
0
votes
1 answer

Hypothesis strategy from_regex does not respect min_size and max_size

I am having issues with Hypothesis generating strings which do not respect the given min/max sizes. Example: @given( st.text( alphabet=st.from_regex(regex=r"^[a-z][b-z]$", fullmatch=True), min_size=0, max_size=15, …
damd
  • 6,116
  • 7
  • 48
  • 77
0
votes
1 answer

Randomizing a user dataclass with pytest and hypothesis

I can manually define an Address builder strategy: import attrs from hypothesis import given import hypothesis.strategies as st @attrs.frozen(kw_only=True) class Address: street: str city: str AddressStrategy = st.builds( Address, …
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
0
votes
1 answer

Create hypothesis strategy that returns unique values

I'm trying to create a hypothesis strategy which produces integers with no repeats. Here's my code: import hypothesis import hypothesis.strategies as strategies def unique(strat): previous = set() @strategies.composite def…
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
0
votes
1 answer

Hypothesis python package for onehots and longitudinal data

For context I work with mixed tabular data. I have complex data pipelines that I’d like to make sure works on any configuration of data. I see the pandas add-on/extra and have some questions related to that. How would I generate one-hot columns…
davzaman
  • 823
  • 2
  • 10
  • 20
0
votes
1 answer

taking square using "value**2" results causes an overflow while "value*value" is fine

Given the same input, x**2 gives an integer overflow while x*x works fine. I am not sure if this is because of the python's internal implementation of those operator or if this is a bug in the hypothesis package. Is the opcode for x**2 different…
anilbey
  • 1,817
  • 4
  • 22
  • 38
0
votes
1 answer

How to check internal size parameters that controls example generation

In PropEr, there's an internal variable called Size that represents the size of generated example. For instance, when we have 2 variables and would like to make them proportional each other, PropEr let you write the following test: prop_profile2()…
Yoshi
  • 405
  • 4
  • 12
0
votes
1 answer

`event` throws TypeError caused by WeakKeyDictionary when tuple is passed

I wanted to see the statistics of list values and I passed the value to event() after converting it to tuple to make it hashable. @dataclass(frozen=True) class Foo: x: int y: str def __hash__(self): return hash((self.x,…
Yoshi
  • 405
  • 4
  • 12