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 details)

Uploaded Source

File details

Details for the file aio.testing-0.0.2.tar.gz.

File metadata

  • Download URL: aio.testing-0.0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aio.testing-0.0.2.tar.gz
Algorithm Hash digest
SHA256 73f6867a05c288daab29aecbc060122e39b28c8eca9f264d2d0f709255c96ea0
MD5 8928f5172a31f97ed08d4b2cdb55fb85
BLAKE2b-256 3925a9f481f6a67bf081e20c794dc7a45b95fe23979605a2081646ec78f3a634

See more details on using hashes here.

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