Package: asyncUnittest
asyncUnittestasyncUnittest 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·InstallasyncUnittest inPyPIpipinstallasyncUnittestUsagesimple examplefromasyncUnittestimportAsyncTestCaseclassTest(AsyncTestCase):enable_test=True@classmethodasyncdefsetUpClass(cls)->None:print('setUpClass')asyncdefsetUp(self)->None:print('setUp')asyncdeftest1(self):print('test1')asyncdeftest2(self):print('test2')self.assertTrue(0)deftest3(self):print('test3')asyncdeftearDown(self)->None:print('tearDown')@classmethodasyncdeftearDownClass(cls)->None:print('tearDownClass')AsyncTestCaseIf you are familiar with unittest, this is intuitive. Implement your test case class by inheriting 'AsyncTestCase'
imported from asyncUnittest.enable_testThe 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.setUpClassThe test case runs with the first step of calling and awaiting 'setUpClass', before anything else happens. It could be
omitted.setUpAll test methods run concurrently. Before each test method runs, its belonging 'setUp' would be called and awaited.It
could be omitted.test methodA 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.tearDownAfter each test method completes, its belonging 'tearDown' would be called and awaited.It could be omitted.tearDownClassAfter everything else completes in the test case, the last step is to call and await 'tearDownClass'.It could be
omitted.runfromasyncUnittestimportrunWhen "run" is called, any test case inherited from 'AsyncTestCase' would run. So you can run test cases in one file:classTest(AsyncTestCase):...classTest2(AsyncTestCase):...run()Or you can import test cases to the target scope and run them concurrently. For example:fromGear_testimportTestGearfrommethod_run_when_testimportTestInstance_run_whenfromrun_when_testimportTestRunWhenfromasyncUnittestimportrunrun()resultThe test result would be printed in the python console:setUpClass
setUp
tearDown
setUp
setUp
setUp
test3
tearDown
test1
test2
tearDown
tearDown
tearDownClass2021-01-2115:35:00.339|WARNING|asyncUnittest.async_unittest:test_one_method:99-Testmethodenable_testiscomplete.Left:['<class '__main__.Test'>.test1','<class '__main__.Test'>.test2','<class '__main__.Test'>.test3']2021-01-2115:35:00.341|WARNING|asyncUnittest.async_unittest:test_one_method:99-Testmethodtest3iscomplete.Left:['<class '__main__.Test'>.test1','<class '__main__.Test'>.test2']2021-01-2115:35:00.341|WARNING|asyncUnittest.async_unittest:test_one_method:99-Testmethodtest1iscomplete.Left:['<class '__main__.Test'>.test2']2021-01-2115:35:00.342|WARNING|asyncUnittest.async_unittest:test_one_method:99-Testmethodtest2iscomplete.Left:[]2021-01-2115:35:00.342|ERROR|asyncUnittest.async_unittest:run:126-Traceback(mostrecentcalllast):File"/Users/90houlaoheshang/Desktop/asyncUnittest/asyncUnittest/async_unittest.py",line90,intest_one_methodawaitasyncio.create_task(test_function())File"/Users/90houlaoheshang/Desktop/asyncUnittest/test.py",line19,intest2self.assertTrue(0)File"/Users/90houlaoheshang/Desktop/asyncUnittest/asyncUnittest/async_unittest.py",line30,inassertTrueself.assertEqual(True,*args)File"/Users/90houlaoheshang/Desktop/asyncUnittest/asyncUnittest/async_unittest.py",line26,inassertEqualraiseAssertionError(AssertionError:(True,0)itemsdonotequaleachother.2021-01-2115:35:00.342|ERROR|asyncUnittest.async_unittest:run:127-Spentseconds:0.008960776000000004,errorcount: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.