The tutorial repository can be found here:
1. Create a simple project
To present an example of Pytest usage we need to create a Python project at the beginning. In the project folder called calculator, we are going to create the Python package sources. For tutorial purposes, we are creating operations.py contains a simple divide function and empty file __init__.py. Creating the __init__.py file means that the sources package can be easily imported as a module from the parent directory.
We also have to create a folder for our tests. In this tutorial, this folder is called tests. To get started writing tests we are going to create a file test_operations.py. If we want Pytest to find test files without listing their names during running tests we should add the prefix or suffix test in the files' names. Moreover, for the same purpose as in the sources package, we also added the __init__.py file.
Our project folder should look like this:
. └── calculator ├── sources │ ├── operations.py │ └── __init__.py └── tests ├── __init__.py └── test_operations.py
2. Write an example function
Firstly we are going to write a simple function in the calculator.py file. The division function gets two values a and b and returns the result of dividing a by b.
def divide(a, b): return a/b
3. Write an example test
Now we want to write tests to check the correctness of divide function results. Firstly, we are going to write a test which will check dividing by two correctness. Above all, we need to import the division function from the sources package. The next step is to write a test. The name of the test function also should be prefixed or suffixed with the test substring. Our simple test contains an assert statement that checks the condition of the comparison dividing function result with an expected result. If the comparison returns True we get information about the passed test. Otherwise, if the comparison returns False we will see information that our test failed.
from sources.operations import divide def test_division_by_two(): assert divide(4, 2) == 2 # SHOULD PASS assert divide(9, 2) == 100 # SHOULD FAIL assert divide(1000, 2) == 500 # SHOULD PASS
4. Run a test
In general, Pytest is invoked with the command pytest. This will execute all tests in all files whose names follow the form test_*.py or \*_test.py in the current directory and its subdirectories.
pytest
In the output, you will see that test failed.
In the output report:
- A dot (
.
) means that the test passed. - An
F
means that the test has failed. - An
E
means that the test raised an unexpected exception.
For failed tests, the report gives a detailed breakdown of the failure. In the example above, the test failed because of assertion fails.
5. Write another test
Let's write another test that will check dividing by three correctness.
test_operations.py file should look like this:
from sources.operations import divide def test_division_by_two(): assert divide(4, 2) == 2 # SHOULD PASS assert divide(9, 2) == 100 # SHOULD FAIL assert divide(1000, 2) == 500 # SHOULD PASS def test_division_by_three(): assert divide(9, 3) == 3 # SHOULD PASS assert divide(30, 3) == 10 # SHOULD PASS assert divide(9999, 3) == 3333 # SHOULD PASS
6. Run a tests
pytest
In the output, we can see that one test is failed and the second is passed. To see the detailed output of each test case we are going to run tests again with the '-v' option.
pytest -v
In the extended report, there is information about each test's result, test session progress, and assertion details when tests fail.
7. Example of Pytest command-line options
Command | Result |
---|---|
pytest -version | Gives a version of the Pytest module. |
pytest -v | Increases the verbosity. |
pytest -h or pytest –help | Shows help on the command line and config-line options |
pytest -x | Stops execution after the first failure. |
pytest -maxfail=n | Stops execution after nth failure. |
pytest module name | Helps to run tests by specifying the module name. |
pytest directory_name/ | Runs the tests from the directory. |
pytest -k EXPRESSION | Runs tests that match the given substring expression. |
pytest –ignore=path | Specifies module, directory, or tests to ignore the tests. |
pytest --markers | Lists available markers. |
pytest --fixtures | Lists available fixtures. |