ipytest icon indicating copy to clipboard operation
ipytest copied to clipboard

Test collection fails in IntelliJ

Open edwinnazarian opened this issue 9 months ago • 3 comments

Here is my test condition that I set up in PyCharm (IntelliJ):

[Cell 1]

__file__ = "test_ipytest.ipynb"

import ipytest
ipytest.config(raise_on_error=True, coverage=False)

[Cell 2]

%%ipytest

import pytest

def test_simple():
    assert simple()

def simple():
    return True

I first run the Cell1. Cell 1's output is:

{'addopts': ('-q', '--color=yes'), 'clean': '[Tt]est*', 'coverage': False, 'defopts': 'auto', 'display_columns': 100, 'magics': True, 'raise_on_error': True, 'rewrite_asserts': True, 'run_in_thread': False}

Then I run Cell 2. Cell 2's output is:

no tests ran in 0.00s

Error Traceback (most recent call last) Cell In[54], line 1 ----> 1 get_ipython().run_cell_magic('ipytest', '', '\nimport pytest\n\ndef test_simple():\n assert simple()\n\ndef simple():\n return True\n\n')

File ~/IPytestProject/.venv/lib/python3.12/site-packages/IPython/core/interactiveshell.py:2542, in InteractiveShell.run_cell_magic(self, magic_name, line, cell) 2540 with self.builtin_trap: 2541 args = (magic_arg_s, cell) -> 2542 result = fn(*args, **kwargs) 2544 # The code below prevents the output from being displayed 2545 # when using magics with decorator @output_can_be_silenced 2546 # when the last Python token in the expression is a ';'. 2547 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~/IPytestProject/.venv/lib/python3.12/site-packages/ipytest/_impl.py:166, in ipytest_magic(line, cell, module) 158 raise RuntimeError( 159 "The ipytest magic cannot evaluate the cell. Most likely you " 160 "are running a modified ipython version. Consider using " 161 "ipytest.run and ipytest.clean directly.", 162 ) from e 164 raise e --> 166 run(*run_args, **run_kwargs)

File ~/IPytestProject/.venv/lib/python3.12/site-packages/ipytest/_impl.py:89, in run(module, plugins, run_in_thread, raise_on_error, addopts, defopts, display_columns, coverage, *args) 86 ipytest.exit_code = exit_code 88 if raise_on_error is True and exit_code != 0: ---> 89 raise Error(exit_code) 91 return exit_code

Error: ipytest failed with exit_code 5

Not sure why it thinks I might be running a modified ipython. Not sure why my test method is not executed.

edwinnazarian avatar Apr 16 '25 18:04 edwinnazarian

Hi @edwinnazarian,

as I don't use IntelliJ myself I can't reproduce the issue. Note, that the error you're getting is not that the Kernel is modified, but the error from pytest that errors can't be found. In this case pytest gives back the error code 5. The exception with the message is just part of stack trace, but actually raised.

chmp avatar Apr 26 '25 07:04 chmp

Hi @chmp

Thank you for your response.

The error 5 from pytest means that there were no tests found. I am not sure why it is not working in intelliJ but works perfectly in Databricks and Jupyter.

edwinnazarian avatar Apr 30 '25 00:04 edwinnazarian

@edwinnazarian I don't have a solution TBH. But probably good to keep this issue open for folks to be aware. Thanks a lot for checking, on your end what works and does not.

chmp avatar May 01 '25 08:05 chmp