Skip to main content

Fully asynchronous unit test

Project description

asyncUnittest

asyncUnittest is created for fully asynchronous unit test,which is inspired by unittest. I hear that the newest unittest has supported asynchronous test already. Well, maybe that is a better choice.


Install · Usage ·


Install

asyncUnittest in PyPI

pip install asyncUnittest

Usage

simple example

from asyncUnittest import AsyncTestCase


class Test(AsyncTestCase):
    enable_test = True

    @classmethod
    async def setUpClass(cls) -> None:
        print('setUpClass')

    async def setUp(self) -> None:
        print('setUp')

    async def test1(self):
        print('test1')

    async def test2(self):
        print('test2')
        self.assertTrue(0)

    def test3(self):
        print('test3')

    async def tearDown(self) -> None:
        print('tearDown')

    @classmethod
    async def tearDownClass(cls) -> None:
        print('tearDownClass')

AsyncTestCase

If you are familiar with unittest, this is intuitive. Implement your test case class by inheriting 'AsyncTestCase' imported from asyncUnittest.

enable_test

The property 'enable_test' is True as default. You do not need to explicitly define it. If you want to close the inheriting test case, assign False to it.

setUpClass

The test case runs with the first step of calling and awaiting 'setUpClass', before anything else happens. It could be omitted.

setUp

All test methods run concurrently. Before each test method runs, its belonging 'setUp' would be called and awaited.It could be omitted.

test method

A test method may be a normal function or coroutine function.

Any method is a test method as long as string 'test' is in their name's lower case copy, starting or ending with no ' _'. For example, test_connect and cancel_test are OK, but _test_connect, _test_connect_, _cancel_test_ and cancel_test_ will be ignored. You can close some test method by adding '_'.

assertTrue

"assertTrue" is an assertion method, which asserts all its arguments true. You can find more assertion methods in your editor hint. More will be implemented in the future.

tearDown

After each test method completes, its belonging 'tearDown' would be called and awaited.It could be omitted.

tearDownClass

After everything else completes in the test case, the last step is to call and await 'tearDownClass'.It could be omitted.

run

from asyncUnittest import run

When "run" is called, any test case inherited from 'AsyncTestCase' would run. So you can run test cases in one file:

class Test(AsyncTestCase):
    ...


class Test2(AsyncTestCase):
    ...


run()

Or you can import test cases to the target scope and run them concurrently. For example:

from Gear_test import TestGear
from method_run_when_test import TestInstance_run_when
from run_when_test import TestRunWhen
from asyncUnittest import run

run()

result

The test result would be printed in the python console:

setUpClass
setUp
tearDown
setUp
setUp
setUp
test3
tearDown
test1
test2
tearDown
tearDown
tearDownClass
2021-01-21 15:35:00.339 | WARNING  | asyncUnittest.async_unittest:test_one_method:99 - Test method enable_test is complete.Left:['<class '__main__.Test'>.test1', '<class '__main__.Test'>.test2', '<class '__main__.Test'>.test3']

2021-01-21 15:35:00.341 | WARNING  | asyncUnittest.async_unittest:test_one_method:99 - Test method test3 is complete.Left:['<class '__main__.Test'>.test1', '<class '__main__.Test'>.test2']

2021-01-21 15:35:00.341 | WARNING  | asyncUnittest.async_unittest:test_one_method:99 - Test method test1 is complete.Left:['<class '__main__.Test'>.test2']

2021-01-21 15:35:00.342 | WARNING  | asyncUnittest.async_unittest:test_one_method:99 - Test method test2 is complete.Left:[]

2021-01-21 15:35:00.342 | ERROR    | asyncUnittest.async_unittest:run:126 - Traceback (most recent call last):
  File "/Users/90houlaoheshang/Desktop/asyncUnittest/asyncUnittest/async_unittest.py", line 90, in test_one_method
    await asyncio.create_task(test_function())
  File "/Users/90houlaoheshang/Desktop/asyncUnittest/test.py", line 19, in test2
    self.assertTrue(0)
  File "/Users/90houlaoheshang/Desktop/asyncUnittest/asyncUnittest/async_unittest.py", line 30, in assertTrue
    self.assertEqual(True, *args)
  File "/Users/90houlaoheshang/Desktop/asyncUnittest/asyncUnittest/async_unittest.py", line 26, in assertEqual
    raise AssertionError(
AssertionError: 
(True, 0)

items do not equal each other.

2021-01-21 15:35:00.342 | ERROR    | asyncUnittest.async_unittest:run:127 - Spent seconds: 0.008960776000000004, error count:1

"Test method * is complete.Left:[...]" shows which test method is completed and which are still running.

"Traceback" shows error tracebacks.

"Spent seconds...error count..." shows spent time and error count.

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

asyncUnittest-1.5.6.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

asyncUnittest-1.5.6-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file asyncUnittest-1.5.6.tar.gz.

File metadata

  • Download URL: asyncUnittest-1.5.6.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.7

File hashes

Hashes for asyncUnittest-1.5.6.tar.gz
Algorithm Hash digest
SHA256 2dfece6a1c5b2ec08d7dfd2fe5c748251e6566b5b34be71c3754cdf0100fe2c7
MD5 ccd198f672cd5dc7513457f04fdcda1d
BLAKE2b-256 260318fd1b30a0fa205a7b5c08427e931a0000875dfe99d7d7d5a0bb72c79dc2

See more details on using hashes here.

File details

Details for the file asyncUnittest-1.5.6-py3-none-any.whl.

File metadata

  • Download URL: asyncUnittest-1.5.6-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.7

File hashes

Hashes for asyncUnittest-1.5.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5ddd7beb53ef1ed1ca132a32bb0677d91c2cb2653fc11ae7df54b65d6b092291
MD5 8d4676f49cace9a264fc90ced7cb268d
BLAKE2b-256 ed12284277ef03d6da0a88e022b6e282a4d8dd4ac552dbc31c6654ca87ff89d8

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