wolever / parameterized

Parameterized testing with any Python test framework
Other
833 stars 105 forks source link

Is it possible to write unittests for async functions using @parameterized.expand? #137

Closed SadiaAfrinPurba closed 1 year ago

SadiaAfrinPurba commented 2 years ago

Hi,

I want to test the async functions using the @parameterized.expand decorator. There is no error while executing the tests, but it shows zero test coverage.

I'm using the @async_test decorator to test the async functions and without @parameterized.expand they work fine, meaning that it shows 100% code coverage.

from aioitertools.tests.helpers import async_test

class TestService(TestCase):
    def setUp(self) -> None:
        self.mock_query_details = {"column_id": 0}

    @parameterized.expand([
        ({"col1": ["", "a", "b", "c", "d", "", "", ""], "cluster": [0, 1, 1, 1, 1, 0, 0, 0]},
         [['a'], ['b'], ['c'], ['d']]),
        ({"col1": [" ", "a", "b", "c", "d", " ", " ", " "], "cluster": [0, 1, 1, 1, 1, 0, 0, 0]},
         [['a'], ['b'], ['c'], ['d']]),
    ])
    @async_test
    async def test_function(self, mock_data, expected):
        data_frame = pd.DataFrame(mock_data)
        num_rows = 4

        service = Service()
        actual = await service.function(query_detail=self.mock_query_details, data_frame=data_frame,
                                                       num_rows=num_rows)

        self.assertCountEqual(actual, expected)

Do I need to follow any extra steps for using the @parameterized.expand decorator to test the async function?

dbungert commented 1 year ago

Depending on python version this may work when paried with unittest.IsolatedAsyncioTestCase, or you may run into https://github.com/python/cpython/issues/101486

I started some fixes for this - https://github.com/wolever/parameterized/compare/master...dbungert:parameterized:expand-async - but am unsure about handling of pre-python 3.5.

wolever commented 1 year ago

This has been added in 0.9.0 thanks to , which will be on pypi in a day or so (https://github.com/wolever/parameterized/pull/135; thanks @Ronserruya!)

kkozmic commented 1 year ago

I too am waiting for this. Anything else holding 0.9.0 up @wolever ?

gaby commented 1 year ago

@wolever Any chance to get v0.9.0 released? Thanks!

wolever commented 1 year ago

It's up! https://pypi.org/project/parameterized/0.9.0/

gaby commented 1 year ago

@wolever Thanks!