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

Class converted to invalid code #51

Open nedbat opened 3 years ago

nedbat commented 3 years ago

One of my test files (https://github.com/nedbat/coveragepy/blob/coverage-5.4/tests/test_testing.py) was converted into invalid code. I don't know what it is about that file that caused it, though it is a bit twisty: it's a test of test assert helper methods.

Here is a stripped-down file that shows the same behavior:

import pytest

from coverage.backunittest import TestCase

class TestingTest(TestCase):
    """Tests of helper methods on `backunittest.TestCase`."""

    def test_assert_count_equal(self):
        self.assertCountEqual(set(), set())
        with self.assertRaises(AssertionError):
            self.assertCountEqual({1,2,3}, set())
        with self.assertRaises(AssertionError):
            self.assertCountEqual({1,2,3}, {4,5,6})

Running unittest2pytest -w test_testing.py, the file becomes:

import pytest

from coverage.backunittest import TestCase
"""Tests of helper methods on `backunittest.TestCase`."""
deftest_assert_count_equal(self):
    self.assertCountEqual(set(), set())
    with pytest.raises(AssertionError):
        self.assertCountEqual({1,2,3}, set())
        withpytest.raises(AssertionError):
        self.assertCountEqual({1,2,3}, {4,5,6})
nedbat commented 3 years ago

It also changed this file:

from coverage.backunittest import TestCase
from coverage.backward import iitems, binary_bytes, bytes_to_ints

class BackwardTest(TestCase):
    """Tests of things from backward.py."""

    def test_iitems(self):
        d = {'a': 1, 'b': 2, 'c': 3}
        items = [('a', 1), ('b', 2), ('c', 3)]
        self.assertCountEqual(list(iitems(d)), items)

    def test_binary_bytes(self):
        byte_values = [0, 255, 17, 23, 42, 57]
        bb = binary_bytes(byte_values)
        self.assertEqual(len(bb), len(byte_values))
        self.assertEqual(byte_values, list(bytes_to_ints(bb)))

to:

from coverage.backunittest import TestCase
from coverage.backward import iitems, binary_bytes, bytes_to_ints
"""Tests of things from backward.py."""
deftest_iitems(self):
    d = {'a': 1, 'b': 2, 'c': 3}
    items = [('a', 1), ('b', 2), ('c', 3)]
    self.assertCountEqual(list(iitems(d)), items)

    def test_binary_bytes(self):
    byte_values = [0, 255, 17, 23, 42, 57]
    bb = binary_bytes(byte_values)
    assert len(bb) == len(byte_values)
    assert byte_values == list(bytes_to_ints(bb))
spookylukey commented 3 years ago

I also noticed this. Several issues: