Python: Automated testing
Theory: Pytest framework
When there are a lot of tests and test files, new questions arise:
- How to group the tests?
- How to run all the tests in one directory?
- How many tests should be there?
- How long should the tests be?
- Can we run the tests in parallel?
Test frameworks solve these issues. They help organize the structure of tests and give many handy things, such as convenient output. We'll get acquainted with most of these features later in the course.
The Pytest framework is popular in the Python world, and we'll focus on it in this lesson.
What is Pytest
Python is one of the few languages whose standard library already includes the latest framework — it is called unittest. It would be logical to look at it here, but it is deeply tied to classes you may not know. Therefore, we're looking at Pytest, which is still more popular than the embedded framework.
Next, we'll create a project from scratch and add tests.
The setup of Pytest
Create a directory somewhere on your computer with the name hexlet_pytest. Go into it and run the following commands:
The poetry new command creates a dummy Python package and adds a directory for future tests. Install Pytest in the dev group using the poetry add-G dev pytest command. Check that everything is working correctly:
Now, let us add some source code. Create a file hexlet_pytest/example.py with this as the contents:
Pytest expects the tests to be in the tests directory at the project root. You can create any structure inside this directory — Pytest will find all the tests there. The naming of test files should be as follows: test_<name>.py. As a rule, <name> corresponds to the name of the tested file.
Let us write our first test. Create a file called tests/test_example.py with the following content:
We will take apart the structure of this file later, but let us try to run a test for execution for now:
The structure of Pytest
Let us take another look at the test file:
Each test inside the file is a function starting with the prefix test_. You can do any number of test functions because everything already depends on the complexity of the tested code. We perform checks using the assert statement inside the functions.
The assert instruction works differently in Pytest Despite the similarity with the built-in instructions in Python. Try to make an error in the original function and repeat the tests:
The output of Pytest contains not only the original statement but also the result of the function call:
- What came out is marked with a plus
- What we expected is marked with a minus
This output makes it much easier to find and fix the error.
There is one peculiarity in the work of Pytest. If you need to print something to the screen, then Pytest will not output anything. By default, it intercepts and suppresses all output. You can turn off this behavior using the -s flag: