In [1]:
import pytest

import ipytest

__file__ = 'TestAsync.ipynb'
ipytest.config(magics=True, rewrite_asserts=True, addopts=["-qq"], raise_on_error=True)


Out[1]:
<ipytest._config.ConfigContext at 0x10f027f28>

In [2]:
@pytest.mark.asyncio
async def test_foo():
    assert True

In [3]:
# NBVAL_RAISES_EXCEPTION
# NOTE: pytest + ipython clash, when running async tests in the main thread
ipytest.run()


F                                                                                                                                                                             [100%]
===================================================================================== FAILURES ======================================================================================
_____________________________________________________________________________________ test_foo ______________________________________________________________________________________

pyfuncitem = <Function test_foo>

    @pytest.mark.tryfirst
    def pytest_pyfunc_call(pyfuncitem):
        """
        Run asyncio marked test functions in an event loop instead of a normal
        function call.
        """
        for marker_name, fixture_name in _markers_2_fixtures.items():
            if marker_name in pyfuncitem.keywords \
                    and not getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
                event_loop = pyfuncitem.funcargs[fixture_name]
    
                funcargs = pyfuncitem.funcargs
                testargs = {arg: funcargs[arg]
                            for arg in pyfuncitem._fixtureinfo.argnames}
    
                event_loop.run_until_complete(
                    asyncio.ensure_future(
>                       pyfuncitem.obj(**testargs), loop=event_loop))

../../../../.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/pytest_asyncio/plugin.py:158: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py:571: in run_until_complete
    self.run_forever()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_UnixSelectorEventLoop running=False closed=False debug=False>

    def run_forever(self):
        """Run until stop() is called."""
        self._check_closed()
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
            raise RuntimeError(
>               'Cannot run the event loop while another loop is running')
E           RuntimeError: Cannot run the event loop while another loop is running

/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py:529: RuntimeError
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~/Code/SideProjects/ipytest/tests/TestAsync.ipynb in <module>
      1 # NBVAL_RAISES_EXCEPTION
      2 # NOTE: pytest + ipython clash, when running async tests in the main thread
----> 3 ipytest.run()

~/Code/SideProjects/ipytest/ipytest/_pytest_support.py in run(module, filename, plugins, return_exit_code, *args)
     59     if not config.run_in_thread:
     60         return _run_impl(
---> 61                 *args, module=module, filename=filename, plugins=plugins, return_exit_code=return_exit_code)
     62 
     63     exit_code = None

~/Code/SideProjects/ipytest/ipytest/_pytest_support.py in _run_impl(module, filename, plugins, return_exit_code, *args)
     95     if config.raise_on_error and exit_code != 0:
     96         raise RuntimeError(
---> 97             "Error in pytest invocation. Exit code: {}".format(exit_code)
     98         )
     99 

RuntimeError: Error in pytest invocation. Exit code: 1

In [4]:
# When running in a separate thread no error is raised
with ipytest.config(run_in_thread=True):
    ipytest.run()


.                                                                                                                                                                             [100%]
================================================================================= warnings summary ==================================================================================
/Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/config/__init__.py:784
  /Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/config/__init__.py:784: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: nbval
    self._mark_plugins_for_rewrite(hook)

/Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/config/__init__.py:784
  /Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/config/__init__.py:784: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_asyncio
    self._mark_plugins_for_rewrite(hook)

-- Docs: https://docs.pytest.org/en/latest/warnings.html

In [ ]: