Skip to content

Fix slow pytest 9.1.x - #430

Draft
tsbinns wants to merge 4 commits into
mne-tools:mainfrom
tsbinns:try_fix_test_time
Draft

tsbinns wants to merge 4 commits into
mne-tools:mainfrom
tsbinns:try_fix_test_time

Conversation

@tsbinns

@tsbinns tsbinns commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #429

Locally, test suite runs with pytest 9.0.3 and 9.1.1 take the same time to run (Ubuntu 24.04, Python 3.13).

However, with pytest 9.1.1, test_plot_connectivity_circle (the last test that gets run) has a plot pop-up which I have to close before the tests can finish, and which I didn't see for 9.0.3.

There is a fixture that is supposed to prevent this from happening, but maybe something in 9.1.1 is preventing the fixture from working properly.

This is the only visualisation test that actually gets run, as the others are skipped due to lack of imageio-ffmpeg

Have disabled the test in question to see if that's indeed the culprit.

@tsbinns

tsbinns commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

Yeah, tests run in normal time with that disabled, so tests aren't taking longer, it just gets caught on the final one.

@tsbinns

tsbinns commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

The logic in this conftest.py matches what is in MNE, but it didn't seem to be taking effect.

Explicitly adding autouse=True in the matplotlib config fixture prevented the figure popping up locally, but this should not be necessary because it gets added as a used fixture to the config (as is done for MNE).

However, it also resolved locally when I explicitly added mne_connectivity to the testpaths config option.

I'm not sure which change exactly in 9.1.0 caused the fixture to no longer take effect, but it runs now 🤷🏻

@tsbinns tsbinns changed the title Try figure out why pytest 9.1.x is slow Fix slow pytest 9.1.x Jun 21, 2026
@tsbinns
tsbinns enabled auto-merge (squash) June 21, 2026 15:37
@tsbinns

tsbinns commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

Removed the pytest specs to avoid 9.1. Most of the diff is the formatter changing pyproject.toml

@tsbinns
tsbinns disabled auto-merge June 21, 2026 15:45
@tsbinns

tsbinns commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

Huh, updating testpaths fixed it locally, but not here. Seeing if adding autouse to the fixture also only works locally.

@tsbinns

tsbinns commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

Setting the fixture to autouse fixes it on runners and locally. However, I would like to know why the config changes we make in conftest don't seem to take effect, so I'll wait before merging this.

@tsbinns
tsbinns marked this pull request as draft June 21, 2026 15:56
# License: Simplified BSD


import matplotlib.pyplot as plt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that doing this at the top sets the backend to non-Agg, which could be slower. I'd be surprised if it's 10x slower but 🤷

In MNE-Python we force set to Agg I think unless an env var is set to use Qt (which can be useful for debugging). I'd force-set here as well if that's not done already (haven't looked!)

Comment on lines 98 to 105
try:
want = os.environ["MNE_MPL_TESTING_BACKEND"]
except KeyError:
want = "agg" # don't pop up windows
with warnings.catch_warnings(record=True): # ignore warning
warnings.filterwarnings("ignore")
matplotlib.use(want, force=True)
import matplotlib.pyplot as plt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nevermind this is already force-setting to agg

@larsoner

Copy link
Copy Markdown
Member

Setting the fixture to autouse fixes it on runners and locally. However, I would like to know why the config changes we make in conftest don't seem to take effect, so I'll wait before merging this.

My shot in the dark is that in previous versions, having a session-scoped fixture might have caused it to be used even if it's not marked autouse. So arguably it's a bugfix that you now have to specify autouse=True even if it's not used in any tests

@larsoner

Copy link
Copy Markdown
Member

but this should not be necessary because it gets added as a used fixture to the config (as is done for MNE).

Okay I didn't read everything until just now... yeah this part seems weird. You could open a bug report about this change in behavior if you can isolate it properly.

Can you make the corresponding autouse change in MNE-Python as well? I don't see a problem with adding it both places. Then you could remove it from where it's told to be used in the config

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Figure out why Pytest 9.1.x is slow

2 participants