How do I tell tox not to collect tests from dependencies? - python

By default tox will collect the test from your dependencies too and I want it to collect only the ones from my package.
How can I do this?

Tox is a tool which creates a new virtualenv for each python version you have configured, installs the module your running and then runs a user-specified command to run the tests. It doesn't actually collect the tests to run. That's up to whichever testing tool you're using: py.test, nose, etc. To do that with tox, you'll edit/create a tox.ini to use the correct command that limits the collection of tests to whatever you want.
With nose:
[tox]
envlist = py26,py27
[testenv]
deps=nose
commands=nosetests test.module
With py.test:
[tox]
envlist = py26,py27
[testenv]
deps=pytest
commands=py.test test.module

Related

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?
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.

python tox: how to use a different python envlist in environment

Here is my tox.ini:
[tox]
envlist = py27,py35
[testenv]
deps =
Flask
connexion
pytest
coverage
pytest-cov
requests
six
commands=pytest --junitxml xunit-reports/xunit-result-XXX.xml --cov {envsitepackagesdir} --cov-report=xml
[testenv:local]
#HOW DO I SPECIFY A NEW LIST OF PYENV LIKE 31,36 IN HERE????
commands=
pytest --cov {envsitepackagesdir}/XXX --cov-report html
When I run tox it runs in py27 and py35. I want tox -e local to run in a different set of multiple python environments. I can't figure out how to do this. How do I do this? Right now it does not even respect the intial envlist and only runs on Python 2.7.
So what you want is to have two different sets of environments and run them independently.
What you have to understand first is that envlist is a list of all environments that would be run if you invoke tox without the -e option.
The next thing you have to understand is that there is only one of these lists per tox.ini and that one is in the global [tox] section.
The other thing you have to understand is that the pyXX factors (factors are the parts of environment names that are separated by the - sign) have a special meaning for tox, because they instruct it to build an environment with a specific interpreter. They are also called "default environments" (see basic usage). If you don't ask for that factor when calling tox, then the basepython interpreter will be used to build the virtualenv (the interpreter you are invoking tox from).
so if you invoke tox -e local with a tox.ini like yours, it will execute what is defined in [tox:local] with the basepython, because you are not defining which python should be used to create the virtualenv, so it uses the same interpreter that you invoked tox with.
If you want to be able to invoke your local factor with other interpreters, independent from those other environments, the following could get you started (described in the v2 config docs):
[tox]
envlist = {py27,py35}-remote,{py31,py36}-local
[testenv]
deps =
Flask
connexion
pytest
coverage
pytest-cov
requests
six
[testenv:remote]
commands=pytest --junitxml xunit-reports/xunit-result-XXX.xml --cov {envsitepackagesdir} --cov-report=xml
[testenv:local]
commands= pytest --cov {envsitepackagesdir}/XXX --cov-report html
Check which envs this creates with:
$ tox -a
py27-remote
py35-remote
py31-local
py36-local
What envlist with the curly braces notation does, is create environment names by combining all factors with their permutations (this can have more dimensions also).
if you say tox without -e they will all run and all use the correct interpreter.
If you want to run the local envs only you will have to call it with:
$ tox -e py31-local,py36
Then only those two will run. The thing to take away here is that if you want to run a subset of all environments you have to ask for them with their full names. There is no "sub generation" or extra envlist magic. Just list the full names of the envorenments in a comma separated list and you are golden.
UPDATE
Today I learned that you can also use the generation syntax from the command line. So you could type:
$ tox -e 'py{31,36}'-local
Thank you #phd for pointing it out.
A possible solution to what you are attempting to do is to use tox -l to list all the environments, filter the ones you want, and then feed them back into tox -e.
For example, to run all environments that have "local" in the name (using bash):
tox -e $(tox -l | grep local | paste -sd "," -)
Step-by-step explanation:
tox -l lists all the environments, one on each line
grep local filters only the lines of the input which contain the word "local"
paste -sd "," - joins the lines of the input with commas
[tox]envlist is only a default — a list of environments to run when tox is invoked without option -e and without TOXENV environment variable. Once you use tox -e [tox]envlist is ignored.
You can run local environment with different python versions, but I don't know any way to run it multiple times. You have to list all environments explicitly:
tox -e py33-local,py34-local
You can shorten the command line using tox' conventions:
tox -e 'py3{3,4}'-local
Use generative envlist and factor-conditional settings.
[tox]
envlist = {py27,py31,py35,py36}-{default,local}
[testenv]
deps =
Flask
connexion
pytest
coverage
pytest-cov
requests
six
commands =
{default,local}: python --version
default: pytest --junitxml xunit-reports/xunit-result-XXX.xml --cov {envsitepackagesdir} --cov-report=xml
local: pytest --cov {envsitepackagesdir}/XXX --cov-report html
List all possible combinations of python version and factors using: tox -l
For your "local" case you'd invoke tox in the one of the following ways:
tox -e py31-local
tox -e py36-local
tox -e 'py3{1,6}'-local
Answer heavily influenced by #oliver-bestwalter's answer, but I couldn't get that to work properly for some reason.
OK I greatly appreciate and upvoted the other two answers here but what I ended up doing was different. It seemed onerous just to achieve a separate
python version and command.
What I ended up doing was just making a seperate tox.ini and calling it like tox -c tox-local.ini

