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)
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.

Mockoon Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source. It has been built wi

mockoon 4.4k Dec 30, 2022
A small automated test structure using python to test *.cpp codes

Get Started Insert C++ Codes Add Test Code Run Test Samples Check Coverages Insert C++ Codes you can easily add c++ files in /inputs directory there i

Alireza Zahiri 2 Aug 03, 2022
A mocking library for requests

httmock A mocking library for requests for Python 2.7 and 3.4+. Installation pip install httmock Or, if you are a Gentoo user: emerge dev-python/httm

Patryk Zawadzki 452 Dec 28, 2022
Python version of the Playwright testing and automation library.

🎭 Playwright for Python Docs | API Playwright is a Python library to automate Chromium, Firefox and WebKit browsers with a single API. Playwright del

Microsoft 7.8k Jan 02, 2023
HTTP traffic mocking and testing made easy in Python

pook Versatile, expressive and hackable utility library for HTTP traffic mocking and expectations made easy in Python. Heavily inspired by gock. To ge

Tom 305 Dec 23, 2022
A python bot using the Selenium library to auto-buy specified sneakers on the nike.com website.

Sneaker-Bot-UK A python bot using the Selenium library to auto-buy specified sneakers on the nike.com website. This bot is still in development and is

Daniel Hinds 4 Dec 14, 2022
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
pytest plugin for manipulating test data directories and files

pytest-datadir pytest plugin for manipulating test data directories and files. Usage pytest-datadir will look up for a directory with the name of your

Gabriel Reis 191 Dec 21, 2022
Given some test cases, this program automatically queries the oracle and tests your Cshanty compiler!

The Diviner A complement to The Oracle for compilers class. Given some test cases, this program automatically queries the oracle and tests your compil

Grant Holmes 2 Jan 29, 2022
DUCKSPLOIT - Windows Hacking FrameWork using Reverse Shell

Ducksploit Install Ducksploit Hacker setup raspberry pico Download https://githu

2 Jan 31, 2022
bulk upload files to libgen.lc (Selenium script)

LibgenBulkUpload bulk upload files to http://libgen.lc/librarian.php (Selenium script) Usage ./upload.py to_upload uploaded rejects So title and autho

8 Jul 07, 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
Integration layer between Requests and Selenium for automation of web actions.

Requestium is a Python library that merges the power of Requests, Selenium, and Parsel into a single integrated tool for automatizing web actions. The

Tryolabs 1.7k Dec 27, 2022
A rewrite of Python's builtin doctest module (with pytest plugin integration) but without all the weirdness

The xdoctest package is a re-write of Python's builtin doctest module. It replaces the old regex-based parser with a new abstract-syntax-tree based pa

Jon Crall 174 Dec 16, 2022
Um scraper feito em python que gera arquivos de excel baseados nas tier lists do site LoLalytics.

LoLalytics-scraper Um scraper feito em python que gera arquivos de excel baseados nas tier lists do site LoLalytics. Começando por um único script com

Kevin Souza 1 Feb 19, 2022
CNE-OVS-SIT - OVS System Integration Test Suite

CNE-OVS-SIT - OVS System Integration Test Suite Introduction User guide Discussion Introduction CNE-OVS-SIT is a test suite for OVS end-to-end functio

4 Jan 09, 2022
Python Projects - Few Python projects with Testing using Pytest

Python_Projects Few Python projects : Fast_API_Docker_PyTest- Just a simple auto

Tal Mogendorff 1 Jan 22, 2022
:game_die: Pytest plugin to randomly order tests and control random.seed

pytest-randomly Pytest plugin to randomly order tests and control random.seed. Features All of these features are on by default but can be disabled wi

pytest-dev 471 Dec 30, 2022
Ab testing - The using AB test to test of difference of conversion rate

Facebook recently introduced a new type of offer that is an alternative to the current type of bidding called maximum bidding he introduced average bidding.

5 Nov 21, 2022
UX Analytics & A/B Testing

UX Analytics & A/B Testing

Marvin EDORH 1 Sep 07, 2021