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

assertRaises used as an expression is converted to a statement, resulting in a syntax error #25

Open jdufresne opened 5 years ago

jdufresne commented 5 years ago

Example input:

class MyTest(unittest.TestCase):
    def test_thing(self):
        self.my_custom_assert(self.assertRaises(IOError, my_func))

Output

@@ -1,3 +1,5 @@
+import pytest
 class MyTest(unittest.TestCase):
     def test_thing(self):
-        self.my_custom_assert(self.assertRaises(IOError, my_func))
+        self.my_custom_assert(with pytest.raises(IOError):
+            my_func())

There is now a with statement as the first argument passed to my_custom_assert. pytest.raises also supports an expression syntax, so perhaps that should be used here as well.

This came up in a real life project:

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

htgoebel commented 5 years ago

This should be easy to fiy by adding here a test whether the current nodes parent node is an expression - or something like this.

We's apprechiae a pull-request. Please mind adding a test-case, too. Thanks.