unittest.TestCase.assertRaisesRegex throws DeprecationWarning

Peter Otten __peter__ at web.de
Tue Jul 31 18:56:53 EDT 2018


Uri Even-Chen wrote:

> Hi,
> 
> We are using unittest.TestCase.assertRaisesRegex in our Django project
> (Speedy Net / Speedy Match - https://github.com/urievenchen/speedy-net),
> and I always prefer calling Python function parameters by name. So we call
> self.assertRaisesRegex with parameters (keyword arguments)
> expected_exception, expected_regex and callable - as documented on
> 
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaisesRegex
> . You can see our code on
> https://github.com/urievenchen/speedy-net/blob/master/speedy/core/accounts/tests/test_models.py
> . When I run tests regularly (with "./manage.py test") they pass, But when
> I run tests with "python -W error::DeprecationWarning manage.py test",
> (with Python 3.5.5), I receive errors like this:
> 
> DeprecationWarning: 'callable' is an invalid keyword argument for this
> function
> 
> See the full errors here:
> https://travis-ci.org/urievenchen/speedy-net/jobs/410482655 (by the way,
> with Python 3.4.6 the tests pass).

No, the function to be tested is not run at all (it looks like callable was 
called callable_obj back then):

$ cat unittest_regex.py
import unittest

def foo(*args, **kw):
    print("RUNNING FOO")

class T(unittest.TestCase):
    def test_baz(self):
        self.assertRaisesRegex(
            expected_exception=ValueError,
            expected_regex="bar",
            callable=foo
        )

if __name__ == "__main__":
    unittest.main()
$ python3.4 unittest_regex.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK


> Do you know what is the problem and why I receive these deprecation
> warnings?

Apparently they want you to use the context manager ;)
Like

with self.assertRaisesRegex(ValueError, "bar"):
    foo()

Clean, readable, robust -- but no keyword args.





More information about the Python-list mailing list