How to minimize repetition in tox file

Goal: Successfully execute specific tox commands, and have it run for "just" that specific matched command.
Example: tox -e py35-integration
tox should run only for py35-integration and not including the default or standalone py35 definition.
I have tried two different approaches, which as far as I understand are the two ways to go about trying to do what I'm trying to do.
note the flake8 command is to easily isolate between different commands and indicate to me what is running. It is not an indication of the command I'm really trying to run.
Furthermore, ini files are only showing relevant parts.
First approach
[tox]
envlist = {py27,py35}, {py27,py35}-integration
[testenv]
commands =
py27: python -m testtools.run discover
py35: python -m testtools.run discover
py27-integration: flake8 {posargs}
py35-integration: flake8 {posargs}
With this approach, the understanding here is that I want to have tox -e py27-integration run without also running what is defined for the py27 command. This is not what is happening. Instead, it will run both py27 and py27-integration.
Second approach
[tox]
envlist = {py27,py35}, {py27,py35}-integration
[testenv]
commands =
python -m testtools.run discover
[testenv:integration]
commands =
flake8 {posargs}
Now, here I am explicitly isolating for a "sub" environment with its own command to run for "integration".
However, unfortunately, I'm met with the exact same behaviour of all matched patterns of "py27" being executed.
I'm trying to avoid repeating testenv structures as: [testenv:py27-integration] and [testenv:py35-integration], which contain the exact same definitions (goal is to minimize repetition).
I would love to know if there is a way I can achieve what I am trying to do.
I do not want to venture down doing something like p27-integration as an alternative naming scheme, since our CI pipelines have templates expecting certain name structures, and these names are also idiomatic to tox, in that py27 for example is understood to install a 2.7 virtual environment.
Updated
[tox]
minversion = 3.15
envlist = {py27,py35}, {py27,py35}-integration
[testenv]
commands =
python -m testtools.run discover
[testenv:py{27,35}-integration]
commands =
flake8 {posargs}

How to configure pycharm / intellij idea to run tox tests

Is it possible to configure pycharm / intellij idea to run tox tests? I want to test my code against different python versions in separated py environments. I was trying to configure it, but so far I only managed to configure single py.test support.
See this PyCharm issue: PY-9727
Credit goes to Andrey Vlasovskikh at PyCharm
As a workaround, you can create a Python file in your project that imports and launches Tox:
import tox
tox.cmdline()
and then run this file via your project interpreter using its context menu.
You'll get hyperlinks in the console output for stack traces, but nothing more.
I'm afraid it's not supported, PyCharm will use the configured interpreter to run the tests.
You are welcome to submit a feature request.
7 years passed and now you can run Tox inside PyCharm. If you use pytest for testing it will even show Test Result same way like with running test locally by PyCharm.
To run tests in pycharm I use config like this
[tox]
envlist = py3.{7,8},codestyle,flake8,lint
minversion = 3.7
[testenv]
usedevelop = true
deps =
pytest
pytest-cov
commands = pytest --cov=src
[testenv:codestyle]
deps = pycodestyle
commands = pycodestyle src tests
[testenv:flake8]
deps = flake8
commands = flake8 src tests
[testenv:lint]
deps = pylint
commands = pylint src tests --rcfile=.pylintrc
Here I described why it looks like this. Also, there is integration with GitHub CI to run it on every push.

How to convince python tox to run tests only for the available python interpreters?

I am using python tox to run python unittest for several versions of python, but these python interpreters are not all available on all machines or platforms where I'm running tox.
How can I configure tox so it will run tests only when python interpretors are available.
Example of tox.ini:
[tox]
envlist=py25,py27
[testenv]
...
[testenv:py25]
...
The big problem is that I do want to have a list of python environments which is auto-detected.
As of Tox version 1.7.2, you can pass the --skip-missing-interpreters flag to achieve this behavior. You can also set skip_missing_interpreters=true in your tox.ini file. More info here.
[tox]
envlist =
py24, py25, py26, py27, py30, py31, py32, py33, py34, jython, pypy, pypy3
skip_missing_interpreters =
true
First if you don't have tox : pip install tox.
Use this command : tox --skip-missing-interpreters , it skips for the compilers which are not available locally and just runs for the available versions of python
tox will display an Error if an interpreter cannot be found. Question is up if there should be a "SKIPPED" state and making tox return a "0" success result. This should probably be explicitely enabled via a command line option. If you agree, file an issue at http://bitbucket.org/hpk42/tox .

Categories

Resources