Loading video player…

Using Python's assert to Debug and Test Your Code (Overview)

Python’s assert statement allows you to write sanity checks in your code. These checks are known as assertions, and you can use them to test if certain assumptions remain true while you’re developing your code. If any of your assertions turn false, then you have a bug in your code.

Assertions are a convenient tool for documenting, debugging, and testing code during development. Once you’ve debugged and tested your code with the help of assertions, then you can turn them off to optimize the code for production. Assertions will help you make your code more efficient, robust, and reliable.

In this video course, you’ll learn:

  • What assertions are and when to use them
  • How Python’s assert statement works
  • How assert can help you document, debug, and test your code
  • How assertions can be disabled to improve performance in production
  • What common pitfalls you might face when using assert statements

To get the most out of this video course, you should have previous knowledge of expressions and operators, functions, conditional statements, and exceptions. Having a basic understanding of documenting, debugging, and testing Python code is also a plus.

Note that the REPL on-screen in this course is bpython, but the code will also work in the standard REPL. For a discussion of bpython, give The Real Python Podcast: Episode 141 a listen.

Download

Sample Code (ZIP)

3.1 KB
Download

Course Slides (PDF)

16.3 MB

00:00 Using Python’s assert to Debug and Test Your Code. Python’s assert statement allows you to write sanity checks in your code. These checks are known as assertions, and you can use them to test if certain assumptions remain true while you are developing your code. If any of your assertions become false, then you have a bug in your code.

00:22 Assertions are a convenient tool for documenting, debugging, and testing code during development. Once you’ve debugged and tested your code with the help of assertions, then you can turn them off to optimize the code for production.

00:34 Assertions will help you make your code more efficient, robust, and reliable. In this course, you’ll learn what assertions are and when to use them, how Python’s assert statement works, how assert can help you document, debug, and test your code, how assertions can be disabled to improve performance in production, and what common pitfalls you might face when using assert statements.

01:01 To get the most out of this course, you should have previous knowledge of expressions and operators, functions, conditional statements, and exceptions. Having a basic understanding of documenting, debugging, and testing Python code is also a plus.

01:15 As ever, Real Python has you covered with this course on expressions and operators, this one on functions, this one on conditional statements, and this one on exceptions.

01:32 You can check out this one for documentation, this one for debugging, and this one for testing.

01:43 Any code that you see running in the REPL will be using the bpython interpreter. This is a replacement Python interpreter that offers a number of enhancements, including code highlighting and suggestions.

01:54 But any code you see running on-screen will work in the Python REPL, which is typically accessed by typing python or python3 at your terminal or command-line prompt.

02:05 So now you know what’s going to be covered, let’s get started.

Become a Member to join the conversation.