GithubHelp home page GithubHelp logo

kwarunek / aiounittest Goto Github PK

View Code? Open in Web Editor NEW
58.0 4.0 7.0 68 KB

Test python asyncio-based code with ease.

Home Page: http://aiounittest.readthedocs.io

License: MIT License

Python 100.00%
asyncio unittest async-python

aiounittest's People

Contributors

encukou avatar hroncok avatar kwarunek avatar reivilibre avatar zeskbest avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

aiounittest's Issues

Compatibility with pytest 8

I am getting a test failure when built with pytest 8.2.1.

=================================== FAILURES ===================================
_______________ TestAsyncCaseWithCustomLoop.test_await_async_add _______________
../../BUILDROOT/python-aiounittest-1.3.1-16.fc41.x86_64/usr/lib/python3.12/site-packages/aiounittest/helpers.py:137: in wrapper
    future = asyncio.ensure_future(ret, loop=loop)
/usr/lib64/python3.12/asyncio/tasks.py:695: in ensure_future
    return loop.create_task(coro_or_future)
/usr/lib64/python3.12/asyncio/base_events.py:456: in create_task
    self._check_closed()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=False closed=True debug=False>

    def _check_closed(self):
        if self._closed:
>           raise RuntimeError('Event loop is closed')
E           RuntimeError: Event loop is closed

/usr/lib64/python3.12/asyncio/base_events.py:541: RuntimeError
=============================== warnings summary ===============================
tests/test_asynctestcase_get_event_loop.py:30
  /builddir/build/BUILD/aiounittest-1.3.1/tests/test_asynctestcase_get_event_loop.py:30: DeprecationWarning: There is no current event loop
    self.my_loop = asyncio.get_event_loop()

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ============================
FAILED tests/test_asynctestcase_get_event_loop.py::TestAsyncCaseWithCustomLoop::test_await_async_add
============== 1 failed, 8 passed, 2 xfailed, 1 warning in 0.89s ===============

Please switch away from nose

Hi,
Nose is deprecated since 2015, nobody should be using it anymore. Please switch to something more modern like pytest, and remove nose from your setup.py.

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/

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.

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

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.

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.

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

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.

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.

Mock

Hi there!
What about unittest.mock in asyncio?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.