Search code examples
pythontox

How can I specify which positional argument to use in tox?


I have the following tox.ini:

[tox]
envlist = py37-{lint, test}

[testenv:py37-{lint, test}]
envdir = {toxworkdir}/lint_and_test_env
deps = 
    pylint
    pytest
    pytest-xdist
commands =
    lint: pylint src {posargs}
    test: pytest tests {posargs}

I want to run both environments in parallel and specify --jobs=4 for pylint and -n auto for pytest. Executing tox -p -- --jobs=4 -n auto fails because pylint does not recognize the -n argument and vice versa.

Is there a way to achieve my goal?


Solution

  • I see no way to do what you want.

    That said, I would strongly recommend that you split your environments and use one for testing and one for linting.

    [tox]
    envlist = py37, lint
    
    [testenv]
    deps =
        pytest
        pytest-xdist
    commands =
        pytest tests {posargs}
    
    [testenv:lint]
    deps = pylint
    commands = pylint src {posargs}
    

    Then you can pass in different arguments to the commands, or run all envs via tox.

    Also you could set defaults in case you do not specify {posargs}.

    e.g.

    [testenv:lint]
    deps = pylint
    commands = pylint src {--jobs=4:posargs}
    

    If you want to run all tox envs in parallel you could run...

    tox -p
    

    P.S.: I am one of the tox maintainers, and I have contributed to hundreds of open source projects, and basically 99% use separate envs for testing and linting.

    P.P.S.: If you want to run more than one linter, you should have a look at pre-commit.