Skip to content

Coroutine support for asynchronous tests #3077

@sixten-hilborn

Description

@sixten-hilborn

Description

C++20 coroutines are increasingly used for asynchronous APIs, but writing tests for coroutine-based code typically requires blocking adapters (e.g. sync_wait) or custom wrappers. This adds friction and does not work in event-loop-driven environments where blocking inside a test body is not possible.

It would therefore be useful if Catch2 supported test cases defined as coroutines, for example:

TEST_CASE_ASYNC("my async test")
{
    auto result = co_await async_operation();
    CHECK(result == 42);
}

Because Catch2 supports C++14, this feature would only be available when compiling with C++20/coroutine support. It would have to be guarded behind a build config flag.

Additional context

We have implemented a proof of concept in a private fork where we added a very basic coroutine task type. All scheduling and event-loop integration happen in user code, which keeps the approach flexible while adding minimal complexity inside Catch2. This was enough to solve related issues such as #1755 (we also target Emscripten).

I've looked at other C++ testing frameworks, but none currently seem to support coroutines natively. However, popular frameworks in other languages (e.g. C#, Python, JavaScript) support async tests, so the concept itself isn't novel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions