Dockerized Testing with Pytest¶
This setup is based on the Unit Testing in Python Linkedin course.
Docker configuration¶
Dockerfile¶
FROM python:3.7.6-buster
RUN mkdir /pytest_project/
COPY ./test-requirements.txt /pytest_project/
COPY ./setup.py ./setup.py
RUN pip install --upgrade pip
RUN pip install -e .
RUN pip3 install -r /pytest_project/test-requirements.txt
WORKDIR /pytest_project/
CMD "pytest"
ENV PYTHONDONTWRITEBYTECODE=true
docker-compose.yaml¶
version: '3.1'
services:
test:
build: .
volumes:
- .:/pytest_project
stdin_open: true
tty: true
Python configuration¶
setup.py¶
from setuptools import setup, find_packages
import os
long_description = '''
This project is an example of a pytest
project featuring assertions,
exceptions, parametrization,
fixtures and factory fixtures.
'''
version = "1.0.0"
requirements = [
]
if __name__ == '__main__':
setup(
name='python_unit_testing_in_docker',
version=version,
description='Pytest Configuration Example',
long_description=long_description,
author="jomeke",
packages=find_packages(
exclude=[
'tests',
],
include=[
'scripts',
'utils'
],
),
license='MIT',
install_requires=requirements,
classifiers=[
'Development Status :: 1 - Planning',
'Framework :: Pytest',
'Intended Audience :: Developers',
'Intended Audience :: Education',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: Software Development',
'Topic :: Software Development :: Libraries',
'Topic :: Software Development :: Libraries :: Python Modules'
],
)
setup.cfg¶
[metadata]
description-file = README.md
pytest.ini¶
[pytest]
python_paths = scripts
testpaths = tests
addopts = --pep8 --flakes --verbose --durations=10 --color=yes
--cov=scripts
pep8maxlinelength=100
markers =
pep8: pep8 style check
flakes: pyflakes style check
test-requirements.txt¶
coverage==4.5.2
pytest==5.2.0
pytest-cov==2.6.1
pytest-flakes==2.0.0
pytest-pep8
pytest-pythonpath
docker
Usage¶
Build docker image
$ docker-compose build
Run test container
This will run the container and open a bash shell inside the container.
$ docker-compose run test sh
Inside container
Run entire test suite
$ pytest
Run tests from files that match a keyword
$ pytest -k <keyword-to-match-filename>
Run tests while printing all variables and verbose output
$ pytest -vvl
Exit the shell and the test container
$ exit
Discussion¶
Pytest settings could be moved from pytest.ini to setup.cfg. See Pytest Configuration.
setup.cfg¶
[tool:pytest]
python_paths = scripts
testpaths = tests
addopts = --pep8 --flakes --verbose --durations=10 --color=yes
--cov=scripts
pep8maxlinelength=100
markers =
pep8: pep8 style check
flakes: pyflakes style check
See Also¶
pytest-cov package documentation
pytest-flakes on pypi
pytest-pep8 package on pypi
- pytest-pythonpath package on pypi
docker package on pypi or documentation
Meta¶
Created on: 2023-03-29