Skip to content

Convert unittest.TestCase.subTest to pytest.parametrize #41

Description

@Pierre-Sassoulas

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 😄

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions