pytest-dev / unittest2pytest

helps rewriting Python `unittest` test-cases into `pytest` test-cases
GNU General Public License v3.0
128 stars 27 forks source link

assertRaisesRegex should convert the regex pattern to the new assert #26

Closed jdufresne closed 5 years ago

jdufresne commented 5 years ago

Example input:

class MyTest(unittest.TestCase):
    def test_thing(self):
        with self.assertRaisesRegex(Exception, "My exception message"):
            my_func()

Output:

@@ -1,4 +1,7 @@
+import pytest
+import re
 class MyTest(unittest.TestCase):
     def test_thing(self):
-        with self.assertRaisesRegex(Exception, "My exception message"):
+        with pytest.raises(Exception) as excinfo:
             my_func()
+        assert re.search(pattern, excinfo.value)

In the output, pattern should be "My exception message".

This came up in a real life project:

https://github.com/python-pillow/Pillow/blob/7bf5246b93cc89cfb9d6cca78c4719a943b10585/Tests/test_color_lut.py#L40

jdufresne commented 5 years ago

Another issue with the output, excinfo.value is not a str type, it is an Exception. So even with the pattern converted it still fails. I think the result should be:

assert re.search("My exception message", str(excinfo.value))
boralyl commented 5 years ago

@jdufresne This is fixed on develop, just not officially released: https://github.com/pytest-dev/unittest2pytest/pull/24

jdufresne commented 5 years ago

Thanks!