如果你从一个最小的开始
test_one.py
def test_one():
from collections import Hashable
简单的
setup.py
:
from setuptools import setup, find_packages
if __name__ == '__main__':
setup(
name="depwarntest",
version="0.0.1",
description="test to get DeprecationWarning in code on 3.7",
long_description = "more details soon",
author_email="a.van.der.neut@ruamel.eu",
author="Anthon van der Neut",
license="MIT",
url="",
packages=find_packages(),
)
一个基本
tox.ini
:
[tox]
envlist = py37,py36,py27
[testenv]
commands =
/bin/bash -c 'pytest test_*.py'
deps =
pytest
[pytest]
filterwarnings =
error::DeprecationWarning
error::PendingDeprecationWarning
并运行
tox
,您将得到一个很好的干净异常,因为
import
:
==================================================================================== FAILURES =====================================================================================
____________________________________________________________________________________ test_one _____________________________________________________________________________________
def test_one():
> from collections import Hashable
test_one.py:6:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
<frozen importlib._bootstrap>:1032: in _handle_fromlist
???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
name = 'Hashable'
def __getattr__(name):
# For backwards compatibility, continue to make the collections ABCs
# through Python 3.6 available through the collections module.
# Note, no new collections ABCs were added in Python 3.7
if name in _collections_abc.__all__:
obj = getattr(_collections_abc, name)
import warnings
warnings.warn("Using or importing the ABCs from 'collections' instead "
"of from 'collections.abc' is deprecated, "
"and in 3.8 it will stop working",
> DeprecationWarning, stacklevel=2)
E DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
.tox/py37/lib/python3.7/collections/__init__.py:52: DeprecationWarning
============================================================================ 1 failed in 0.31 seconds =============================================================================
在
py37
而
py36
和
py27
运行良好。
有趣的是,如果将测试文件更改为
from collections import Hashable
def test_one():
from collections import Hashable
运行
托克斯
会很顺利的
PY37
也。
如果将模块级导入移到另一个
test_XYZ.py
文件
.
为了
ruamel.yaml
这意味着所有的模块级导入
卢米尔
在测试文件中需要移动到方法/函数;测试中依赖于
ruamel.yaml.YAML()
需要使用生成器;并且模块级
yaml_object()
也需要用特殊的方式来处理。
额外的
托克斯
target通过进行一致性测试来帮助测试逐步移动:
# deprecation warning fail
[testenv:dwf]
basepython = python3.7
commands =
/bin/sed 's/collections.abc/collections/' -i .tox/dwf/lib/python3.7/site-packages/ruamel/yaml/comments.py
/bin/bash -c 'pytest --maxfail=2 _test/test_[a-cz]*.py'
这里是已经更正的来源
comments.py
恢复,仅对已调整的模块进行测试。
ted -e py37,dwf
应通过第一个(再次通过321个测试)并在第二个目标上失败。