Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Testing Code With Assertions

In this lesson, you’ll set up a virtual environment. For more information on this important coding practice, watch Working With Python Virtual Environments.

00:00 Testing Your Code With Assertions. Testing is another field in the development process where assertions are useful. Testing boils down to comparing an observed value with an expected one to check if they’re equal or not. This kind of check perfectly fits into assertions.

00:18 Assertions must check for conditions that should typically be true, unless you have a bug in your code. This idea is another important concept behind testing. The pytest third-party library is a popular testing framework for Python. At its core, you’ll find the assert statement, which you can use to write most of your test cases in pytest.

00:42 On-screen are a few examples of writing test cases using assert statements. These examples take advantage of some built-in functions, which provide the testing material.

01:05 All of these test cases use the assert statement. Most of them are written using the assertion formats you’ve already seen in the course. They all showcase how you’d write real-world test cases to check different pieces of your code with pytest.

01:43 Now, why does pytest favor plain assert statements in test cases over a custom API, which is what other testing frameworks prefer? Firstly, the assert statement allows pytest to lower the entry barrier and somewhat flatten the learning curve because its users can take advantage of Python syntax they already know. And secondly, the users of pytest don’t need to import anything from the library to start writing test cases.

02:07 They only need to start importing things if their test cases become complicated, demanding more advanced features. These advantage make working with pytest a pleasant experience for beginners and people coming from other testing frameworks with custom APIs.

02:24 For example, the standard-library unittest module provides an API consisting of a list of .assert*() methods that work pretty much like assert statements.

02:33 This kind of API can be difficult to learn and memorize for developers starting with the framework. You can use pytest to run all the test case examples seen previously.

02:45 Whenever you install a third-party library, it’s good practice to do so in a virtual environment. You can see one being created and activated on-screen, firstly for macOS and Linux …

03:07 and for Windows, where the activation command is different.

03:23 You can find out more about virtual environments in this Real Python course. With the virtual environment created and active, you need to install the library by issuing the command seen on-screen.

03:43 Then you can execute this command, testing test_samples.py. You should see an output similar to what’s seen on-screen. This line tells you that pytest discovered and ran eight test cases.

04:03 This line shows that seven out of eight tests passed. That’s why you get seven green dots and an F. Note that to avoid issues with pytest, you must run your Python interpreter in normal mode.

04:15 Remember the optimized mode disables assertions. You can check the current value of the PYTHOPTIMIZE environment variable with a command. If you’re using the Windows command prompt, then this is the command you’ll need.

04:38 If you’re using Windows Terminal, then you’ll need this command.

04:48 And for macOS or Linux, this is the command you’ll need. If PYTHOPTIMIZE is set, then you’ll see the current value. One feature to note is that pytest integrates nicely with the assert statement.

05:05 The library can display error reports with detailed information about the failing assertions and why they’re failing. As an example, check out the lines starting with the E letter in the previous output. They display error messages.

05:21 These lines clearly uncover the root cause of the failure. In this example, pow(10, 2) returns 100 instead of 42, which is intentionally wrong.

05:34 In the next section of the course, you’ll look at some common pitfalls when using assert.

Become a Member to join the conversation.