首页 > 解决方案 > 从 pytest 隐藏 DeprecationWarning

问题描述

我正在为调用 TensorFlow 2.0.1 的代码编写一些测试。该库导入 imp 模块,当我运行 pytest 时会触发弃用警告。我试图消除这个警告,但没有成功。

这是我的测试模块:

import pytest
import warnings

from fclib.models.dilated_cnn import create_dcnn_model

def test_create_dcnn_model():
    with pytest.deprecated_call():
        create_dcnn_model(seq_len=1, max_cat_id=[30, 120])  # calls tensorflow.keras code

的输出pytest test_dcnn.py

[...]
======================================== FAILURES ========================================
_________________________________ test_create_dcnn_model _________________________________

    def test_create_dcnn_model():
        with pytest.deprecated_call():
>           create_dcnn_model(seq_len=1, max_cat_id=[30, 120])
E           Failed: DID NOT WARN. No warnings of type (<class 'DeprecationWarning'>, <class 'PendingDeprecationWarning'>) was emitted. The list of emitted warnings is: [].

fclib/tests/test_dcnn.py:11: Failed
---------------------------------- Captured stderr call ----------------------------------
2020-07-07 03:15:03.162833: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA
2020-07-07 03:15:03.174538: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2095190000 Hz
2020-07-07 03:15:03.177318: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55ef3370ff20 executing computations on platform Host. Devices:
2020-07-07 03:15:03.177351: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Host, Default Version
==================================== warnings summary ====================================
/data/anaconda3/envs/forecasting_env/lib/python3.6/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py:15
  /data/anaconda3/envs/forecasting_env/lib/python3.6/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py:15: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================================ short test summary info =================================
FAILED fclib/tests/test_dcnn.py::test_create_dcnn_model - Failed: DID NOT WARN. No warn...
============================== 1 failed, 1 warning in 2.39s ==============================

所以它抱怨没有警告,但打印输出却说有警告。这里发生了什么事?

如果我删除该with pytest.deprecated_call()行,我会得到:

def test_create_dcnn_model():
    # with pytest.deprecated_call():
        create_dcnn_model(seq_len=1, max_cat_id=[30, 120])
================================== test session starts ===================================
platform linux -- Python 3.6.10, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /data/forecasting/fclib
collected 1 item                                                                         

fclib/tests/test_dcnn.py .                                                         [100%]

==================================== warnings summary ====================================
/data/anaconda3/envs/forecasting_env/lib/python3.6/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py:15
  /data/anaconda3/envs/forecasting_env/lib/python3.6/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py:15: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============================== 1 passed, 1 warning in 2.00s ==============================

所以它通过了,但警告信息仍然存在。我怎样才能完全摆脱该消息?

标签: python-3.xpytestdeprecation-warning

解决方案


推荐阅读