Hi all,
NB: It’s worth prefacing this with acknowledging I’ve never used async before and apologies for any silly mistakes below…
I’ve been working on adding a an async
option to django’s test suite to:
- Better test async code
- In theory speed up testing
As a starting point I’ve tried to add async
to SimpleTestCase
without employing an @async_to_sync
decorator. The idea is to have an self.client.a.get
method that can asynchronously test a get
request.
That seems to require async
-ing all the way down and having finally gotten to this error I figured this was a decent point to ask advice:
runtests.py -k AsyncClientTest --parallel=1 --pdb
Testing against Django installed in '/django/django'
Traceback (most recent call last):
File "/usr/local/lib/python3.7/warnings.py", line 522, in _warn_unawaited_coroutine
warn(msg, category=RuntimeWarning, stacklevel=2, source=coro)
RuntimeWarning: coroutine 'SimpleTestCase.__init__' was never awaited
Traceback (most recent call last):
File "/django/tests/runtests.py", line 566, in <module>
options.start_at, options.start_after, options.pdb,
File "/django/tests/runtests.py", line 308, in django_tests
extra_tests=extra_tests,
File "/django/django/test/runner.py", line 682, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "/django/django/test/runner.py", line 569, in build_suite
tests = self.test_loader.discover(start_dir=label, **kwargs)
File "/usr/local/lib/python3.7/unittest/loader.py", line 349, in discover
tests = list(self._find_tests(start_dir, pattern))
File "/usr/local/lib/python3.7/unittest/loader.py", line 406, in _find_tests
full_path, pattern, namespace)
File "/usr/local/lib/python3.7/unittest/loader.py", line 460, in _find_test_path
return self.loadTestsFromModule(module, pattern=pattern), False
File "/usr/local/lib/python3.7/unittest/loader.py", line 124, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "/usr/local/lib/python3.7/unittest/loader.py", line 93, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "/usr/local/lib/python3.7/unittest/suite.py", line 24, in __init__
self.addTests(tests)
File "/usr/local/lib/python3.7/unittest/suite.py", line 57, in addTests
for test in tests:
TypeError: __init__() should return None, not 'coroutine'
You can see the latest here: https://github.com/spool/django/commit/0bf95772e76d207f08aeba7e15420152f1f748c4
I’m getting the sense that adding an asynchronous event loop around each test is a solution and I think that’s what’s beeing added in python 3.8:
https://docs.python.org/3.8/whatsnew/3.8.html#unittest
There’s also a pytest-asyncio
plugin worth checking out.
Thanks, hope that makes sense.