[PYTHON] tox configuration file cheat sheet

Overview

Since I introduced tox after studying with CI for Python module, I will describe the contents that I often write in the configuration file for myself.

What is tox

It is a tool that can perform tests in MultiEnvironment using virtualenv. It's nice to be able to test modules with multiple versions of Python.

Configuration file type

According to the document, three types of files are read in the following order of priority.

  1. pyproject.toml
  2. tox.ini
  3. setup.cfg

The contents of tox.ini are described below.

Embedded variables

A list of embedded variables available in each section.

Setting name Overview
{toxinidir} tox.iniDirectory where
{toxworkdir} Directory where the virtual environment is created
{envname} Virtual environment name,[tox:{envname}]What was described in the part of
{envdir} Virtual environment directory, by default{toxworkdir}/{envname}

global section

This section describes common settings. [tox] Described below.

Setting name Overview Default value
envlist Multiple execution environments can be described by enumerating, separating with commas, or line breaks.
Basically described
py37``py38Some are prepared by default
-
toxworkdir Directory where the virtual environment is created
Files created by tox are stored in this directory
{toxinidir}/.tox
skipsdist Whether to package the results
trueSkip with
false
skip_missing_interpreters Set the return code when it fails in the interpreter relationship
trueIf it fails, return the code that is considered successful,configReads from the config file
config

example

tox.ini


[tox]
envlist =
    py38, py37
    flake8
    isort
skipsdist = true
skip_missing_interpreters = true

env section

This section describes the details of each environment described in the envlist of the global section. [tox: {envname}] Described below.

Setting name Overview Default value
commands Mandatory
Command content to be executed in the current environment
coverage run -p -m pytestOrflake8Set etc.
-
deps Specify the dependencies required to execute the command
Specify the module directly or require.Describe in txt-rrequirements.txtSpecify with
In shortpip installOptional part of
-
passenv Specify the environment variable that you want to inherit to the execution virtual environment
LANG``PIP_INDEX_URLEtc. can be set
-
setenv Set new environment variables in the execution virtual environment -
whitelist_externals It is not prepared on the execution virtual environment side, but specify it when you want to use it without problems with commands in the local environment. -
alwayscopy If you don't like symbolic links to Python filestrueDesignation false
changedir Specify the working directory when executing the command {toxinidir}
envlogdir Set log file storage {envdir}/log
description Give a brief description
The contents are displayed if verbose is specified when executing the environment display command with tox.
no description

example

tox.ini


[tox]
envlist =
    py38
    flake8
skipsdist = true

#Details of py38 environment
[testenv]
passenv = LANG
deps = -rrequirements.txt
passenv = PYTHONPATH
setenv =
    PYTHONDONTWRITEBYTECODE=1
changedir = tests
commands =
    coverage run -p -m pytest

#details of flake8 environment
[testenv:flake8]
skip_install = true
deps =
    flake8 >= 3.7
    flake8-import-order
changedir = {toxinidir}
description = 'check pep8 style'
commands = flake8 module

The settings for pytest and flake8 itself are described in setup.cfg.

setup.cfg


[tool:pytest]
testpaths = tests

[coverage:run]
branch = True
source =
    module
    tests

[flake8]
exclude = build,.git/*,.tox/*,./tests/*
ignore =
    # allow Multiple spaces before Operator
    E221
max-line-length = 140

Reference -Official Document

Recommended Posts

tox configuration file cheat sheet
Curry cheat sheet
SQLite3 cheat sheet
pyenv cheat sheet
conda command cheat sheet
PIL / Pillow cheat sheet
Linux command cheat sheet
ps command cheat sheet
Spark API cheat sheet
Python3 cheat sheet (basic)
PySpark Cheat Sheet [Python]
Python sort cheat sheet
Go language cheat sheet
numpy memory reuse cheat sheet
[Python3] Standard input [Cheat sheet]
Data Science Cheat Sheet (Python)
Slack API attachments cheat sheet
Python Django Tutorial Cheat Sheet
scikit learn algorithm cheat sheet
Apache Beam Cheat Sheet [Python]
Google Test / Mock personal cheat sheet
Continuation Passing Style (CPS) Cheat Sheet
Python cheat sheet (for C ++ experienced)
Configuration file best practices in Flask
Python Computation Library Cheat Sheet ~ itertools ~