Unittesting
Introduction
Our python-package libranet_logging
comes with a series of tests to check
the validity of our code. Some are unittests, others are full integration-tests.
The distinction between the several types of tests is of lesser importance.
However we try to minimize the amount of mocking and/or patching.
The test-framework we use is pytest
.
Pytest is an extensible framework, and has a whole series of addons and plugins available on PyPi.
You just can pip-install these packages:
<env-dir>/bin/pip install pytest pytest-cov pytest-flask pytest-mccabe pytest-mock
Note
Since the tests are importing the code of libranet_logging, this package needs be installed in a virtualenv, together with all its dependencies. See installation.
Re-usable test-fixtures
For our package, we have several application-specific test-fixtures in tests/conftest.py
:
# pylint: disable=missing-function-docstring
"""conftest.py - custom pytest-plugins.
For more information about conftest.py, please see:
- https://docs.pytest.org/en/latest/writing_plugins.html
- https://pytest-flask.readthedocs.io/en/latest/tutorial.html
"""
import os
import pathlib
import pytest
@pytest.fixture(scope="session")
def tests_dir():
tests_dir_ = os.path.dirname(os.path.realpath(__file__))
return pathlib.Path(tests_dir_)
# @pytest.fixture(scope="session")
# def pkg_dir():
# pkg_dir_ = pkg_resources.resource_filename("libranet_logging", "")
# return pl.Path(pkg_resources.files("libranet_logging") / "etc/logging.yml")
# return pathlib.Path(pkg_dir_)
@pytest.fixture(scope="function")
def env(tmpdir):
var_dir = tmpdir.mkdir("var")
var_log_dir = var_dir.mkdir("log")
# var_run_dir = var_dir.mkdir('run')
# var_tmp_dir = var_dir.mkdir('tmp')
# override any existing env-variable
os.environ["PYTHON_LOG_DIR"] = str(var_log_dir)
return tmpdir
Run tests
Run pytest via:
> cd <env-dir>
> bin/pytest src/libranet_logging/
> cd <env-dir>src/libranet_logging
> pytest
(libranet) wouter@midgard: <env-dir>src/libranet_logging > make py
platform linux -- Python 3.5.5, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /opt/miniconda/envs/libranet/src/libranet_logging, inifile: setup.cfg
plugins: xdist-1.22.2, tap-2.2, forked-0.2, flask-0.10.0, flake8-1.0.0, cov-2.5.1, celery-4.0.2
collected 15 items
========== test session starts =====================
tests/test_cli.py .
tests/test_filters.py ....
tests/test_logconfig.py ........
tests/test_loglevel.py ..
========== 15 passed in 0.33 seconds ===============
Run tests with code-coverage
> pytest --cov=libranet_logging --cov-report html .