Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Fixes #304, add ignore_warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Jun 7, 2020
1 parent f6d7e9d commit 7fa7990
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion _unittests/ut_pycode/test_extunittest.py
Expand Up @@ -7,7 +7,8 @@
import warnings
import pandas

from pyquickhelper.pycode import ExtTestCase, unittest_require_at_least
from pyquickhelper.pycode import (
ExtTestCase, unittest_require_at_least, ignore_warnings)
from pyquickhelper.pandashelper import df2rst
from pyquickhelper import __file__ as rootfile

Expand Down Expand Up @@ -249,6 +250,7 @@ def zoo2():
except Exception as e:
self.assertIn('older than', str(e))

@ignore_warnings(RuntimeWarning)
def test_sparse_arr(self):
from numpy import array
from scipy.sparse import coo_matrix, csc_matrix, csr_matrix
Expand Down
3 changes: 2 additions & 1 deletion src/pyquickhelper/pycode/__init__.py
Expand Up @@ -15,7 +15,8 @@
from .unittestclass import (
ExtTestCase, skipif_appveyor, skipif_travis, skipif_circleci,
skipif_linux, skipif_vless, skipif_azure_macosx,
skipif_azure, skipif_azure_linux, unittest_require_at_least
skipif_azure, skipif_azure_linux, unittest_require_at_least,
ignore_warnings
)
from .pytest_helper import run_test_function, TestExecutionError
from .utils_tests import main_wrapper_tests
Expand Down
22 changes: 20 additions & 2 deletions src/pyquickhelper/pycode/unittestclass.py
Expand Up @@ -8,6 +8,7 @@
import unittest
import warnings
import decimal
from functools import wraps
import pprint
from logging import getLogger, INFO, StreamHandler
from contextlib import redirect_stdout, redirect_stderr
Expand Down Expand Up @@ -50,8 +51,7 @@ def _format_str(s):
"""
if hasattr(s, "replace"):
return "'{0}'".format(s)
else:
return s
return s

def assertNotEmpty(self, x):
"""
Expand Down Expand Up @@ -624,3 +624,21 @@ def unittest_require_at_least(mod, version, msg=""):
msg = "Module '{}' is older than '{}' (= '{}'). {}".format(
mod, version, v, msg)
return unittest.skip(msg)


def ignore_warnings(warns):
"""
Catches warnings.
@param warns warnings to ignore
"""
def wrapper(fct):
if warns is None:
raise AssertionError( # pragma: no cover
"warns cannot be None for '{}'.".format(fct))
def call_f(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", warns)
return fct(self)
return call_f
return wrapper

0 comments on commit 7fa7990

Please sign in to comment.