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

How to create a hypothesis strategy to sample uniformly over a range?

I am doing something roughly like this: test_a.py import unittest import hypothesis import hypothesis.extra.numpy import numpy as np from hypothesis import strategies as st SHAPE = (10, ) ARRAY_STRATEGY = hypothesis.extra.numpy.arrays(float,…
ringo
  • 978
  • 7
  • 16
1
vote
0 answers

Efficient way of requiring that a certain portion of elements from a Hypothesis strategy must be unique

What is the most efficient way of requiring rather than that all elements generated according to a hypothesis strategy are unique, at least a certain portion are unique? Strategies such as the following appear to be inefficient when multiply…
curlew77
  • 393
  • 5
  • 15
1
vote
1 answer

Can you create a function with a specific signature without using eval?

I’ve written some code that inspects function signatures, and I would like to generate test cases for it. For this, I need to be able to construct objects that result in a given Signature object when signature is called on them. I want to avoid just…
schuelermine
  • 1,958
  • 2
  • 17
  • 31
1
vote
1 answer

Handling of pydantic ValidationError when testing with hypothesis.given

When using hypothesis to test my pydantic models, I do not know how to handle ValidationError thrown by custom validators. This is a very small example that shows the problem: # model from pydantic import BaseModel, validator class…
Simon
  • 495
  • 1
  • 4
  • 18
1
vote
1 answer

generate matrix with independent columns

I am trying to generate matrices with independent columns. At the moment I am using assume which works, but requires a lot of computation: import sys import hypothesis.strategies as st import numpy as np from hypothesis import assume from…
Manuel Schmidt
  • 2,429
  • 1
  • 19
  • 32
1
vote
1 answer

Pytest and Hypothesis: given in nested async functions

I have a problem with a test case as shown below. I want to test a function of class Foo, but I can't create any instance of class Foo until I create my test function. Then for creation test values for the test, I need the instance of Foo for…
Phil997
  • 575
  • 5
  • 15
1
vote
1 answer

How to generate test samples with Hypothesis directly from dataclasses?

I have lets say two dataclasses where one is used inside the other like this : @dataclass(frozen=True) class SensorModel: sensor_id: int type: str health_status: bool @dataclass class SamplingModel: trigger: str priority: str…
KZiovas
  • 3,491
  • 3
  • 26
  • 47
1
vote
1 answer

Combining unit and property-based tests in pytest and Hypothesis

I need to run a mixture of unit tests and property-based tests in Python. My current test code runs them separately, and thus duplicates some code: @pytest.mark.parametrize("values", list_of_values) def test_my_function(a_value): …
Jim
  • 229
  • 1
  • 4
1
vote
1 answer

See all generated inputs for hypothesis tests

Is it possible to see (and hopefully save) the input data generated by hypothesis? I've been digging through the docs, but have so far been unable to find anything. My end goal is to use the generated input data to validate that the new python…
natemcintosh
  • 730
  • 6
  • 16
1
vote
0 answers

hypothesis - How to generate a pandas dataframe with variable number of columns

I am new to Hypothesis and I would like to know if there is a better way to use to Hypothesis than what I have done here... class TestFindEmptyColumns: def test_one_empty_column(self): input = pd.DataFrame({ 'quantity':…
Siraj Samsudeen
  • 1,624
  • 7
  • 26
  • 35
1
vote
2 answers

Given a Hypothesis Strategy, can I get the minimal example?

I'd like to get the minimum for a complicated class, for which I have already written a strategy. Is it possible to ask hypothesis to simply give me the minimum example for a given strategy? Context here is writing a strategy and a default minimum…
Aatif Syed
  • 71
  • 4
1
vote
1 answer

How do I replace the choices strategy?

I updated Python Hypothesis and it seems that choices() is now deprecated. Documentation does some handwaving about data() and sampled_from(), but it's not clear how those should be used in place of choices(). My code looks something line…
sureshvv
  • 4,234
  • 1
  • 26
  • 32
1
vote
2 answers

Write strategies to generate array shapes with total size less than certain value

I am trying to write a strategy generating array shapes of size 4 and product of all dims less than a given value.(say 16728). That means search space for this has a root at (1,1,1,1) and 4 leaves as (16728, 1,1,1), (1,16728,1,1), (1,1,16728,1), (1,…
1
vote
1 answer

Hypothesis.strategies generate string from date

I am using hypothesis to test my application and generate random input data for endpoints. Here is my code: def generate_upload_data(): today = datetime.date.today() start_date = today - relativedelta(months=1) return…
1
vote
1 answer

How to combine Hypothesis in my code programaticly and not as a test? (Use Hypothesis to distinguish between automata and Python function)

I have a Python function which describes Language L which gets a word and returns True in case that the word is in the language and return False otherwise. In addition, I have a Deterministic Finite Automata (DFA) which describes another language L2…