Closed alexzanderr closed 2 years ago
it fails only when my custom function is raising an exception which is catched in the except block.
Thanks @alexzanderr for posting this issue.
The problem is that when you catch the exception in the first step, you do not perform the other two steps. In other words, in your function there are not always three"yield" statements reached.
Why do you actually try/catch the exception in the first step ? You should only do this if this exception is normal. Otherwise, simply let pytest fail :) If the exception is normal and it is legitimate to catch it, then you should write two other steps in the catch branch. Each step should end with "yield".
The problem is that when you catch the exception in the first step, you do not perform the other two steps. In other words, in your function there are not always three"yield" statements reached.
yes i know, that.
Why do you actually try/catch the exception in the first step ? You should only do this if this exception is normal. Otherwise, simply let pytest fail :)
im doing that because im making sure that my function is raising the correct type of exception based on the wrong type params or wrong values of params that it got.
You might be interested in using with pytest.raises
to actually perform such exception raising checks in a "pytest way".
Anyway whatever you decide, the solution is still very easy: make sure that you have three steps in all branches of your code, and that will work. For example:
try:
# we know that this raises some errors
# when the @real_number its not valid
result = fixed_set_precision_str(real_number, precision)
print(result)
except TypeError as error:
print(error)
# step 1
yield
# step 2
yield
# step 3
yield
except ValueError as error:
print(error)
# step 1
yield
# step 2
yield
# step 3
yield
else:
_decimals = get_total_decimals(result)
# step 1
# correct number of decimals ?
assert _decimals == precision
yield
# step 2
# is type str ?
assert isinstance(result, str)
yield
# step 3
# result == expected ?
assert result == expected_result
yield
Indeed pytest does not allow for dynamic modification of the number of tests. So if you declare three steps then there must be three tests, and the only way pytest-steps is able to know what you put inside is to look at the yield statements.
yes, indeed, the solution is to repeat fake yields inside the except block.
# step 1
yield
# step 2
yield
# step 3
yield
thanks a lot for support!
You're welcome!
i have some code
in pytest output i do get this error:
i understand why, but i dont know how to fix this, because
steps
is always expecting a generator.thanks in advance.