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 :)