Pytest 让您将 aFutureWarning
作为错误引发,这样它指向引发警告的代码位置,最简单的方法是通过警告标志
pytest -W error::FutureWarning test_script.py
或test_script.py
通过pytestmark在里面
pytestmark = pytest.mark.filterwarnings("error::FutureWarning")
您可以在此处和此处阅读有关指定对警告采取的操作的更多信息。
例子
考虑以下虚拟示例test_transform.py
脚本
# test_transform.py
import pytest
pytestmark = pytest.mark.filterwarnings("error::FutureWarning")
def test_transformer_boxcox():
from sklearn.preprocessing import PowerTransformer
pt = PowerTransformer('box-cox', False)
assert True
def test_transformer_yeo_johnson():
from sklearn.preprocessing import PowerTransformer
pt = PowerTransformer('yeo-johnson', False)
assert True
并在终端上运行
pytest test_transform.py > logs.log
日志向我展示了PowerTransformer
引发FutureWarning
.
===================================================== test session starts =====================================================
platform linux -- Python 3.7.6, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/jovyan/work
collected 2 items
test_transform.py FF [100%]
========================================================== FAILURES ===========================================================
___________________________________________________ test_transformer_boxcox ___________________________________________________
def test_transformer_boxcox():
from sklearn.preprocessing import PowerTransformer
> pt = PowerTransformer('box-cox', False)
test_transform.py:11:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = (<[AttributeError("'PowerTransformer' object has no attribute 'copy'") raised in repr()] PowerTransformer object at 0x7fa0bda14d50>, 'box-cox', False)
kwargs = {}, extra_args = 1, args_msg = 'standardize=False'
@wraps(f)
def inner_f(*args, **kwargs):
extra_args = len(args) - len(all_args)
if extra_args <= 0:
return f(*args, **kwargs)
# extra_args > 0
args_msg = ['{}={}'.format(name, arg)
for name, arg in zip(kwonly_args[:extra_args],
args[-extra_args:])]
args_msg = ", ".join(args_msg)
warnings.warn(f"Pass {args_msg} as keyword args. From version "
f"{version} passing these as positional arguments "
> "will result in an error", FutureWarning)
E FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error
/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:72: FutureWarning
________________________________________________ test_transformer_yeo_johnson _________________________________________________
def test_transformer_yeo_johnson():
from sklearn.preprocessing import PowerTransformer
> pt = PowerTransformer('yeo-johnson', False)
test_transform.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = (<[AttributeError("'PowerTransformer' object has no attribute 'copy'") raised in repr()] PowerTransformer object at 0x7fa0bd02f750>, 'yeo-johnson', False)
kwargs = {}, extra_args = 1, args_msg = 'standardize=False'
@wraps(f)
def inner_f(*args, **kwargs):
extra_args = len(args) - len(all_args)
if extra_args <= 0:
return f(*args, **kwargs)
# extra_args > 0
args_msg = ['{}={}'.format(name, arg)
for name, arg in zip(kwonly_args[:extra_args],
args[-extra_args:])]
args_msg = ", ".join(args_msg)
warnings.warn(f"Pass {args_msg} as keyword args. From version "
f"{version} passing these as positional arguments "
> "will result in an error", FutureWarning)
E FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error
/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:72: FutureWarning
=================================================== short test summary info ===================================================
FAILED test_transform.py::test_transformer_boxcox - FutureWarning: Pass standardize=False as keyword args. From version 1.0 ...
FAILED test_transform.py::test_transformer_yeo_johnson - FutureWarning: Pass standardize=False as keyword args. From version...
====================================================== 2 failed in 0.71s ======================================================