Visitar URL original
Class converted to invalid code · Issue #51 · pytest-dev/unittest2pytest · GitHub
Skip to content

Class converted to invalid code #51

Open

Description

@nedbat

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})

Activity

  1. nedbat commented on Jan 31, 2021

    @nedbat
    Author

    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))
    
  2. spookylukey commented on Sep 18, 2021

    @spookylukey

    I also noticed this. Several issues:

    • deftest_xxx instead of def test_xxx
    • self parameter wasn't removed
    • all comments got stripped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions