Skip to content

Add support for async test coroutines#292

Merged
deathaxe merged 5 commits into
masterfrom
feat/deferrable-asyncio
May 31, 2026
Merged

Add support for async test coroutines#292
deathaxe merged 5 commits into
masterfrom
feat/deferrable-asyncio

Conversation

@deathaxe
Copy link
Copy Markdown
Member

This PR adds AsyncTestCase and AsyncViewTestCase for testing asyncio coroutines, which interact with Sublime Text.

Both classes work pretty much like DeferrableTestCase, except they can await coroutines.

Initial implementation inherits AsyncTestCase from DeferrableTestCase to explicitly provide a new API with async coroutine support. Each coroutine function is scheduled for execution in sublime_aio's default event loop. Underlying test method awaits completion via yield future.done to enable coroutines to run synchronous tasks in main thread via sublime.set_timeout().

Yielding from async coroutines is not supported.

@rwols: May help with asyncio testing in LSP.

Copy link
Copy Markdown
Contributor

@rwols rwols left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will definitely help. I have a custom hacky test case defined here for now: https://github.com/sublimelsp/LSP/pull/2880/changes#diff-71bab432ef1fd86da70970d919de9bcb94824da23dfce20552da20342a30d8da

I haven't read the full diff here, but please make sure the following (class)methods can also be async:

    @classmethod
    async def setUpClass(cls) -> None:
        pass

    @classmethod
    async def tearDownClass(cls) -> None:
        pass

    async def doCleanups(self) -> None:
        pass

@rwols
Copy link
Copy Markdown
Contributor

rwols commented May 30, 2026

One more suggestion: instead of hardcoding the dependency on sublime_aio, perhaps allow supplying a function for starting a coroutine using dependency injection.

@deathaxe
Copy link
Copy Markdown
Member Author

Note, doCleanups() not being something to be called or overridden by test cases, directly. Cleanup hooks registered via addCleanup() can be async coroutines and are started very much the same way as setUp() and tearDown().

That being said, this PR takes pretty much the same approach by extending DeferrableTestCase. This is basically the only way without completely rewriting stdlib's unittest library. The only optimization possible would be to run setUp, callTestMethod and tearDown in a single coroutine. The currrent approach however allows mixed sync and async test cases in a single class.

@deathaxe deathaxe merged commit 53cb7bc into master May 31, 2026
14 checks passed
@deathaxe deathaxe deleted the feat/deferrable-asyncio branch May 31, 2026 16:51
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

Successfully merging this pull request may close these issues.

2 participants