如何在 运行 测试时让 tox 尊重系统版本?
How to get tox to respect system version when running tests?
我有一些代码,其中控制逻辑根据 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 :)
当 运行 和 Python2.7 时,tox 将使用该环境进行所有配置(如示例所示)。
但是,如果 tox 运行 使用 python3 环境:
$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 版本之间是正确的。
尝试在环境名称中使用“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 …
我有一些代码,其中控制逻辑根据 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 :)
当 运行 和 Python2.7 时,tox 将使用该环境进行所有配置(如示例所示)。
但是,如果 tox 运行 使用 python3 环境:
$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 版本之间是正确的。
尝试在环境名称中使用“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 …