5

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 keyword argument to @given decorator?

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
Audra Jacot
  • 139
  • 7

1 Answers1

7

It depends on whether you want to restrict for one test or for all, but approach is similar and based on settings.

Configuring single test

To change default behavior for some of tests we can decorate them with settings object like

from hypothesis import given, settings, strategies


@given(strategies.integers())
@settings(max_examples=10)
def test_this_a_little(x):
    ...


@given(strategies.integers())
@settings(max_examples=1000)
def test_this_many_times(x):
    ...

for test_this_a_little there will be 10 examples generated (at most) and for test_this_many_times there will be 1000.

Configuring all tests

To change default behavior for all tests somewhere during bootstrap of your test run (e.g. for pytest it can be conftest.py module) we can define a custom hypothesis settings profile and then use it during tests invocation like

from hypothesis import settings

settings.register_profile('my-profile-name',
                          max_examples=10)

and after that (assuming that you are using pytest)

> pytest --hypothesis-profile=my-profile-name

Further reading

hypothesis is pretty awesome and allows us to configure many things, available options are listed in the docs.

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50