Skip to content

Commit 24bf148

Browse files
authored
Document factors (#2852)
1 parent 793132d commit 24bf148

File tree

3 files changed

+141
-32
lines changed

3 files changed

+141
-32
lines changed

docs/changelog/2852.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved documentation for factors and test env names - by :user:`stephenfin`.

docs/config.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ examples first and use this page as a reference.
9797
commands = mypy src
9898
"""
9999
100+
.. _conf-core:
101+
100102
Core
101103
----
102104

@@ -230,7 +232,7 @@ Python language core options
230232
:ref:`base_python` and instead always use the base Python implied from the Python name. This allows you to configure
231233
:ref:`base_python` in the :ref:`base` section without affecting environments that have implied base Python versions.
232234

233-
235+
.. _conf-testenv:
234236

235237
tox environment
236238
---------------

docs/user_guide.rst

Lines changed: 137 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,135 @@
11
User Guide
22
==========
33

4-
Basic example
5-
-------------
4+
Overview
5+
--------
66

77
tox is an environment orchestrator. Use it to define how to setup and execute various tools on your projects. The
8-
tool can be:
8+
tool can set up environments for and invoke:
9+
10+
- test runners (such as :pypi:`pytest`),
11+
- linters (e.g., :pypi:`flake8`),
12+
- formatters (for example :pypi:`black` or :pypi:`isort`),
13+
- documentation generators (e.g., :pypi:`Sphinx`),
14+
- build and publishing tools (e.g., :pypi:`build` with :pypi:`twine`),
15+
- ...
16+
17+
Configuration
18+
-------------
19+
20+
*tox* needs a configuration file where you define what tools you need to run and how to provision a test environment for
21+
these. The canonical file for this is the ``tox.ini`` file. For example:
22+
23+
.. code-block:: ini
24+
25+
[tox]
26+
requires =
27+
tox>=4
28+
env_list = lint, type, py{38,39,310,311}
29+
30+
[testenv]
31+
description = run unit tests
32+
deps =
33+
pytest>=7
34+
pytest-sugar
35+
commands =
36+
pytest {posargs:tests}
937
10-
- a test runner (such as :pypi:`pytest`),
11-
- a linter (e.g., :pypi:`flake8`),
12-
- a formatter (for example :pypi:`black` or :pypi:`isort`),
13-
- a documentation generator (e.g., :pypi:`Sphinx`),
14-
- library builder and publisher (e.g., :pypi:`build` with :pypi:`twine`),
15-
- or anything else you may need to execute.
38+
[testenv:lint]
39+
description = run linters
40+
skip_install = true
41+
deps =
42+
black==22.12
43+
commands = black {posargs:.}
1644
17-
First, in a configuration file you need to define what tools you need to run and how to provision a test environment for
18-
these. The canonical file for this is the ``tox.ini`` file, let's take a look at an example of this (this needs to live
19-
at the root of your project):
45+
[testenv:type]
46+
description = run type checks
47+
deps =
48+
mypy>=0.991
49+
commands =
50+
mypy {posargs:src tests}
2051
21-
.. note::
52+
.. tip::
2253

2354
You can also generate a ``tox.ini`` file automatically by running ``tox quickstart`` and then answering a few
2455
questions.
2556

57+
The configuration is split into two type of configuration: core settings are hosted under a core ``tox`` section while
58+
per run environment settings hosted under ``testenv`` and ``testenv:<env_name>`` sections.
59+
60+
Core settings
61+
~~~~~~~~~~~~~
62+
63+
Core settings that affect all test environments or configure how tox itself is invoked are defined under the ``tox``
64+
section.
65+
2666
.. code-block:: ini
2767
2868
[tox]
29-
envlist =
69+
requires =
70+
tox>=4
71+
env_list = lint, type, py{38,39,310,311}
72+
73+
We can use it to specify things such as the minimum version of *tox* required or the location of the package under test.
74+
A list of all supported configuration options for the ``tox`` section can be found in the :ref:`configuration guide
75+
<conf-core>`.
76+
77+
Test environments
78+
~~~~~~~~~~~~~~~~~
79+
80+
Test environments are defined under the ``testenv`` section and individual ``testenv:<env_name>`` sections, where
81+
``<env_name>`` is the name of a specific environment.
82+
83+
.. code-block:: ini
84+
85+
[testenv]
86+
description = run unit tests
87+
deps =
88+
pytest>=7
89+
pytest-sugar
90+
commands =
91+
pytest {posargs:tests}
92+
93+
[testenv:lint]
94+
description = run linters
95+
skip_install = true
96+
deps =
97+
black==22.12
98+
commands = black {posargs:.}
99+
100+
[testenv:type]
101+
description = run type checks
102+
deps =
103+
mypy>=0.991
104+
commands =
105+
mypy {posargs:src tests}
106+
107+
Settings defined in the top-level ``testenv`` section are automatically inherited by individual environments unless
108+
overridden. Test environment names can consist of alphanumeric characters and dashes; for example: ``py311-django42``.
109+
The name will be split on dashes into multiple factors, meaning ``py311-django42`` will be split into two factors:
110+
``py311`` and ``django42``. *tox* defines a number of default factors, which correspond to various versions and
111+
implementations of Python and provide default values for ``base_python``:
112+
113+
- ``pyNM``: configures ``basepython = pythonN.M``
114+
- ``pypyNM``: configures ``basepython = pypyN.M``
115+
- ``jythonNM``: configures ``basepython = jythonN.M``
116+
- ``cpythonNM``: configures ``basepython = cpythonN.M``
117+
- ``ironpythonNM``: configures ``basepython = ironpythonN.M``
118+
- ``rustpythonNM``: configures ``basepython = rustpythonN.M``
119+
120+
You can also specify these factors with a period between the major and minor versions (e.g. ``pyN.M``), without a minor
121+
version (e.g. ``pyN``), or without any version information whatsoever (e.g. ``py``)
122+
123+
A list of all supported configuration options for the ``testenv`` and ``testenv:<env_name>`` sections can be found in
124+
the :ref:`configuration guide <conf-testenv>`.
125+
126+
Basic example
127+
-------------
128+
129+
.. code-block:: ini
130+
131+
[tox]
132+
env_list =
30133
format
31134
py310
32135
@@ -43,25 +146,25 @@ at the root of your project):
43146
pytest-sugar
44147
commands = pytest tests {posargs}
45148
149+
This example contains a global ``tox`` section as well as two test environments. Taking the core section first, we use
150+
the :ref:`env_list` setting to indicate that this project has two run environments named ``format`` and ``py310`` that
151+
should be run by default when ``tox run`` is invoked without a specific environment.
46152

47-
The configuration is split into two type of configuration: core settings are hosted under the ``tox`` section and per run
48-
environment settings hosted under ``testenv:<env_name>``. Under the core section we define that this project has two
49-
run environments named ``format`` and ``py310`` respectively (we use the ``envlist`` configuration key to do so).
153+
The formatting environment and test environment are defined separately via the ``testenv:format`` and ``testenv:py310``
154+
sections, respectively. For example to format the project we:
50155

51-
Then we define separately the formatting environment (``testenv:format`` section) and the test environment
52-
(``testenv:py310`` section). For example to format the project we:
156+
- add a description (visible when you type ``tox list`` into the command line) via the :ref:`description` setting
157+
- define that it requires the :pypi:`black` dependency with version ``22.3.0`` via the :ref:`deps` setting
158+
- disable installation of the project under test into the test environment via the :ref:`skip_install` setting -
159+
``black`` does not need it installed
160+
- indicate the commands to be run via the :ref:`commands` setting
53161

54-
- add a description (visible when you type ``tox list`` into the command line),
55-
- we define that it requires the ``black`` PyPI dependency with version ``22.3.0``,
56-
- the black tool does not need the project we are testing to be installed into the test environment so we disable this
57-
default behaviour via the ``skip_install`` configuration,
58-
- and we define that the tool should be invoked as we'd type ``black .`` into the command line.
162+
For testing the project we use the ``py310`` environment. For this environment we:
59163

60-
For testing the project we use the ``py310`` environment, for which we:
61-
62-
- define a text description of the environment,
63-
- specify that requires ``pytest`` ``7`` or later together with the :pypi:`pytest-sugar` project,
64-
- and that the tool should be invoked via the ``pytest tests`` CLI command.
164+
- define a text description of the environment via the :ref:`description` setting
165+
- specify that we should install :pypi:`pytest` v7.0 or later together with the :pypi:`pytest-sugar` project via the
166+
:ref:`deps` setting
167+
- indicate the command(s) to be run - in this case ``pytest tests`` - via the :ref:`commands` setting
65168

66169
``{posargs}`` is a place holder part for the CLI command that allows us to pass additional flags to the pytest
67170
invocation, for example if we'd want to run ``pytest tests -v`` as a one off, instead of ``tox run -e py310`` we'd type
@@ -232,8 +335,8 @@ CLI
232335
have a stale Python environment; e.g. ``tox run -e py310 -r`` would clean the run environment and recreate it from
233336
scratch.
234337

235-
Configuration
236-
~~~~~~~~~~~~~
338+
Config files
339+
~~~~~~~~~~~~
237340

238341
- Every tox environment has its own configuration section (e.g. in case of ``tox.ini`` configuration method the
239342
``py310`` tox environments configuration is read from the ``testenv:py310`` section). If the section is missing or does
@@ -259,7 +362,9 @@ Configuration
259362
- To view environment variables set and passed down use ``tox c -e py310 -k set_env pass_env``.
260363
- To pass through additional environment variables use :ref:`pass_env`.
261364
- To set environment variables use :ref:`set_env`.
365+
262366
- Setup operation can be configured via the :ref:`commands_pre`, while teardown commands via the :ref:`commands_post`.
367+
263368
- Configurations may be set conditionally within the ``tox.ini`` file. If a line starts with an environment name
264369
or names, separated by a comma, followed by ``:`` the configuration will only be used if the
265370
environment name(s) matches the executed tox environment. For example:
@@ -279,6 +384,7 @@ Configuration
279384

280385
Parallel mode
281386
-------------
387+
282388
``tox`` allows running environments in parallel mode via the ``parallel`` sub-command:
283389

284390
- After the packaging phase completes tox will run the tox environments in parallel processes (multi-thread based).

0 commit comments

Comments
 (0)