Closed danyeaw closed 4 years ago
Is this the correct expectation, that unittest2pytest should be able to parse these test cases and produce valid pytests including fixtures?
No. unittest2pytest is only meant to help converting test-cases. Please do not expect it to clean up your code, Although the generated code should be less messed up than in your last case. Did you try running the fixes one after each other?
You offer for help is very welcome. The best starting point is to create small test-cases for reproducing the error. This also gives a clean sign what the error actually is.
1) Re. "unittest.TestCase", this should be easy to fix by changing
… '(' 'TestCase' ')' …
into
… '(' ['unittest' '.'] 'TestCase' ')' …
here
Re "self": This should be easy by adding a fixer to `` matching a pattern along this one (untested):
PATTERN = """
funcdef< 'def' any
parameters< '(' args=any> any+ >
suite=suite
>
"""
The lib2to3 fixer fix_tuple_params might be of help here.
Of course this fixer must only touch sub-classes of TestCase, thus you might want to add some maker to the changed class in FixRemoveClass - or ensure the func-fixer is processed prior to the class-fixer. Please also mind adding a non-matching test case.
3) The intention error seem to stem from the assignments jsut above def teardown
.
Please also provide a separate pull-request for each of the cases, as this eases the review a lot. Thanks.
1. Re. "unittest.TestCase", this should be easy to fix by changing `… '(' 'TestCase' ')' …` into `… '(' ['unittest' '.'] 'TestCase' ')' …` [here](https://github.com/pytest-dev/unittest2pytest/blob/develop/unittest2pytest/fixes/fix_remove_class.py#L54)
I spent some time trying to get this pattern working over the last couple of days. I created a new test case:
class TestAssertNotEqual(unittest.TestCase):
def test_you(self):
self.assertNotEqual(abc, 'xxx')
and modified the pattern as follows:
PATTERN = """
classdef< 'class' name=any '(' ['unittest' '.'] 'TestCase' ')' ':'
suite=suite
>
"""
Unfortunately the test fails because it doesn't remove the class definition. I tried a bunch of other combinations like '(' dotted_name< 'unittest.TestCase' > ')'
, removing the optional brackets, etc, but I haven't been able to get the pattern to recognize unittest.TestCase
.
This issue is stale, so I am going to go ahead and close.
Thanks for the great tool, the assert fixer has been a huge time saver for a couple of largest projects I have upgraded to Pytest. :heart:
Here is one of the simpler tests I am working with:
It looks like in the remove_class PATTERN, it is only looking for
TestCase
. So that is the first issue. If I modify the imports like so:I get the following output after rerunning unittest2pytest:
It didn't actually get rid of
self
in the function definition or remove the TestCase import. Things unfortunately don't get better with a more complex test case.After running
unittest2pytest
I get:The code is no longer valid Python, and there would be no way to automatically reformat this even with a tool like Black.
I would be glad to help try to fix this. Is this the correct expectation, that unittest2pytest should be able to parse these test cases and produce valid pytests including fixtures?