1. Invoking pytest from CLI
In this video, we will learn how to invoke pytest from command-line interface. Let's get started.
2. Example CLI run: syntax
Command-Line Interface (or "CLI") is a user interface that allows one to interact with a computer program by entering text commands into a terminal.
Let's take a look at the example. Assume we have some tests written in the "slides dot py" script, and we want to run them. To do that, we have to write the keyword "pytest", followed by the name of the test script, which in our case is - "slides dot py".
The keyword "pytest" means that we want to run this script using the pytest framework (and not just as a regular Python script). The keyword "slides dot py" calls "a module argument" and refers to the specific module we want to run.
So, in simple words, we are asking to run the pytest framework using the tests from the "slides dot py" module.
3. Example CLI run: output
After running that command, you will see the output of the test in the terminal. The output contains a summary of the test run, such as..
4. Example CLI run: output
Module versions.
5. Example CLI run: output
Number of collected tests.
6. Example CLI run: output
Names of the test scripts.
7. Example CLI run: output
And test results. A number of "collected" tests means test functions that pytest found in the test script. The test results represent "how many tests passed, failed, skipped, etc."
8. IDE exercises
Before we dive into the CLI commands, let me introduce you to the new type of exercise - "Integrated Development Environment" or "IDE". The IDE exercises provide students with an Integrated Development Environment.
9. IDE exercises
You can find the exercise instructions on the left, write scripts on the right, and execute console commands at the bottom. Thus, the IDE exercises not only let you write code and execute it but run the commands from the command-line interface (CLI) as well.
10. Directory argument
We can run not only one test script but a whole bunch of them. To do that, we saved all our test scripts in one folder, called "tests underscore dir". Note, that the trailing slash after the directory name is not required in this case, but is a good practice to increase readability. Now, we can use the same "pytest" command, but instead of the particular script, we're passing the whole directory to it.
After running "pytest tests underscore dir", we get an output that tells us that six tests were collected from the two scripts, and six of them passed.
11. Keyword argument - filter tests by name
Let's assume that we want to run only tests with a certain name. For example, we want to pick only the tests with the word "squared" in their names. To accomplish that, we can use the keyword flag, which is represented by "minus k", and followed by the expression that the function name has to match with.
In this simple case, if there is only one function whose name contains the "squared" word, then only that one test will run. Thus, you can see, that there were "3 items collected, 2 deselected, and 1 selected" in the output. The one selected test also passed in this case.
12. Summary
Let's recap. IDE exercises let one write code in IDE and use the command-line interface (CLI). CLI pytest command starts with the "pytest" keyword. Tests can run using one script by passing a script name or a set of scripts from one directory by passing the name of the directory. You can select tests to run with a keyword expression by passing "minus k" followed by the expression.
13. Let's practice!
You are now familiar with pytest CLI. Try to run some tests using different commands!