In this chapter, we will install the latest version of Python, install pytest, create a new Python project for pytest tests, and write our first test case.
Before we can do any testing in Python, we need to install Python on our machine. If your machine doesn't have Python, you can download the latest version from python.org. At the time of this recording, the latest version is
Unfortunately, installing Python properly can be complicated, especially if Python was previously installed on your machine. Be sure to follow appropriate instructions for your operating system.
To verify your Python installation, execute the following command in a terminal:
You should see the proper version printed. If the Python command doesn't work or doesn't print the expected version number, then try using
If that still doesn't work, then the correct Python installation might not be included in your system path. Find the directory into which Python was installed, manually add it to the system path if it is not already included, relaunch the command line, and try running Python again. You can view the current path settings by executing the following command:
If you need help, I included troubleshooting steps in the example project's README. A link to the example repository on GitHub will be provided in the transcript.
However, it has some very cool distinctives which we'll cover in this course. Pytest is also an open-source project. At the time of creating this course, pytest's latest version is 5.4.3.
Here, I'm showing pytest's website at pytest.org. Anytime you get stuck with pytest, try reading the docs. I've always found them to be helpful.
Now, let's install pytest. Pytest is a third-party package and is not part of the Python standard library. You will need to install it separately. If you haven't already installed pytest, run the following terminal command:
pip install pytest
Pip is the standard package installer for Python. I've already installed pytest, but I'll run the "pip" command again here just so you see what it looks like.
If you need to use the
python3 command to run Python, then you might also need to use the
pip3 command in lieu of
pip. Using pip will install packages globally for the whole machine. However, it's typically a best practice to manage dependencies per project using virtual environments. There are several tools and methods for managing virtual environments. I won't use them in my examples in this course just to keep things simple, but I recommend trying to use them on your own. The example project's README includes some pointers about virtual environments.
Now that Python and pytest are installed, let's create our first test project. Create a project directory by running the following command:
Then, change into that directory by running "cd" and the directory name:
Inside the project directory, create a new directory named "tests".
The "tests" directory will contain all the test cases we will write. Pytest does not require a directory named "tests" per se, but most Python projects use it as a conventional way to separate product code from test code.
I'd also like to mention one more thing about a project structure. For this course, we will create a new project for our test cases. However, when you write new test cases on your own, you might want to add them to an existing project rather than creating an entirely new project. For example, unit tests should almost always be added to the same project as the product code they cover. As another example, end-to-end tests might be best located in their own separate project if the application they cover is split into several repositories. Please consider what is best for your team's needs.
Now that we have our project, let's create our first test case module.
For this course, I will use Visual Studio Code as my editor. I like using VS Code for Python because Microsoft publishes a very good Python extension, and I find that VS Code has a good balance between features, speed, and simplicity. Other good Python editors include PyCharm, Atom, and Notepad++.
Create a file named "test_math.py" under the tests directory. Python source code files use the ".py" extension. We call Python files "modules" because they contain code that can be executed directly or imported by other files.
Inside this module, create a new function named "test_one_plus_one":
def test_one_plus_one(): assert 1 + 1 == 2
This function is a complete pytest test case. pytest is unique amongst test frameworks because its test cases are written as functions. Other frameworks like Python's unittest, Java's JUnit and C#'s NUnit, structure test cases as classes. Functions are simpler and more concise.
Naming conventions are important to pytest. Notice that both our test module and our test function contain the prefix "test_". When pytest runs, it will discover tests from its current directory down. By default, any function names with the prefix "test_" in any modules with the prefix "test_" will be identified and executed as test cases. You can override these settings using pytest config files but I recommend sticking with convention. Note that you may also put non-test functions inside test modules.
Finally, notice the one-line body. Our test case simply verifies the basic math operation of addition. To perform assertions, pytest uses Python's native
assert statement. There's no special assertion API or library. The assert statement simply evaluates a boolean condition and raises an exception of type
AssertionError if the condition is false.
Let's run this test. Clearly, one plus one does equal two, so our test should pass. For this course, I will run test cases from the command line. The command line is the most universal way to run tests. Pytest also has a rich set of command line options, many of which we will learn in future chapters. You can also run tests directly from editors and IDEs but they require extra configuration.
To run tests from the command line, enter the following command from the project's root directory. This invocation will find and execute all test cases within the project.
python -m pytest
You can also run tests using the shorter
pytest command. However, I recommend always using
python -m pytest. The lengthier command automatically adds the current directory to
sys.path so that all modules in the project can be discovered.
Let's enter our command to run our test.
When you enter the command to execute the tests, you will see pytest print a banner, list test modules as they run, print a "." for each passing test case, and then conclude with the test results summary. In our case, you should see that our one test passed. Nice! It should run very quickly, too. If for some reason the test case fails when you try to run it, double-check your code and your setup.