I have a method that shall return None in some exceptional situations and otherwise something "complicated".
In my dummy MWE below (not my real method!) there are two situations (x==0 and x==10) where None is returned.
def foo(x: int) -> typing.Optional[float]:
if x == 0 or x == 10:
return None
else:
return 1/x
I have a unittest to test the "complicated" outcome in detail for "normal" situations. Now, I want to write a test specifically about whether the method returns None for the exceptional situations and something which is not None for the normal situation
@pytest.mark.parametrize("x, target",
[(0, None), (10, None), (5, not None)])
def test_foo(x, target):
assert foo(x) is target
I hoped to have a test case assert foo(5) is not None, but the variable target which was not None is evaluated as True and thus assert foo(x) is True will fail.
So how to test with parametrize that something is None or not None?