Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Incremental tests by selectively disabling shuffling #319

Open
Querela opened this issue Jan 16, 2021 · 2 comments
Open

Support for Incremental tests by selectively disabling shuffling #319

Querela opened this issue Jan 16, 2021 · 2 comments

Comments

@Querela
Copy link

Querela commented Jan 16, 2021

Is it possible to use pytest-randomly but selectively disable shuffling of functions in classes?

Similar to https://github.com/jbasko/pytest-random-order#disable-shuffling-in-module-or-class
This could allow incremental tests, as described in https://docs.pytest.org/en/stable/example/simple.html#incremental-testing-test-steps but still allow some randomness.
In incremental tests where previous functions may depend on prior steps (organized in functions in a class) random order would expectedly fail.

@adamchainz
Copy link
Member

I'd be open to adding markers like pytest-random-order. It would also help with rollout of pytest-randomly on a large codebase, one can mark off the "unsafe to shuffle" modules/classes without needing to fix all their interdependencies immediately.

@Querela
Copy link
Author

Querela commented Jan 17, 2021

I saw that there is a CLI option to disable shuffling on a session scope when I looked at the code.
For module and class scope it might be possible to just check for a custom mark, e.g. 'no-shuffle-mark' in item.keywords, and disable the shuffling of test items before adding them to the collection. That should be 2-4 locations in the code (just before the random.shuffle call).
When annotating a module (pytestmark), the marker should be recorded in a class and function items if I remember correctly, so the checks can be the same.
The only issue might be marked functions (which makes no sense but could disable shuffling unintentionally for a larger scope depending on how checks are implemented).

Something along:

random.shuffle(current_items)
# -->
do_shuffle = not all(("no-shuffle-mark" in item.keywords) for item in current_items)
if do_shuffle:
    random.shuffle(current_items)

Maybe item.iter_markers(name="...") if "..." in item.keywords does not work?

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 a pull request may close this issue.

2 participants