Can you use self.assertRaises as an async context manager?
<p>I'd like to test that a python 3 coro fails with a particular exception, but this functionality doesn't seem to be implemented.</p>

<pre><code>async with self.assertRaises(TestExceptionType):
    await my_func()
</code></pre>

<p>as the unit test fails like this:</p>

<pre><code>...
File "/Users/...../tests.py", line 144, in go
    async with self.assertRaises(TestExceptionType):
AttributeError: __aexit__
</code></pre>

<p>So my question is: should this work? And if not, what's the best way to assert a failing async function?</p>

<p>Just use classic old good <code>with</code> around <code>await</code> call:</p>

<pre><code>with self.assertRaises(TestExceptionType):
    await my_func()
</code></pre>

<p>It works.</p>

python unit-testing python-3.x python-asyncio