0

I have a function which returns the divisors of a number num. The function raises an error if number is negative.
I am using the hypothesis library to test this function but I am not sure how I can make it a property-based test.

This is my test:

@given(strategies.integers())
def test_valid(num):
    with pytest.raises(ValueError):
        divisors(-3)

This code works but how can I change it so that the argument is "num" instead of being hardcoded. Similarly how can I assert that one is an element of the set, for any number num.

2 Answers2

1

Change divisors(-3) to divisors(num)

@given(strategies.integers())
def test_valid(num):
    with pytest.raises(ValueError):
        divisors(num)

Bear in mind that strategies.integers() returns positive and negative numbers so you need to pass max_value=0 to it to generate only negative.

In order to assert that divisors func returns set assign a returned value to variable and use isinstance. The assert line could look like assert isinstance(divisors(num), set).

wiaterb
  • 496
  • 3
  • 8
0

No comments credits yet, so providing this as an answer.

To return negative numbers max_value=-1 should be used, as max_value is an inclusive boundary, i.e. if set to 0, 0 would be sampled.

AO_PDX
  • 26
  • 5