pytest icon indicating copy to clipboard operation
pytest copied to clipboard

add_marker with usefixtures no longer works with pytest 8.0.0

Open rhshadrach opened this issue 1 year ago • 7 comments

I believe this may be related to https://github.com/pytest-dev/pytest/issues/3664.

In pandas, we dynamically add a marker for our doctests so that we don't need import pandas as pd and import numpy as np in every docstring.

https://github.com/pandas-dev/pandas/blob/9008ee5810c09bc907b5fdc36fc3c1dff4a50c55/pandas/conftest.py#L191-L198

The add_doctest_imports fixture is here.

https://github.com/pandas-dev/pandas/blob/9008ee5810c09bc907b5fdc36fc3c1dff4a50c55/pandas/conftest.py#L253-L259

With pytest 8.0.0, add_marker no longer has any effect. Is the intentional, and if so, is there an alternative?

rhshadrach avatar Jan 28 '24 19:01 rhshadrach

Very similar to #11759. Would the alternative described there of using an autouse fixture instead of the hook work for pandas?

bluetech avatar Jan 28 '24 21:01 bluetech

Thanks! There is a comment in the pandas code that we don't use autouse because of perf. I'll see if I can track down the history there and see if I can reproduce the perf issue.

rhshadrach avatar Jan 28 '24 21:01 rhshadrach

This was originally changed in pandas in https://github.com/pandas-dev/pandas/pull/45667. It tried undoing that patch locally, seeing negligible differences in runtimes for the test suite. Will see what it looks like on the pandas CI in https://github.com/pandas-dev/pandas/pull/57122.

rhshadrach avatar Jan 29 '24 03:01 rhshadrach

It would be nice if an alternative to an autouse fixture could be used in the future as non-doctests don't need this fixture at all (which is probably +98% of tests in pandas).

The prior motivation for doing this dynamically was me being curious why in the durations output the last-ran unit test TEARDOWN could be one of the longest durations, and removing an autouse=True, doctest_namespace fixture was the culprit.

mroeschke avatar Jan 29 '24 20:01 mroeschke

@rhshadrach

Sorry to ping here, but I'm trying to understand how your autouse fix in pandas works here. It seems as though no fixtures are called when running $ pytest --doctest-modules src/my_lib. Is there something else going on?

Zeitsperre avatar Jul 25 '24 19:07 Zeitsperre

@Zeitsperre

Sorry to ping here...

Pings are welcome!

It seems as though no fixtures are called when running $ pytest --doctest-modules src/my_lib. Is there something else going on?

pandas CI invokes doctests via: python -c "import pandas as pd; pd.test(run_doctests=True)". There may be other ways to invoke them from the command line. However, when running that line, the fixture here below is used.

https://github.com/pandas-dev/pandas/blob/aa4dc71f54764252ff795cc42b1c465e20a204c0/pandas/conftest.py#L245

rhshadrach avatar Jul 29 '24 20:07 rhshadrach

@rhshadrach That fixed my issues, thanks!

Still hoping for a better workaround for the autouse=True issue.

Cheers!

Zeitsperre avatar Aug 05 '24 15:08 Zeitsperre