Async Support

TestSlide’s DSL supports asynchronous I/O testing.

For that, you must declare all of these as async:

  • Hooks: around, before and after.
  • Examples.
  • Memoize before.
  • Functions.

like this:

from testslide.dsl import context

@context
def testing_async_code(context):
  @context.around
  async def around(self, example):
    await example()  # Note that this must be awaited!

  @context.before
  async def before(self):
    pass

  @context.after
  async def after(self):
    pass

  @context.memoize_before
  async def memoize_before(self):
    return "memoize_before"

  @context.function
  async def function(self):
    return "function"

  @context.example
  async def example(self):
    assert self.memoize_before == "memoize_before"
    assert self.function == "function"

The test runner will create a new event look to execute each example.

Note

You can not mix async and sync stuff for the same example. If your example is async, then all its hooks and memoize before must also be async.

Note

It is not possible to support async @context.memoize. They depend on __getattr__ to work, which has no async support. Use @context.memoize_before instead.