How to Catch and Print Assertion Errors in Python

  • Context: Python 
  • Thread starter Thread starter Sunwoo Bae
  • Start date Start date
  • Tags Tags
    Coding Error Python
Click For Summary

Discussion Overview

The discussion revolves around how to handle and format assertion errors in Python, particularly in the context of unit testing. Participants explore different approaches to managing output when assertions fail, including the use of try...except blocks and the assertRaises function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses a desire to modify the output of assertion errors to exclude file and line number details, focusing solely on the AssertionError message.
  • Another participant argues that including line numbers is beneficial for debugging and suggests using try...except to handle errors gracefully instead of allowing the program to fail.
  • A third participant proposes using assertRaises for unit testing as a better alternative to capturing crash dump outputs.
  • A later reply questions whether the original poster is using a unit test framework and suggests reverting to catching assertions and printing their messages if not.

Areas of Agreement / Disagreement

Participants do not reach a consensus, as there are multiple competing views on how to handle assertion errors and whether the original context is appropriate for unit testing.

Contextual Notes

There is uncertainty regarding the original poster's familiarity with unit testing frameworks and whether they are following a specific tutorial, which may affect their approach to handling assertions.

Who May Find This Useful

Individuals interested in Python programming, particularly those working with error handling and unit testing.

Sunwoo Bae
Messages
60
Reaction score
4
1581924271419.png

I would like to make output only read:

Traceback (most recent call last):
AssertionError

... without giving the File "<input>", line 1, in <module>
 
Technology news on Phys.org
1581925677016.png
 
Why? The line number is there to help you debug your code. If this is to show a message to an end user then you should be using try...except, not just letting the program fail.
 
Oh its for unit testing? You should use assertRaises in your test instead of testing the crash dump output.
 
pbuk said:
Oh its for unit testing? You should use assertRaises in your test instead of testing the crash dump output.
can you tell me what assertRaises exactly do and how to use it? I have never heard about it :(
 
Hmmm, looking again I am not sure you are using a unit test framework. Is this an online tutorial? If so, stick with my first answer: catch the assertion and print its message. You can look up how to catch the failing assertion with try...except and how to get the message string from an AssertionError in a Python reference if you haven't covered them already (have you skipped some of the tutorial?).
 

Similar threads

Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 9 ·
Replies
9
Views
10K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K