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

Undefined variable `pattern` when converting self.raisesRegexp #23

Closed boralyl closed 6 years ago

boralyl commented 6 years ago

When running the following to convert asserts:

unittest2pytest -f self_assert test/test_foo.py

I consistently get the following output with the below test:

     def test_foo(self):
-        with self.assertRaisesRegexp(ValueError, 'Invalid'):
+        with pytest.raises(ValueError) as excinfo:
             raise ValueError('Invalid')
+        assert re.search(pattern, excinfo.value)

Notice the variable pattern which isn't defined anywhere. This is with unittest2pytest==0.3

Also, it would be nicer if it didn't use re.search and just use the match kwarg to raises. e.g.

     def test_foo(self):
-        with self.assertRaisesRegexp(ValueError, 'Invalid'):
+        with pytest.raises(ValueError, match='Invalid'):
             raise ValueError('Invalid')
nicoddemus commented 6 years ago

Definitely, thanks for the report. We would appreciate a PR if you have the time, otherwise hope someone can tackle this soon. 👍

boralyl commented 6 years ago

Sure thing: https://github.com/pytest-dev/unittest2pytest/pull/24