johnklee / fpu

Functional programming utility
MIT License
4 stars 4 forks source link

`pytest.mark.parameterize` not supported inside unittest class #57

Closed wkCircle closed 12 months ago

wkCircle commented 1 year ago

@pytest.mark.parameterize doesn't support decorating functions inside unitttest.TestCase class with self/classmethod/staticmethod. Unfortunately, pytest has claimed that they won't fix such issue (see pytest issue 541).

We need to find alternatives to replace @pytest.mark.parameterize functionality for testing.

Besides, keep in mind the AAA structure as mentioned before by @johnklee .

johnklee commented 1 year ago

Module tests/unit/parameterized.py is implemented for this issue. We could leverage this module to write parameterized tests this way:

from tests.unit import parameterized

class ListTest(unittest.TestCase):
  """Unit test cases of interfact List."""
  ...
    @parameterized.named_parameters(
      dict(
          testcase_name='case 1',
          test_func=lambda e: e == 1,
          expected_result=True),
      dict(
          testcase_name='case 2',
          test_func=lambda e: e == 5,
          expected_result=False),
      dict(
          testcase_name='case 3',
          test_func=lambda e: e > 1,
          expected_result=True),
  )
  def test_api_exists(self, testcase_name, test_func, expected_result):
    """Tests the method `exists` on FPU list."""
    alist = fl(1, 2, 3)

    self.assertEqual(
        alist.exists(test_func), expected_result)
johnklee commented 1 year ago

If all the sub test cases pass, it will look like: pass

If there is any test cases fail, it will look like: fail

johnklee commented 1 year ago

Ready in https://github.com/johnklee/fpu/pull/63

johnklee commented 1 year ago

Sorry for the inconvenience in accidentally closing this issue. We still have some works to do from this issue. So I reopen it again.

wkCircle commented 12 months ago

No worries, guess we can close the issue now.