Python 3/Doctest: Exception is not evaluated as expected result
Clash Royale CLAN TAG#URR8PPP
Python 3/Doctest: Exception is not evaluated as expected result
I have a python module containing (amongst other functions) this piece of code:
def check_color_range(*argv):
"""
Abbreviated documentation and other tests.
>>> check_color_range(23, -1, 99, 10000)
Traceback (most recent call last):
...
TypeError: Falscher Farbwert -1!
"""
for c in argv:
if c < 0 or c > 255:
raise TypeError('Falscher Farbwert ' + str(c) + '!')
When I run this using doctest like so: python -m doctest -v demo.py
, I get the following output:
python -m doctest -v demo.py
Trying:
check_color_range(23, -1, 99, 10000)
Expecting:
Traceback (most recent call last):
...
TypeError: Falscher Farbwert -1!
**********************************************************************
File "C:azcode_camp_pythonsrcEigeneProgrammeTag3arcade_basedemo.py", line 5, in demo.check_color_range
Failed example:
check_color_range(23, -1, 99, 10000)
Expected:
Traceback (most recent call last):
...
TypeError: Falscher Farbwert -1!
Got:
Traceback (most recent call last):
File "C:azminiconda3envspy37libdoctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest demo.check_color_range[0]>", line 1, in <module>
check_color_range(23, -1, 99, 10000)
File "C:azcode_camp_pythonsrcEigeneProgrammeTag3arcade_basedemo.py", line 12, in check_color_range
raise TypeError('Falscher Farbwert ' + str(c) + '!')
TypeError: Falscher Farbwert -1!
1 items had no tests:
demo
**********************************************************************
1 items had failures:
1 of 1 in demo.check_color_range
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
For me the expected and the actual Errors look the same, but I may be missing something. I already compared whitespace etc., which seems to be the same.
I then tried to paste the complete Traceback from the "Got:" section into the testcase - and I'm still get the failed test, so I guess I must be doing something wrong.
I'd be very happy, if you could give me a heads-up.
check_color_range(23, -1, 99, 10000)
TypeError: Falscher Farbwert -1!
-1
Yes and the testcase should accept this as an expected exception. You can see that doctest expects a Traceback, but states that the actual Traceback is different - and I cannot see where it differs.
– Nudelsuppe
Aug 10 at 9:27
In this case, maybe you should share the code that calls the
check_color_range
method, because it sound like there's the problem.– DrKaoliN
Aug 10 at 9:31
check_color_range
Well, I did that. You have the complete module code (first section) and the call (inlined code in the "When I run this..." sentence.
– Nudelsuppe
Aug 10 at 9:32
Oh, I understand now.
– DrKaoliN
Aug 10 at 9:37
1 Answer
1
On line 8 you have: TypeError: Falscher Farbwert -1!____
(4 blank spaces at the end)
TypeError: Falscher Farbwert -1!____
You should replace it with: TypeError: Falscher Farbwert -1!
TypeError: Falscher Farbwert -1!
Thanks for spotting that!
– Nudelsuppe
Aug 10 at 10:25
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
From what I see, you are running
check_color_range(23, -1, 99, 10000)
. So of course, you will receive the messageTypeError: Falscher Farbwert -1!
, because your second argument is-1
.– DrKaoliN
Aug 10 at 9:26