beanbaginc / kgb

Python function spy support for unit tests
MIT License
48 stars 5 forks source link

Usage with asynctest #4

Closed errietta closed 5 years ago

errietta commented 5 years ago

Not necessarily an issue, but I was wondering if you've tried playing with this and asynctest (or any other ways of testing async code?). This is the way that I found that seemed to work:

import asynctest

from mandate import Cognito
from kgb import SpyAgency

class testApi(SpyAgency, asynctest.TestCase):
    async def test_register(self):
        cog = Cognito(
            'user_pool_id',  # user pool id
            'client_id',
            user_pool_region='eu-west-2',
            username='test@test.com'
        )

        cog.add_base_attributes(email='test@test.com')

        async with cog.get_client() as client:
            mock = asynctest.CoroutineMock()
            self.spy_on(client.sign_up, call_fake=mock)
            await cog.register('test@test.com', 'password')
            mock.assert_awaited()

Basically create CoroutineMock, call_fake with it, then use the asynctest.TestCase API. So only use kgb to do the spying. Seems like a bit of a nasty hack, but the core patching API (so also this API) is harder to use/more inflexible, which is why I wanted to use kgb to begin with 😂

errietta commented 5 years ago

Forgot to say, was just wondering if there's a better way that you know of

chipx86 commented 5 years ago

Sorry, I haven't done any work with asynctest and kgb. No idea what the best approach would be. I'm certainly open to improvements in this area. It's just not something we've had an immediate need for in our work.