I am setting python 3 tests and am trying to get my students to raise a specific exception in response to incorrect user input. For example:
raise Exception("The value must be greater than 0")
Unfortunately, even though the output generated by running code matches the expected output exactly, it is seen as an error, and thus not marked.
Any idea on what I should do to accept a specific exception as the correct answer?
Example of problem screenshot:
Apologies for answering my own question. I thought it would be better to add the solution than to delete, just in case someone needs to do the same in future.
The solution is to catch the error in the input field.
x = DataPoint("ABCDEF", 23)
except Exception as e:
Now the output matches the desired raised error message exactly.
Good answer :)
However, one thing to be aware of is that some students will simply print the expected output rather than throwing an exception. The easiest fix is to have a second similar test in which the exception handler prints something different.