Pytest and tox¶
pytest is NOT compatible with Python 3.3.
Using Anaconda and tox on Windows is likely to result in a “Error: InterpreterNotFound” message and fail. If that is the case, see Working with Windows, conda, tox on how to make it work.
Test in one environment¶
To run tests for the current Python environment, simply invoke pytest in project root,
test as an option argument to
C:\dev\mypolr> python setup.py test
Tests can be run for multiple python versions in separate virtualenvs using tox. Its setup is defined in the tox.ini, and will run tests in separate environments for:
- Python 2.7
- Python 3.4
- Python 3.5
- Python 3.6
These need to be created first using
conda. (Keep reading.)
To run tests in all the Python environments, simply invoke tox in project root (after the Python environments are created).
Working with Windows, conda, tox¶
conda (miniconda or Anaconda) is at first glance not a good match,
or at least on Windows.
Tox expects to work from a virtualenv and not a conda environment.
However, it’s possible with a few, simple steps:
Read the tips below
if you’re using conda to manage Python environments and
have problems with
InterpreterNotFound-errors when attempting to run tox.
To use tox and conda on Windows, the following recommendations apply:
- For tox to find your environments, consider to either:
- Install environments in
C:\PythonXY, where X and Y is major and minor version, respectively; or
- Make a symlink from
C:\PythonXYto your real path. E.g.:
mklink /J C:\Python27 C:\Anaconda3\envs\myPy27env
- Install environments in
- In the environment where you call tox:
virtualenvwith conda and not with pip. This seems to work well with Anaconda.
- Tips 1:
--prefix, -p-option to define the location of new environments:
conda install -p C:\PythonXY python=X.Yto create an environment called
PythonXYin the location
C:\PythonXY. (No symlink creation is needed.)
conda install -p C:\path\to\myenv python=X.Yto create an environment called
myenvin the location
C:\path\to\myenv. (Symlinks should be made.)
- Tips 2:
- Add the
--yesoption to prevent conda from asking confirmation upon creating environments.
- Tips 3:
- If your Anaconda installation is on a different drive than C, e.g. E:\Anaconda3\,
then environments will be installed in the E:\Anaconda3\envs\-directory if your
current working drive is E.
This allows you to create envs in the same drive as the rest of Anaconda without the need to use
Fast and easy fix¶
The tests/tox_with_conda.py-file is a utility for making the steps above with a single call.
ToxEnvMatcher-class can be used from Python to create environments and set up the needed symlinks,
but it’s also possible to use the file from command line.
Examples of use in Python:
my_envs = join('E:\\', 'Anaconda3', 'envs') tem = ToxEnvMatcher(my_envs) for version in '27,34,35,36'.split(','): tem.make(version)
Examples of use from cmd.exe:
E:\dev\mypolr\tests> tox_with_conda.py E:\Anaconda3\envs 27 34 35 36
Environment prefix (defaults to py) can be overridden with -p/–env_prefix options:
E:\dev\mypolr\tests> python tox_with_conda.py E:\Anaconda3\envs 27 34 35 36 -p Python
This will create new environments in
E:\Anaconda3\envs\PythonXY instead of
If, for some reason you need to, it’s possible to use
--base option to override the default base location (
E:\dev\mypolr\tests> tox_with_conda.py E:\Anaconda3\envs 27 34 35 36 --base D:\Python
The tox_with_conda.py-file has been uploaded to a repository of its own on https://github.com/fauskanger/tox_with_conda and can also be installed with pip:
pip install tox_with_conda
If installed with pip, then instead of
python tox_with_conda.py ...
python -m tox_with_conda ...
The .travis.yml-file defines the Travis CI setup for this project. When new code has been pushed to the git repository, Travis CI will automatically pull the updates. Then it will build and run tests for multiple versions of Python. The process can be monitored here.
Travis continuous integration is not a replacement for running tests locally before committing changes or making pull requests.