dannysepler / pytestify

Automatically convert unittests to pytest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convert ``unittest.TestCase.subTest`` to ``pytest.parametrize``

Pierre-Sassoulas opened this issue · comments

Hello, thank you for creating this tool !

The following code:

import unittest

try:
    import numpy  # pylint: disable=unused-import

    HAS_NUMPY = True
except ImportError:
    HAS_NUMPY = False


@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
class BrainNumpyCoreFromNumericTest(unittest.TestCase):

    numpy_functions = (("sum", "[1, 2]"),)

    def test_numpy_function_calls_inferred_as_ndarray(self):
        """
        Test that calls to numpy functions are inferred as numpy.ndarray
        """
        licit_array_types = (".ndarray",)
        for func_ in self.numpy_functions:
            with self.subTest(typ=func_):
                inferred_values = list(self._inferred_numpy_func_call(*func_))
                self.assertEqual(
                    len(inferred_values),
                    1,
                    msg=f"Too much inferred value for {func_[0]:s}",
                )
                self.assertIn(
                    inferred_values[-1].pytype(),
                    licit_array_types,
                    msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})",
                )

Give the following result:

import pytest

try:
    import numpy  # pylint: disable=unused-import

    HAS_NUMPY = True
except ImportError:
    HAS_NUMPY = False

from astroid import builder


@pytest.mark.skipif(not HAS_NUMPY, "This test requires the numpy library.")
class TestBrainNumpyCoreFromNumeric:
    """
    Test the numpy core fromnumeric brain module
    """

    numpy_functions = (("sum", "[1, 2]"),)

    def test_numpy_function_calls_inferred_as_ndarray(self):
        """
        Test that calls to numpy functions are inferred as numpy.ndarray
        """
        licit_array_types = (".ndarray",)
        for func_ in self.numpy_functions:
            with self.subTest(typ=func_):
                inferred_values = list(self._inferred_numpy_func_call(*func_))
                assert (
                    len(inferred_values) == 1
                ), f"Too much inferred value for {func_[0]:s}"
                assert (
                    inferred_values[-1].pytype() in licit_array_types
                ), f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})"

I think self.subTest(typ=func_): should be converted to a pytest.parametrize.
Reference documentation

Context : I'm trying to migrate astroid's test to pytest style, there's 36 subtests 😄

that's awesome, best of luck converting astroid!

i'll admit, this will actually be very hard to do, because it's not a simple syntax replacement. as you mentioned, one can use pytest.mark.parametrize, or there's even a pytest-subtests package!

after some thought, if we were to use pytest-subtests, this would be a simpler find-and-replace. so maybe we can wait and see if pytest-dev/pytest-subtests#71 gets merged

we'd maybe still want a feature flag to opt-in or opt-out of this behavior, but we can come to that later