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:
Code Block |
---|
|
.
└── calculator
├── sources
│ ├── operations.py
│ └── __init__.py
└── tests
├── __init__.py
└── test_operations.py |
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.
Code Block |
---|
|
def divide(a, b):
return a/b |
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.
Code Block |
---|
|
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 |
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.
In the output, you will see that test failed.
Image Modified
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.
Write another test
Let's write another test that will check dividing by three correctness.
test_operations.py file should look like this:
Code Block |
---|
|
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
|
Run a tests
Image Modified
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.
Image Modified
In the extended report, there is information about each test's result, test session progress, and assertion details when tests fail.
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. |