1
1
User Guide
2
2
==========
3
3
4
- Basic example
5
- -------------
4
+ Overview
5
+ --------
6
6
7
7
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}
9
37
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:.}
16
44
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}
20
51
21
- .. note ::
52
+ .. tip ::
22
53
23
54
You can also generate a ``tox.ini `` file automatically by running ``tox quickstart `` and then answering a few
24
55
questions.
25
56
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
+
26
66
.. code-block :: ini
27
67
28
68
[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 =
30
133
format
31
134
py310
32
135
@@ -43,25 +146,25 @@ at the root of your project):
43
146
pytest-sugar
44
147
commands = pytest tests {posargs}
45
148
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.
46
152
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:
50
155
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
53
161
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:
59
163
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
65
168
66
169
``{posargs} `` is a place holder part for the CLI command that allows us to pass additional flags to the pytest
67
170
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
335
have a stale Python environment; e.g. ``tox run -e py310 -r `` would clean the run environment and recreate it from
233
336
scratch.
234
337
235
- Configuration
236
- ~~~~~~~~~~~~~
338
+ Config files
339
+ ~~~~~~~~~~~~
237
340
238
341
- Every tox environment has its own configuration section (e.g. in case of ``tox.ini `` configuration method the
239
342
``py310 `` tox environments configuration is read from the ``testenv:py310 `` section). If the section is missing or does
@@ -259,7 +362,9 @@ Configuration
259
362
- To view environment variables set and passed down use ``tox c -e py310 -k set_env pass_env ``.
260
363
- To pass through additional environment variables use :ref: `pass_env `.
261
364
- To set environment variables use :ref: `set_env `.
365
+
262
366
- Setup operation can be configured via the :ref: `commands_pre `, while teardown commands via the :ref: `commands_post `.
367
+
263
368
- Configurations may be set conditionally within the ``tox.ini `` file. If a line starts with an environment name
264
369
or names, separated by a comma, followed by ``: `` the configuration will only be used if the
265
370
environment name(s) matches the executed tox environment. For example:
@@ -279,6 +384,7 @@ Configuration
279
384
280
385
Parallel mode
281
386
-------------
387
+
282
388
``tox `` allows running environments in parallel mode via the ``parallel `` sub-command:
283
389
284
390
- After the packaging phase completes tox will run the tox environments in parallel processes (multi-thread based).
0 commit comments