配置 tox.ini 以在使用 py27 进行测试时忽略库

Configute tox.ini to ignore library during tests with py27

我有以下 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 时忽略 Marshmallow 库测试。我是毒物新手,所以我不确定该怎么做。欢迎任何帮助,谢谢。

您需要用 pytest 标记标记那些“Marshmellow”测试。

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 的 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/