Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pierre-Sassoulas opened this issue Jul 11, 2022 · 2 comments
Open

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

Pierre-Sassoulas opened this issue Jul 11, 2022 · 2 comments

Comments

@Pierre-Sassoulas
Copy link

Pierre-Sassoulas commented Jul 11, 2022

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 😄

@dannysepler
Copy link
Owner

dannysepler commented Jul 14, 2022

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!

@dannysepler
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants