Skip to main content

Asyncio testing utils

Project description

Test utils for the aio asyncio framework

Build status

https://travis-ci.org/phlax/aio.testing.svg?branch=master

Installation

Install with:

pip install aio.testing

@aiotest decorator

aio.testing provides a method decorator for running asyncio-based tests

import unittest
import asyncio

from aio.testing import aiotest


class MyTestCase(unittest.TestCase):

    @aiotest
    def test_example():
        yield from asyncio.sleep(2)
        self.assertTrue(True)

Prior to the test running asyncio.get_new_loop() is called and set using asyncio.set_event_loop().

On completion of the test asyncio.set_event_loop() is again called with the original event loop.

@aiofuturetest decorator

If your code needs to test long-running tasks, you can use the @aiofuturetest decorator

Any (async) setup required can be done in the body of the test function which returns a test callback

The callback returned should be a coroutine.

import unittest
import asyncio

from aio.testing import aiofuturetest


class MyFutureTestCase(unittest.TestCase):

    @aiofuturetest
    def test_example():
        yield from asyncio.sleep(2)

        @asyncio.coroutine
        def callback_test(self):
            yield from asyncio.sleep(2)
            self.assertTrue(True)

        # this function is called 5 seconds after being returned
        return callback_test

After the test_example function returns, the decorator waits for 5 seconds and then runs the tests in the callback_test function

As with aiotest, the test is run in a separate loop.

@aiofuturetest decorator with timeout

You can specify how many seconds to wait before running the callback tests by setting the timeout value

import unittest
import asyncio

from aio.testing import aiofuturetest


class MyFutureTestCase(unittest.TestCase):

    @aiofuturetest(timeout=10)
    def test_example():
        yield from asyncio.sleep(2)

        @asyncio.coroutine
        def callback_test(self):
            yield from asyncio.sleep(2)
            self.assertTrue(True)

        # this function is called 10 seconds after being returned
        return callback_test

@aiofuturetest decorator with sleep

Sometimes a test needs to wait for some time after services have been stopped and the test loop has been destroyed.

You can specify how many seconds to wait after running the callback tests by setting the sleep value

import unittest
import asyncio

from aio.testing import aiofuturetest


class MyFutureTestCase(unittest.TestCase):

    @aiofuturetest(sleep=10)
    def test_example():
        yield from asyncio.sleep(2)

        @asyncio.coroutine
        def callback_test(self):
            yield from asyncio.sleep(2)
            self.assertTrue(True)

        return callback_test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aio.testing-0.0.2.tar.gz (4.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page