Skip to content

make embed kernel and kernelapp tests independent of test order - #1608

Open
daphnezlin wants to merge 2 commits into
ipython:mainfrom
daphnezlin:fix-test-order-dependence
Open

daphnezlin wants to merge 2 commits into
ipython:mainfrom
daphnezlin:fix-test-order-dependence

Conversation

@daphnezlin

@daphnezlin daphnezlin commented Nov 29, 2026 •

Copy link
Copy Markdown

A few tests depend on what earlier tests left behind, so they pass in the default order but fail
when the order changes (pytest-randomly, pytest-xdist, or running a single file).

  1. test_embed_kernel_func and test_start_app need a current asyncio event loop and don't create
    one. Run on their own on Python 3.12, they fail with "DeprecationWarning: There is no current
    event loop" (an error with this repo's filterwarnings). They only pass when an earlier test
    happened to leave a loop set.

  2. When test_embed_kernel_func fails, it never clears the IPKernelApp and ZMQInteractiveShell
    singletons, so later tests that create an app or an in-process shell fail with
    MultipleInstanceError. That's what happened in Six tests failing with ipykernel 6.29.5 and Python 3.13 on Debian #1302: a Python 3.13 failure in
    test_embed_kernel_func also failed test_install_kernelspec and test_zmq_interactive_shell.
    start testing on 3.13 #1277 fixed that trigger, but the leftover singletons are still there.

  3. test_install_kernelspec leaves InstallIPythonKernelSpecApp as a singleton, which breaks
    test_embed_kernel_func if it runs later.

To reproduce on main:

pytest tests/test_kernelapp.py::test_start_app
pytest tests/test_embed_kernel.py::test_embed_kernel_func tests/inprocess/test_kernel.py::test_do_execute
pytest tests/test_kernelspec.py::test_install_kernelspec tests/test_embed_kernel.py::test_embed_kernel_func

All three fail on main and pass with this change.

Changes (tests only):

  • a new_event_loop fixture in conftest.py that gives a sync test its own event loop and closes it
    afterwards, used by test_embed_kernel_func and test_start_app
  • test_embed_kernel_func joins its stop thread and clears IPKernelApp and ZMQInteractiveShell
    in a finally block
  • test_install_kernelspec clears InstallIPythonKernelSpecApp afterwards

Related: #1583 fixes the missing event loop in IPKernelApp.start() for #1582. That covers
test_embed_kernel_func's event loop problem, but not test_start_app (it fails while creating the
kernel, before start()) or the singleton leaks. Happy to drop the fixture from
test_embed_kernel_func once #1583 is in.

Also fixes test_sys_path_profile_dir, which used the default IPython profile and failed when it ran before any test that creates it.

@daphnezlin

Copy link
Copy Markdown
Author

I added a second commit for another test with the same kind of problem: test_sys_path_profile_dir used the default IPython profile, which only exists if an earlier test already started IPython. When it runs first it fails with OSError: Couldn't find profile 'default'. It now uses a temporary profile dir instead. To reproduce on main:

IPYTHONDIR=$(mktemp -d) pytest tests/test_kernel.py::test_sys_path_profile_dir

To check the whole pr, I ran the full suite 60 times in shuffled order (pytest-randomly, with some runs CPU limited) on main at 7940a09 and on this branch:

main (7940a09) this PR
test_embed_kernel_func failures 29/60 0/60
other tests failing after it 16 0
tests that failed at least once 21 4

The 4 left on this branch were test_sys_path_profile_dir (1/60, fixed by the new commit) and 3 tests that failed once or twice in 130+ runs with no pattern, which look unrelated.

One more thing the thread.join() fixes: when embed_kernel failed right away, the trigger_stop thread outlived the test and raised about a second later, during whichever test was running then. pytest reported that as PytestUnhandledThreadExceptionWarning, and since warnings are errors in this repo, a random later test failed. I saw it hit test_comm_in_manager and test_ipython_start_kernel_no_userns. Neither failed on this branch.

A minimal order that fails on main:

pytest -p no:cacheprovider --randomly-seed=943254755 --randomly-dont-reorganize tests/test_embed_kernel.py::test_embed_kernel_func tests/inprocess/test_kernel.py::test_do_execute

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant