0
votes

I have some code where the control logic changes based on python2 vs python3. I am also using tox to test this, when I realized:

  • tox doesn't use the right sys.version_info.major for tests, it uses whatever version of python you used when you run the tox command

A MVCE can be shown below with three files in the same directory:

setup.py

from setuptools import setup
if __name__ == '__main__':
    setup(name='throwaway')

dev_requirements.txt

nose

tox.ini

[tox]
envlist=py{27,3.6}
[testenv]
deps=-rdev_requirements.txt
commands=
   nosetests 

test_versions.py

import sys
import unittest


class tester(unittest.TestCase):

    def test_version(self):
        self.assertEqual(2, sys.version_info.major)

This test case should be failing when run in the python3.6 setting, however, when it is run using the tox command:

$python --version
Python 2.7.13
$tox
GLOB sdist-make: /Users/username/tmp/toxtesting/setup.py
py27 create: /Users/username/tmp/toxtesting/.tox/py27
py27 installdeps: -rdev_requirements.txt
py27 inst: /Users/username/tmp/toxtesting/.tox/dist/throwaway-0.0.0.zip
py27 installed: appdirs==1.4.3,nose==1.3.7,packaging==16.8,pyparsing==2.2.0,six==1.10.0,throwaway==0.0.0
py27 runtests: PYTHONHASHSEED='3463719492'
py27 runtests: commands[0] | nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
py3.6 create: /Users/username/tmp/toxtesting/.tox/py3.6
py3.6 installdeps: -rdev_requirements.txt
py3.6 inst: /Users/username/tmp/toxtesting/.tox/dist/throwaway-0.0.0.zip
py3.6 installed: appdirs==1.4.3,nose==1.3.7,packaging==16.8,pyparsing==2.2.0,six==1.10.0,throwaway==0.0.0
py3.6 runtests: PYTHONHASHSEED='3463719492'
py3.6 runtests: commands[0] | nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
___________________________________________________________________________________ summary ____________________________________________________________________________________
  py27: commands succeeded
  py3.6: commands succeeded
  congratulations :)
2

2 Answers

1
votes

When run with Python2.7, tox will use that environment for all its configurations (as shown in the example).

However, if tox is run using a python3 environment:

$python --version
Python 3.6.0

$tox 
GLOB sdist-make: /Users/username/tmp/toxtesting/setup.py
py27 create: /Users/username/tmp/toxtesting/.tox/py27
py27 installdeps: -rdev_requirements.txt
py27 inst: /Users/username/tmp/toxtesting/.tox/dist/throwaway-0.0.0.zip
py27 installed: appdirs==1.4.3,nose==1.3.7,packaging==16.8,pyparsing==2.2.0,six==1.10.0,throwaway==0.0.0
py27 runtests: PYTHONHASHSEED='595957228'
py27 runtests: commands[0] | nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.003s

OK
py3.6 create: /Users/username/tmp/toxtesting/.tox/py3.6
py3.6 installdeps: -rdev_requirements.txt
py3.6 inst: /Users/username/tmp/toxtesting/.tox/dist/throwaway-0.0.0.zip
py3.6 installed: appdirs==1.4.3,nose==1.3.7,packaging==16.8,pyparsing==2.2.0,six==1.10.0,throwaway==0.0.0
py3.6 runtests: PYTHONHASHSEED='595957228'
py3.6 runtests: commands[0] | nosetests
F
======================================================================
FAIL: test_version (test_versions.tester)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/username/tmp/toxtesting/test_versions.py", line 8, in test_version
    self.assertEqual(2, sys.version_info.major)
AssertionError: 2 != 3

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (failures=1)
ERROR: InvocationError: '/Users/username/tmp/toxtesting/.tox/py3.6/bin/nosetests'
___________________________________________________________________________________ summary ____________________________________________________________________________________
  py27: commands succeeded
ERROR:   py3.6: commands failed

the expected behavior will be seen, where sys.version_info is correct between both the python2.7 and python3.6 versions.

0
votes

Try '36' instead of '3.6' in env name. If that doesn't help declare basepython for your environments:

[testenv]
basepython =
    py27: python2.7
    py36: python3.6

Add a comand to display python version:

commands =
    {envpython} --version

I usually add the command in [testenv] and call it from every other test section like this:

[testenv]
…
commands =
    {envpython} --version

[mytest]
commands =
    {[testenv]commands}
    … other test commnds …