You can report results and assert that your test succeeded or failed using pytest, an open-source testing library for Python.
Writing tests is hard. You have to decide how you want to design your tests, how you want to structure them, and how to write them. If you find yourself repeating the same actions over and over again, your tests will be repetitive, and that makes them harder to maintain.
pytest is a testing tool that allows you to write and report assertions in your tests. This means you can easily write your tests, test the result of your tests, and report the results of your tests.
You don’t need to know how to write unit tests in order to use pytest. You only need to know how to write tests. pytest is a simple library. It’s available as a package on PyPI (the Python Package Index), and you can install it with pip:
pip install pytest
The documentation is clear and concise, and you can read the basics in just a few minutes.
pytest comes with a number of useful features, including the ability to write tests that are both readable and expressive.
When you write a test, pytest will create a test function that looks like this:
@pytest.mark.usefixtures('foo_setup')
def test_my_example():
assert foo.bar() == 1
The preceding test function shows you how to write a test. The first line shows that you are using the @pytest.mark.usefixtures decorator. This decorator tells pytest to run the fixture foo_setup after each test.
The second line shows that you are using a function called test_my_example to test the value of foo.bar().
The last line shows that you are asserting that the value of foo.bar() is 1.
You can also specify a setup function that pytest will execute before each test, and a teardown function that will execute after each test. The setup and teardown functions can be used to create or clean up resources, for example.
Each test function has a number of optional arguments. The most common ones are shown in the preceding code. The first argument is the name of the test function, which you provide in the first line of the test. The next two arguments are the name of the fixture and the names of the fixtures themselves, respectively.
Finally, there are three optional arguments that control how the test function works. The first argument is the name of the test. The second argument is a list of values. If the list contains more than one element, the test function will iterate through the list, running the test once for each element. If there are no elements in the list, then the test function will be skipped.
For example, the following test function will fail because the list doesn’t contain any elements:
@pytest.mark.usefixtures([‘foo’])
def test_my_example():
assert foo.bar() == 1
The preceding test will pass because it uses the test function test_my_example and a list containing only one element, foo.
The first time that the test function runs, the test will fail. Then the test function will be rerun, and the test will succeed.
In the example above, the test function is designed to be idempotent. That means it will always produce the same result, regardless of whether the test function is run again. This is usually a good thing, because it avoids unnecessary errors and keeps the tests reliable.
idempotent
If you want to know more about the differences between fixtures and tests, read the documentation for the pytest module.
pytest comes with a number of useful features. The following sections show how to use some of these features.
Assertions
pytest offers several assertions that you can use to assert that the value of a variable is correct. The following example uses the assert keyword, followed by the name of the variable that you want to assert, and the expected value of that variable. For example, the following example will fail:
@pytest.mark.usefixtures(‘foo_setup’)
def test_my_example():
assert foo.bar() == 1
Because the expected value of foo.bar() is 1, but the actual value of the variable is 0, the test fails.
If the actual value of foo.bar() is 1, then the test will succeed.
Expected values can also be lists, tuples, or dicts.
If you want to test more than one variable, you can use the assertion method chain. This method allows you to use multiple variables, in the same way that you can use multiple arguments to a function.
The following example uses the assertion method chain to assert that the value of the foo and bar variables are equal to 1 and 2 respectively.
@pytest.mark.usefixtures(‘foo_setup’)
def test_my_example():
assert foo == 1 and bar == 2
Here is another example fibonacci series – Pytest
import mymath
def test_fibonacci():
assert mymath.fibonacci(1) == 1
$ py.test test_fibonacci_ok.py
============================= test session starts
==============================
platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest- 2.5.2
collected 1 items
test_fibonacci_ok.py .
1 passed in 0.01 seconds
===========================