Skip to main content

Enhance the standard unittest package with features for testing asyncio libraries

Project description

The package asynctest is built on top of the standard unittest module and cuts down boilerplate code when testing libraries for asyncio.

Currently, asynctest targets the “selector” model, hence, some features will not (yet?) work with Windows’ proactor.

Author & license

Authored by Martin Richard <martius@martiusweb.net> and licensed under the Apache 2 license.

See the AUTHORS file for a comprehensive list of the authors.

Documentation

https://readthedocs.org/projects/asynctest/badge/

Full documentation is available at http://asynctest.readthedocs.org/en/latest/,

Features

TestCases

  • Initialize and close a loop created for each test (it can be configurated), if the loop uses a selector, it will be updated with a TestSelector object wrapping the original selector (see below),

  • if the test function is a coroutine function or returns a coroutine, it will run on the loop,

  • TestCase.setUp() and TestCase.tearDown() can be coroutine functions,

  • control post-test checks with @fail_on, for instance, the test fail if the loop didn’t run, some optional checks can be activated,

  • ClockedTestCase allows to control the loop clock and run timed events without waiting the wall clock.

Mock and CoroutineMock

  • CoroutineMock is a new Mock class which mocks a coroutine function, and returns a coroutine when called,

  • MagicMock supports asynchronous context managers and asynchronous iterators,

  • NonCallableMock, Mock and CoroutineMock can return CoroutineMock objects when its attributes are get if there is a matching attribute in the spec (or spec_set) object which is a coroutine function,

  • patch(), patch.object(), patch.multiple() return a MagickMock or CoroutineMock object by default, according to the patched target,

  • patch(), patch.object(), patch.multiple() handle generators and coroutines and their behavior can be controled when the generator or coroutine pauses,

  • all the patch() methods can decorate coroutine functions,

  • mock_open() returns a MagickMock object by default.

  • return_once() can be used with Mock.side_effect to return a value only once when a mock is called.

Selectors

The module asynctest.selector provides classes to mock objects performing IO (files, sockets, etc).

  • FileMock is a special type of mock which represents a file. FileMock.fileno() returns a special value which allows to identify uniquely the mock,

  • SocketMock is a special type of FileMock which uses socket.socket as spec,

  • TestSelector is a custom selector able to wrap a real selector implementation and deal with FileMock objects, it can replace a selector loop by calling loop._selector = TestSelector(loop._selector), and will intercept mock so they don’t get registered to the actual selector.

  • set_read_ready() and set_write_ready() to force read and write event callbacks to be scheduled on the loop, as if the selector scheduled them.

Helpers

  • the coroutine exhaust_callbacks(loop) returns once all the callbacks which should be called immediately are executed, which is useful when the test author needs to assert things which are not yet executed by the loop.

Roadmap

I hope I will find time to develop and release the following features:

  • set of warnings against common mistakes

  • proactor support

Tests

asynctest is unit tested. You can run asynctest test suite with this command:

$ PYTHONPATH=. python -m unittest 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

asynctest-0.12.1.tar.gz (41.7 kB view details)

Uploaded Source

Built Distribution

asynctest-0.12.1-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file asynctest-0.12.1.tar.gz.

File metadata

  • Download URL: asynctest-0.12.1.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for asynctest-0.12.1.tar.gz
Algorithm Hash digest
SHA256 afd030357379ea52193c14ed73f97866a8076ba83e402f12dc0c717c7723a18d
MD5 c48f9c8dc6a0c9e2e3deda562caa5e6a
BLAKE2b-256 78558f6d119a4d098966d7a9d123f75578e3c7316601580bf774a61cfc5c26bd

See more details on using hashes here.

File details

Details for the file asynctest-0.12.1-py3-none-any.whl.

File metadata

File hashes

Hashes for asynctest-0.12.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3cc304cfb59608d09fb302acbe086feafa010b0a9254b1b04c4c75f2265767fc
MD5 7d260e8f3728e45416d1be53b9c87b13
BLAKE2b-256 16b753e6435495fc065a5dff718531f79b68272809a3e03066577a0f5fba7af8

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