gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:15:54 26 4
gpt4 key购买 nike

我有一些代码,其中控制逻辑根据 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 将使用该环境进行所有配置(如示例所示)。

但是,如果使用 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 版本之间都是正确的。

关于python - 运行测试时如何让 tox 尊重系统版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43897854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com