1. What is Pytest?
The pytest framework makes it easy to write small, readable tests and can scale to support complex functional testing for applications and libraries.
2. Why do I choose pytest instead of unittest?
Although both frameworks are great for performing testing in python. Firstly, pytest is fast, efficient, and easy to work with. For example, in unittest, we will need to import modules, create a class and define the testing functions within that class. However, in pytest, we only need to define the testing functions. In addition, pytest has rich plug-in support and community support and much more popular than the unittest.
3. Highlighted features in pytest library.
In general, I think there are three key features worthwhile to be shared here: Fixture, Marker, and Customized Configuration.
3.1 Fixture
They provide a fixed baseline so that tests execute reliably and produce consistent, repeatable, results. Initialization may setup services, a state, or other operating environments. These are accessed by test functions through arguments; for each fixture used by a test function, there is typically a parameter (named after the fixture) in the test function’s definition.
Example:
import pytest
class Fruit:
def __init__(self, name):
self.name = name
self.cubed = False
def cube(self):
self.cubed = True
class FruitSalad:
def __init__(self, *fruit_bowl):
self.fruit = fruit_bowl
self._cube_fruit()
def _cube_fruit(self):
for fruit in self.fruit:
fruit.cube()
# Arrange
@pytest.fixture
def fruit_bowl():
return [Fruit("apple"), Fruit("banana")]
def test_fruit_salad(fruit_bowl):
# Act
fruit_salad = FruitSalad(*fruit_bowl)
# Assert
assert all(fruit.cubed for fruit in fruit_salad.fruit)
3.2 Marker
We can create a pytest.ini file under the testing directory, and declare the test configuration within this file. Mostly, we can hybrid the concept of fixtures to set up predefine connection strings or even client connections. In my most current code case, I have defined some fixture and global variables under the test directory and named it contest.py. Here I will share some examples line for pytest.ini.
Registering marks
You can register custom marks in your pytest.ini
file like this:
[pytest]
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
serial
or in your pyproject.toml
file like this:
[tool.pytest.ini_options]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
"serial",
]
3.3 Customized Configuration
We can create a pytest.ini file under the testing directory, and declare the test configuration within this file. Mostly, we can hybrid the concept of fixtures to set up predefine connection strings or even client connections. In my most current code case, I have defined some fixture and global variables under the test directory and named it contest.py. Here I will share some examples line for pytest.ini.
pytest.ini
pytest.ini
files take precedence over other files, even when empty. Let me share one example of pytest.ini
# pytest.ini
[pytest]
minversion = 6.0
python_files = test_*.py
python_classes = Test
python_function = test_*
addopts = -ra -q
testpaths =
tests
integration