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(self):
        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(self):
        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(self):
        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(self):
        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.1.tar.gz (3.9 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for aio.testing-0.0.1.tar.gz
Algorithm Hash digest
SHA256 37e11741998b4dbc7f6bbeb6078aa770b2530f68cddbeb63505d9d9af609e0bf
MD5 81542ddc1953c0319ad760b98bbb90bd
BLAKE2b-256 730699f1ab3d0e7cbf5f557bfc9a8e78c8fffea9ba7ebd4b41c2d4a1701cb84c

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