Pytest let you raise a FutureWarning
as an error, this way it points to the code location where the warning was raised, the simplest way is through the warning flag
pytest -W error::FutureWarning test_script.py
or inside test_script.py
through pytestmark
pytestmark = pytest.mark.filterwarnings("error::FutureWarning")
you can read more about specifying the action being taken on a Warning here and here.
Example
Considering the following dummy example test_transform.py
script
# 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
and running on terminal
pytest test_transform.py > logs.log
the logs show me exactly that PowerTransformer
raised the 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 ======================================================