The package hypothesis provides a rich set of strategies to use, if one wants to test against known type of input arguments.
Consider the following class and one of its test using hypothesis:
from hypothesis.strategies import floats, integers, one_of
from hypothesis import given
import unittest
class A:
def __init__(self, value=0):
assert isinstance(value, (int, float))
self.value = value
@property
def doubled(self):
return self.value * 2
class Test_A(unittest.TestCase):
@given(testval=one_of(floats(allow_infinity=False, allow_nan=False), integers()))
def test_A_instantiation(self, testval):
self.assertEqual(testval * 2, A(value=testval).doubled)
if __name__ == '__main__':
unittest.main()
This checks two things: implicitly, if the provided value is of the accepted types and explicitly, if the calculation in self.double is correct.
This test class can be considered incomplete at least because any input type other than float or int is not checked: for that one needed another test case, where the assertion error is checked against directly, let's say using AssertRaises.
My question is: how do you do that in the hypothesis testing phiolosophy?
Hypothesis doesn't seem to have the a "not_one_of" strategy, that is, one where you can define something like "all builtin types except for this-and-that types". This leaves only the possibility to define a "one_of" strategy with a rally long list of types to choose from.
Note: this whole situation reminds me of the root problem in Jurassic Park, when they checked if the have the expected number of dinos, but never checked if they have more. So I don't think there is no simple solution, but I first tried hypothesis yesterday so it may be my lack of knowledge.
Testing above case can never be complete, especially since the main point would be about testing an assertion. Therefore as discussed above a single test where assertion should fail should be sufficient.