In this lesson, you’ll set up a virtual environment. For more information on this important coding practice, watch Working With Python Virtual Environments.
Testing Code With Assertions
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.
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
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.
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
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.
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.
For example, the standard-library
unittest module provides an API consisting of a list of
.assert*() methods that work pretty much like
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.
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.
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.
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.
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
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.
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.
In the next section of the course, you’ll look at some common pitfalls when using
Become a Member to join the conversation.