【问题标题】:Configute tox.ini to ignore library during tests with py27将 tox.ini 配置为在使用 py27 进行测试期间忽略库
【发布时间】:2022-01-25 15:40:08
【问题描述】:

我有以下 tox.ini 配置文件:

# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
envlist = py27, py37, pycodestyle, pylint

[testenv]
commands =
    pytest --junitxml=unit-tests.xml --cov=xivo --cov-report term --cov-report xml:coverage.xml xivo
deps =
    -rrequirements.txt
    -rtest-requirements.txt
    pytest-cov

[testenv:pycodestyle]
# E501: line too long (80 chars)
commands =
    -sh -c 'pycodestyle --ignore=E501 xivo > pycodestyle.txt'
deps =
    pycodestyle
whitelist_externals =
    sh

[testenv:pylint]
commands =
    -sh -c 'pylint --rcfile=/usr/share/xivo-ci/pylintrc xivo > pylint.txt'
deps =
    -rrequirements.txt
    -rtest-requirements.txt
    pylint
whitelist_externals =
    sh

[testenv:black]
skip_install = true
deps = black
commands = black --skip-string-normalization .

[testenv:linters]
skip_install = true
deps =
    flake8
    flake8-colors
    black
commands =
    black --skip-string-normalization --check .
    flake8

[testenv:integration]
basepython = python3.7
usedevelop = true
deps = -rintegration_tests/test-requirements.txt
changedir = integration_tests
passenv =
    WAZO_TEST_DOCKER_OVERRIDE_EXTRA
commands =
    make test-setup
    pytest -v {posargs}
whitelist_externals =
    make
    sh

[flake8]
# E501: line too long (80 chars)
# W503: line break before binary operator
# E203: whitespace before ':' warnings
# NOTE(sileht):
# * xivo_config.py not python3 ready
exclude = .tox,.eggs,xivo/xivo_config.py
show-source = true
ignore = E501, E203, W503
max-line-length = 99
application-import-names = xivo

我已经更新了我的 requirements.txt 文件,以便将 Marshmallow 的版本从 3.0.0b14 升级到 3.10.0;像这样:

https://github.com/wazo-platform/wazo-lib-rest-client/archive/master.zip
https://github.com/wazo-platform/wazo-auth-client/archive/master.zip
https://github.com/wazo-platform/xivo-bus/archive/master.zip
cheroot==6.5.4
flask==1.0.2
netifaces==0.10.4
psycopg2==2.7.7
pyyaml==3.13
python-consul==1.1.0
marshmallow==3.10.0
six==1.12.0
stevedore==1.29.0

现在我的问题是,当我运行 tox -e py37 时一切正常,但是当我运行此命令 tox -e py27 时,它失败了。我知道问题是 Python 2.7 不支持 Marshmallow 3.10.0;所以我想做的是更改tox.ini 文件,以便在运行此命令tox -e py27 时忽略棉花糖库测试。我是tox的新手,所以我不知道该怎么做。欢迎任何帮助,谢谢。

【问题讨论】:

    标签: python unit-testing marshmallow tox


    【解决方案1】:

    您需要使用 pytest 标记标记这些“棉花糖”测试。

    https://docs.pytest.org/en/6.2.x/example/markers.html

    例如

    @pytest.mark.marshmellow
    def test_xxx():
        ...
    

    然后你需要运行pytest -m "not marshmellow"

    由于您只想为 Python 2.7 执行此操作,因此您需要创建一个新的 testenv。

    例如

    [testenv:py27]
    commands = pytest -m "not marshmellow" ...
    

    【讨论】:

    • 这一切都说,你的方法有点......不是主流:-) 通常,如果你需要保持 Python 2.7 支持,你要么保留旧的 Marshmellow 版本,要么创建两个分支,一个用于 2.7 和一个用于 Python 3,或者为 Python 2.7 支持创建最后一个版本,然后放弃支持。
    • 感谢您的解决方案,我还有一个问题;我将如何告诉我创建的新 testenv (testenv:py27) 忽略从 requirements.txt 安装 Marshmallow;因为我仍然收到此错误could not install deps [-rrequirements.txt
    • 好的。如果您甚至无法安装新的 Marshmallow 版本,那么您还需要遵循 phd 的回答。由于您在问题中没有提到它,我假设您只面临运行时错误。 ? 对于 tox 的问题,发布tox -rvv的结果通常是个好主意
    【解决方案2】:

    最后一个支持 Python 2.7 的 Marshmallow 版本是 2.21.0。你可以标记你的requirements.txt 为不同版本的 Python 安装不同版本的 Marshmallow:

    marshmallow<3; python_version == '2.7'
    marshmallow==3.10.0; python_version >= '3.6'
    

    https://www.python.org/dev/peps/pep-0496/

    【讨论】:

    • 感谢您的回复,我尝试了您上面建议的两行,但是没有用;我仍然遇到同样的错误 (could not install deps [-rrequirements.txt)
    • 使用 Python 2.7 和 3.7 在pip install -r requirements.txt 中为我工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2019-07-19
    • 2012-06-15
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    相关资源
    最近更新 更多