pythontestingpytestpython-hypothesis

How do specify a only finite number of Hypothesis examples


I am using Hypothesis to unit test my package. There are some functions in my package that I only want to run a few examples over for testing. I do not want Hypothesis to generate examples for me.

If I do the following:

@example(s="hello world")
@example(s="hello world 2")
def test_test(s):
    assert len(s) > 0

and run pytest, PyTest complains that fixture 's' not found. I am not sure what the most elegant solution for this is. Cheers.


Solution

  • While it seems like for your particular case you wish to write tests with explicit data that doesn't use any data generation from hypothesis, you might be better off writing explicit tests named to reflect the data and expectations you are testing with.

    e.g.

    def test_when_this_then_foo_should_happen(this_data_thing):
        ...
    
    def test_when_that_then_bar_should_happen(that_data_thing):
        ...
    

    However, to help meet the requirement you are trying to achieve, perhaps you can do something like this (typically I'd always have a base strategy when adding explicit examples, so this is not something I typically do):

    By using one_of and just, you can then simply pass them in to your given and you can test each of those. You can see from the code sample below, that the following will/should pass as it will only generate the provided three examples.

    It is important to note of a warning in the documentation when using just:

    Note: value is not copied. Be wary of using mutable values.

    If value is the result of a callable, you can use builds(callable) instead of just(callable()) to get a fresh value each time.

    @given(strategies.one_of(
        strategies.just("mighty"),
        strategies.just("chicken"),
        strategies.just("burger")
    ))
    def test_stuff(s):
        assert s in ["chicken", "burger", "mighty"]