Test python asyncio-based code with ease.

Overview

aiounittest

image0 image1

Info

The aiounittest is a helper library to ease of your pain (and boilerplate), when writing a test of the asynchronous code (asyncio). You can test:

  • synchronous code (same as the unittest.TestCase)
  • asynchronous code, it supports syntax with async/await (Python 3.5+) and asyncio.coroutine/yield from (Python 3.4)

In the Python 3.8 (release note) and newer consider to use the unittest.IsolatedAsyncioTestCase. Builtin unittest module is now asyncio-featured.

Installation

Use pip:

pip install aiounittest

Usage

It's as simple as use of unittest.TestCase. Full docs at http://aiounittest.readthedocs.io.

import asyncio
import aiounittest


async def add(x, y):
    await asyncio.sleep(0.1)
    return x + y

class MyTest(aiounittest.AsyncTestCase):

    async def test_async_add(self):
        ret = await add(5, 6)
        self.assertEqual(ret, 11)

    # or 3.4 way
    @asyncio.coroutine
    def test_sleep(self):
        ret = yield from add(5, 6)
        self.assertEqual(ret, 11)

    # some regular test code
    def test_something(self):
        self.assertTrue(True)

Library provides some additional tooling:

License

MIT

Comments
  • test_sync_async_add: After closing the default event loop, set a new one

    test_sync_async_add: After closing the default event loop, set a new one

    TestAsyncCase.test_sync_async_add leaves the default loop closed. If TestAsyncCaseWithCustomLoop.test_await_async_add runs right after it, it will fail. As far as I can see from the other test methods, the default loop should be reset if it's closed.

    Usually, this issue is masked by tests that run in between these two and re-set the default event loop as a side effect. It can be reproduced with pytest -k 'test_await_async_add or test_sync_async_add', or on Python 3.11 with #21 merged (cc @hroncok).

    opened by encukou 1
  • Re-export top-level imports to satisfy Mypy

    Re-export top-level imports to satisfy Mypy

    Using Mypy (at least in strict mode) with the library as it currently is causes errors.

    import aiounittest
    
    
    class ExampleTest(aiounittest.AsyncTestCase):
        def test_example(self) -> None:
            self.assertEqual(1, 2 - 1)
    
    tests/test_example.py:4: error: Name "aiounittest.AsyncTestCase" is not defined
    tests/test_example.py:4: error: Class cannot subclass "AsyncTestCase" (has type "Any")
    

    It seems to be good practice to 're-export' (using __all__) all the things that you import into a module and wish to allow others to use as an import. I don't think this is actually functionally different, but just a convention. IDEs (auto-import feature) and Mypy pick up on this, at the very least.

    Signed-off-by: Olivier Wilkinson (reivilibre) [email protected] I am happy for these changes to be available under the project's licence (MIT).

    opened by reivilibre 1
  • patch on method/class level

    patch on method/class level

    Hi :)

    class Test(AsyncTestCase):
        @patch('some_path.features_topic.send', new=AsyncMock())
        async def test_smoke(self, mocked_topic):
            ...
    

    I get a TypeError: test_smoke() missing 1 required positional argument: 'mocked_topic'

    If I patch within the test with a with patch(..) as patched_object: it works fine.

    opened by jorotenev 1
  • Event loop is closed after a test but new default event loop is not created

    Event loop is closed after a test but new default event loop is not created

    When running multiple test cases using aiounittest under Python 3.8, the second and all the following unit test cases fail because there is no default event loop set in the MainThread. The following exception is raised when the unit tests are executed.

    Traceback (most recent call last):
      File "lib\site-packages\aiounittest\helpers.py", line 130, in wrapper
        loop = get_brand_new_default_event_loop()
      File "lib\site-packages\aiounittest\helpers.py", line 117, in get_brand_new_default_event_loop
        old_loop = asyncio.get_event_loop()
      File "lib\asyncio\events.py", line 639, in get_event_loop
        raise RuntimeError('There is no current event loop in thread %r.'
    RuntimeError: There is no current event loop in thread 'MainThread'.
    

    This exception occurs because get_brand_new_default_event_loop tries to get the default event loop after it has been closed in the decorator.

    opened by tmaila 1
  • Latest version isn't published to PyPI

    Latest version isn't published to PyPI

    The PyPI version does not check if a test case method is a coroutine, only that it is a callable. This differs from what's in this repo, which I think is the more correct behavior:

        def __getattribute__(self, name):
            attr = super().__getattribute__(name)
            if name.startswith('test_') and callable(attr):
                return async_test(attr, loop=self.get_event_loop())
            else:
                return attr
    
    opened by jcmcken 1
  • setUp and tearDown can't be async

    setUp and tearDown can't be async

    The AsyncTestCase assumes that only methods named test_* can be async, which isn't necessarily a valid assumption. This restriction should be removed, since you already test if the given function is a coroutine.

    opened by jcmcken 1
  • Can't call python -m unittest my_test_module.MyTestCase.test_my_async_test

    Can't call python -m unittest my_test_module.MyTestCase.test_my_async_test

    Can't call one test method directly:

    $ python -m unittest my_test_module.MyTestCase.test_my_async_test
      File ".../unittest/loader.py", line 205, in loadTestsFromName
        test = obj()
    TypeError: test_my_async_test() missing 1 required positional argument: 'self'
    
    

    The reason is that the object given by __getattribute__ is tested against type.MethodType, but async_test(...) is not.

    opened by Alcolo47 0
  • Fixed an issue where RuntimeError was thrown when running multiple tests

    Fixed an issue where RuntimeError was thrown when running multiple tests

    Fixes issue #10: RuntimeError was thrown the decorator trying to get the default event loop after the old event loop was closed. No default event loop existed and the asyncio threw a RuntimeErroe exception.

    opened by tmaila 0
  • mypy / static typing support

    mypy / static typing support

    Right now mypy is not aware that aiounittest subclasses TestCase, which is a bummer for static typing. One has to

    import aiounittest  # type: ignore
    

    which gives it the Any type. Apart from disabling type checking, this also prevents us from turning on mypy --strict mode which includes disallow_subclassing_any = True or the following error ensues:

    tests/test_case.py:15: error: Class cannot subclass 'AsyncTestCase' (has type 'Any')
    

    It seems like we only need to add an empty py.typed marker file as described in https://www.python.org/dev/peps/pep-0561/

    opened by vbraun 0
  • Initial Update

    Initial Update

    The bot created this issue to inform you that pyup.io has been set up on this repo. Once you have closed it, the bot will open pull requests for updates as soon as they are available.

    opened by pyup-bot 0
  • 1.4.1: pytest warnings

    1.4.1: pytest warnings

    Looks like latest pytest shows some warnings

    ============================================================================= warnings summary =============================================================================
    tests/test_asynctestcase.py:41
      /home/tkloczko/rpmbuild/BUILD/aiounittest-1.4.1/tests/test_asynctestcase.py:41: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
        def test_yield_async_add(self):
    
    tests/test_asynctestcase.py:56
      /home/tkloczko/rpmbuild/BUILD/aiounittest-1.4.1/tests/test_asynctestcase.py:56: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
        def test_yield_async_add(self):
    
    tests/test_asynctestcase_get_event_loop.py:38
      /home/tkloczko/rpmbuild/BUILD/aiounittest-1.4.1/tests/test_asynctestcase_get_event_loop.py:38: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
        def test_yield_async_add(self):
    
    -- Docs: https://docs.pytest.org/en/stable/warnings.html
    
    opened by kloczek 0
Releases(1.4.2)
  • 1.4.2(Jun 13, 2022)

    What's Changed

    • Don't run @asyncio.coroutine tests with Python 3.11 by @hroncok in https://github.com/kwarunek/aiounittest/pull/21
    • test_sync_async_add: After closing the default event loop, set a new one by @encukou in https://github.com/kwarunek/aiounittest/pull/22
    • Fixed deps for travis-ci py3.7

    New Contributors

    • @hroncok made their first contribution in https://github.com/kwarunek/aiounittest/pull/21
    • @encukou made their first contribution in https://github.com/kwarunek/aiounittest/pull/22

    Full Changelog: https://github.com/kwarunek/aiounittest/compare/1.4.1...1.4.2

    Source code(tar.gz)
    Source code(zip)
  • 1.4.1(Oct 22, 2021)

    What's Changed

    • Fix typo in documentation by @svisser in https://github.com/kwarunek/aiounittest/pull/17
    • Fix main example by @Zeskbest in https://github.com/kwarunek/aiounittest/pull/18
    • Re-export top-level imports to satisfy Mypy by @reivilibre in https://github.com/kwarunek/aiounittest/pull/19

    New Contributors

    • @svisser made their first contribution in https://github.com/kwarunek/aiounittest/pull/17
    • @Zeskbest made their first contribution in https://github.com/kwarunek/aiounittest/pull/18
    • @reivilibre made their first contribution in https://github.com/kwarunek/aiounittest/pull/19

    Full Changelog: https://github.com/kwarunek/aiounittest/compare/1.4.0...1.4.1

    Source code(tar.gz)
    Source code(zip)
a plugin for py.test that changes the default look and feel of py.test (e.g. progressbar, show tests that fail instantly)

pytest-sugar pytest-sugar is a plugin for pytest that shows failures and errors instantly and shows a progress bar. Requirements You will need the fol

Teemu 963 Dec 28, 2022
Test utility for validating OpenAPI documentation

DRF OpenAPI Tester This is a test utility to validate DRF Test Responses against OpenAPI 2 and 3 schema. It has built-in support for: OpenAPI 2/3 yaml

snok 103 Dec 21, 2022
Headless chrome/chromium automation library (unofficial port of puppeteer)

Pyppeteer Pyppeteer has moved to pyppeteer/pyppeteer Unofficial Python port of puppeteer JavaScript (headless) chrome/chromium browser automation libr

miyakogi 3.5k Dec 30, 2022
pytest plugin providing a function to check if pytest is running.

pytest-is-running pytest plugin providing a function to check if pytest is running. Installation Install with: python -m pip install pytest-is-running

Adam Johnson 21 Nov 01, 2022
Mimesis is a high-performance fake data generator for Python, which provides data for a variety of purposes in a variety of languages.

Mimesis - Fake Data Generator Description Mimesis is a high-performance fake data generator for Python, which provides data for a variety of purposes

Isaak Uchakaev 3.8k Dec 29, 2022
Plugin for generating HTML reports for pytest results

pytest-html pytest-html is a plugin for pytest that generates a HTML report for test results. Resources Documentation Release Notes Issue Tracker Code

pytest-dev 548 Dec 28, 2022
Airspeed Velocity: A simple Python benchmarking tool with web-based reporting

airspeed velocity airspeed velocity (asv) is a tool for benchmarking Python packages over their lifetime. It is primarily designed to benchmark a sing

745 Dec 28, 2022
Flexible test automation for Python

Nox - Flexible test automation for Python nox is a command-line tool that automates testing in multiple Python environments, similar to tox. Unlike to

Stargirl Flowers 941 Jan 03, 2023
An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers.

mitmproxy mitmproxy is an interactive, SSL/TLS-capable intercepting proxy with a console interface for HTTP/1, HTTP/2, and WebSockets. mitmdump is the

mitmproxy 29.7k Jan 02, 2023
A collection of testing examples using pytest and many other libreris

Effective testing with Python This project was created for PyConEs 2021 Check out the test samples at tests Check out the slides at slides (markdown o

Héctor Canto 10 Oct 23, 2022
FFPuppet is a Python module that automates browser process related tasks to aid in fuzzing

FFPuppet FFPuppet is a Python module that automates browser process related tasks to aid in fuzzing. Happy bug hunting! Are you fuzzing the browser? G

Mozilla Fuzzing Security 24 Oct 25, 2022
Free cleverbot without headless browser

Cleverbot Scraper Simple free cleverbot library that doesn't require running a heavy ram wasting headless web browser to actually chat with the bot, a

Matheus Fillipe 3 Sep 25, 2022
Checks for a 200 response from your subdomain list.

Check for available subdomains Written in Python, this terminal based application looks for a 200 response from the subdomain list you've provided. En

Sean 1 Nov 03, 2021
Tools for test driven data-wrangling and data validation.

datatest: Test driven data-wrangling and data validation Datatest helps to speed up and formalize data-wrangling and data validation tasks. It impleme

269 Dec 16, 2022
Generate random test credit card numbers for testing, validation and/or verification purposes.

Generate random test credit card numbers for testing, validation and/or verification purposes.

Dark Hunter 141 5 Nov 14, 2022
UUM Merit Form Filler is a web automation which helps automate entering a matric number to the UUM system in order for participants to obtain a merit

About UUM Merit Form Filler UUM Merit Form Filler is a web automation which helps automate entering a matric number to the UUM system in order for par

Ilham Rachmat 3 May 31, 2022
create custom test databases that are populated with fake data

About Generate fake but valid data filled databases for test purposes using most popular patterns(AFAIK). Current support is sqlite, mysql, postgresql

Emir Ozer 2.2k Jan 04, 2023
This repository has automation content to test Arista devices.

Network tests automation Network tests automation About this repository Requirements Requirements on your laptop Requirements on the switches Quick te

Netdevops Community 17 Nov 04, 2022
No longer maintained, please migrate to model_bakery

Model Mommy: Smart fixtures for better tests IMPORTANT: Model Mommy is no longer maintained and was replaced by Model Bakery. Please, consider migrati

Bernardo Fontes 917 Oct 04, 2022
A suite of benchmarks for CPU and GPU performance of the most popular high-performance libraries for Python :rocket:

A suite of benchmarks for CPU and GPU performance of the most popular high-performance libraries for Python :rocket:

Dion Häfner 255 Jan 04, 2023