7

Is it possible to do the following using a single tox virtual environment?

[tox]
envlist = test, pylint, flake8, mypy
skipsdist = true

[testenv:lint]
deps = pylint
commands = pylint .

[testenv:flake8]
deps = flake8
commands = flake8 .

[testenv:mypy]
commands = mypy . --strict

[testenv:test]
deps = pytest
commands = pytest


As I am only testing on my python version (py3.7), I don't want tox to have to create 4 environments (.tox/test, .tox/pylint,.tox/flake8, .tox/mypy) when they could all be run on a single environment.

I also want to see what failed individually individually, thus don't want to do:

[tox]
skipsdist = true

[testenv]
commands = pylint .
           flake8 .
           mypy . --strict
           pytest

as the output would be like this:

_____________ summary ___________
ERROR:   python: commands failed

and not like this:

____________________summary _________________
ERROR:   test: commands failed
ERROR:   lint: commands failed
ERROR:   mypy: commands failed
  test: commands succeeded
A H
  • 2,164
  • 1
  • 21
  • 36

1 Answers1

10

Use generative names and factor-specific commands (search the tox configuration doc page for those terms for more info) to implement all of your desired environments as one so they share the same envdir:

[testenv:{lint,flake8,mypy,test}]
envdir = {toxworkdir}/.work_env
deps = pylint, flake8, pytest
commands =
    lint: pylint .
    flake8: flake8 .
    mypy: mypy . --strict
    test: pytest

In tox 4+, you may use the plugin tox-ignore-env-name-mismatch and set runner = ignore_env_name_mismatch in the testenv to allow testenvs with different names to share the same virtualenv.

mikenerone
  • 1,937
  • 3
  • 15
  • 19
  • 2
    Solution 1 may not work in tox4 without a plugin, unless you are willing to have the environment re-created each time: see https://github.com/tox-dev/tox/issues/425#issuecomment-1011944293 – Jim Garrison Feb 17 '22 at 02:11
  • @JimGarrison I wasn't aware of that wrinkle. Thanks for pointing it out! I've updated the answer to remove that first approach (which, btw, I'm almost positive worked at one time). – mikenerone Feb 18 '22 at 22:06
  • @JimGarrison Which plugin do you use for this use case? – Tibor Takács Jan 04 '23 at 09:43
  • @TiborTakács I did not use any plugin. – Jim Garrison Jan 04 '23 at 15:48