运行测试时如何让tox尊重系统版本?

时间:2017-05-10 16:15:34

标签: python python-3.x tox

我有一些代码,其中控制逻辑基于python2和python3而改变。当我意识到时,我也在用tox来测试这个:

  • tox没有使用正确的sys.version_info.major进行测试,它使用运行tox命令时使用的任何python版本

MVCE可以在下面显示,同一目录中有三个文件:

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)

当在python3.6设置中运行时,此测试用例应该失败,但是,当使用tox命令运行时:

$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 个答案:

答案 0 :(得分:1)

使用Python2.7运行时,tox将使用该环境进行所有配置(如示例所示)。

但是,如果使用python3环境运行tox:

$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

将会看到预期的行为,其中sys.version_info在python2.7和python3.6版本之间是正确的。

答案 1 :(得分:0)

在env名称中尝试'36'而不是'3.6'。如果这对你的环境没有帮助声明basepython:

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

添加一个命令以显示python版本:

commands =
    {envpython} --version

我通常在[testenv]中添加命令并从其他所有测试部分调用它:

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

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