Test ipytest - notebook integration


In [1]:
import pytest
import ipytest

ipytest.config(rewrite_asserts=True, magics=True, addopts=['-qq'])

__file__ = 'TestIntegration.ipynb'

Successful tests


In [2]:
%%run_pytest[clean]

def test_foo():
    assert True is True


.                                                                                                                                                                         [100%]

In [3]:
assert ipytest.exit_code == 0

Failing tests


In [4]:
%%run_pytest[clean]

def test_foo():
    assert True is False


F                                                                                                                                                                         [100%]
=================================================================================== FAILURES ====================================================================================
___________________________________________________________________________________ test_foo ____________________________________________________________________________________

    def test_foo():
>       assert True is False
E       assert True is False

<ipython-input-4-84ce4706388e>:3: AssertionError
=============================================================================== warnings summary ================================================================================
/Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272
  /Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272: PytestWarning: Module already imported so cannot be rewritten: nbval
    self.config,

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

In [5]:
assert ipytest.exit_code == 1

No tests to collect


In [6]:
%%run_pytest[clean]

pass


=============================================================================== warnings summary ================================================================================
/Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272
  /Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272: PytestWarning: Module already imported so cannot be rewritten: nbval
    self.config,

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

In [7]:
assert ipytest.exit_code == 5

Example without assertion rewriting


In [8]:
ipytest.config.rewrite_asserts = False

In [9]:
ipytest.clean_tests()

def test_foo():
    assert True is False

assert ipytest.run(return_exit_code=True) == 1


F                                                                                                                                                                         [100%]
=================================================================================== FAILURES ====================================================================================
___________________________________________________________________________________ test_foo ____________________________________________________________________________________

    def test_foo():
>       assert True is False
E       AssertionError

<ipython-input-9-c16d3296c0f0>:4: AssertionError
=============================================================================== warnings summary ================================================================================
/Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272
  /Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272: PytestWarning: Module already imported so cannot be rewritten: nbval
    self.config,

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

Test that errors are propagated


In [10]:
ipytest.clean_tests()

def test_foo():
    assert True is False

with ipytest.config(raise_on_error=True):
    with pytest.raises(RuntimeError):
        ipytest.run()


F                                                                                                                                                                         [100%]
=================================================================================== FAILURES ====================================================================================
___________________________________________________________________________________ test_foo ____________________________________________________________________________________

    def test_foo():
>       assert True is False
E       AssertionError

<ipython-input-10-9f900e851c8e>:4: AssertionError
=============================================================================== warnings summary ================================================================================
/Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272
  /Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272: PytestWarning: Module already imported so cannot be rewritten: nbval
    self.config,

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

In [11]:
# check that the old value is restored after the context is left
assert ipytest.config.raise_on_error is False

In [12]:
ipytest.config.raise_on_error = True

In [13]:
%%run_pytest[clean] -qq

def test_foo():
    assert True is False


F                                                                                                                                                                         [100%]
=================================================================================== FAILURES ====================================================================================
___________________________________________________________________________________ test_foo ____________________________________________________________________________________

    def test_foo():
>       assert True is False
E       assert True is False

<ipython-input-13-84ce4706388e>:3: AssertionError
=============================================================================== warnings summary ================================================================================
/Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272
  /Volumes/Home/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:272: PytestWarning: Module already imported so cannot be rewritten: nbval
    self.config,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~/Code/SideProjects/ipytest/tests/TestIntegration.ipynb in <module>
----> 1 get_ipython().run_cell_magic('run_pytest[clean]', '-qq', '\ndef test_foo():\n    assert True is False\n')

~/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2321             magic_arg_s = self.var_expand(line, stack_depth)
   2322             with self.builtin_trap:
-> 2323                 result = fn(magic_arg_s, cell)
   2324             return result
   2325 

<decorator-gen-125> in run_pytest_clean(self, line, cell)

~/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/Code/SideProjects/ipytest/ipytest/_pytest_support.py in run_pytest_clean(self, line, cell)
    153 
    154         clean_tests(items=__main__.__dict__)
--> 155         return self.run_pytest(line, cell)
    156 
    157     @cell_magic

<decorator-gen-126> in run_pytest(self, line, cell)

~/.local/share/virtualenvs/ipytest-w1VRNA16/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/Code/SideProjects/ipytest/ipytest/_pytest_support.py in run_pytest(self, line, cell)
    167         import ipytest
    168 
--> 169         ipytest.exit_code = run(*shlex.split(line), return_exit_code=True)
    170 
    171     @cell_magic

~/Code/SideProjects/ipytest/ipytest/_pytest_support.py in run(module, filename, plugins, return_exit_code, *args)
     75     if config.raise_on_error and exit_code != 0:
     76         raise RuntimeError(
---> 77             "Error in pytest invocation. Exit code: {}".format(exit_code)
     78         )
     79 

RuntimeError: Error in pytest invocation. Exit code: 1

In [14]:
ipytest.config.raise_on_error = False

In [ ]: