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

Fatal error when assertTrue is checked with a generator #16

Closed Debilski closed 7 years ago

Debilski commented 8 years ago

The test here is quite nonsensical (because a generator is of course always True) and I am actually happy that unittest2pytest pointed that out so I could fix the test. However, I do believe that it should fail a little more gracefully:

$ cat test_gen.py
import unittest
class TestGen(unittest.TestCase):
    def test_gen(self):
        self.assertTrue(x for x in range(1))

$ unittest2pytest test_gen.py
Traceback (most recent call last):
  File "$HOME/anaconda3/bin/unittest2pytest", line 11, in <module>
    sys.exit(main())
  File "$HOME/anaconda3/lib/python3.5/site-packages/unittest2pytest/__main__.py", line 30, in main
    raise SystemExit(lib2to3.main.main(fixes.__name__))
  File "$HOME/anaconda3/lib/python3.5/lib2to3/main.py", line 259, in main
    options.processes)
  File "$HOME/anaconda3/lib/python3.5/lib2to3/refactor.py", line 706, in refactor
    items, write, doctests_only)
  File "$HOME/anaconda3/lib/python3.5/lib2to3/refactor.py", line 301, in refactor
    self.refactor_file(dir_or_file, write, doctests_only)
  File "$HOME/anaconda3/lib/python3.5/lib2to3/refactor.py", line 747, in refactor_file
    *args, **kwargs)
  File "$HOME/anaconda3/lib/python3.5/lib2to3/refactor.py", line 354, in refactor_file
    tree = self.refactor_string(input, filename)
  File "$HOME/anaconda3/lib/python3.5/lib2to3/refactor.py", line 386, in refactor_string
    self.refactor_tree(tree, name)
  File "$HOME/anaconda3/lib/python3.5/lib2to3/refactor.py", line 426, in refactor_tree
    self.traverse_by(self.bmi_post_order_heads, tree.post_order())
  File "$HOME/anaconda3/lib/python3.5/lib2to3/refactor.py", line 502, in traverse_by
    new = fixer.transform(node, results)
  File "$HOME/anaconda3/lib/python3.5/site-packages/unittest2pytest/fixes/fix_self_assert.py", line 404, in transform
    process_arg(results['arglist'])
  File "$HOME/anaconda3/lib/python3.5/site-packages/unittest2pytest/fixes/fix_self_assert.py", line 378, in process_arg
    name, equal, value = arg.children
ValueError: not enough values to unpack (expected 3, got 2)

Expected outcome:

$ unittest2pytest test_gen.py 
RefactoringTool: Refactored test_gen.py
--- test_gen.py (original)
+++ test_gen.py (refactored)
@@ -1,5 +1,5 @@
 import unittest
 class TestGen(unittest.TestCase):
     def test_gen(self):
-        self.assertTrue(x for x in range(1))
+        assert (x for x in range(1))

RefactoringTool: Files that need to be modified:
RefactoringTool: test_gen.py

(And possibly a warning that this test is nonsensical.)

htgoebel commented 7 years ago

Thanks for reporting this issue.