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

feature request: order tests by slowest-first #89

Open
charles-cooper opened this issue Mar 17, 2024 · 3 comments
Open

feature request: order tests by slowest-first #89

charles-cooper opened this issue Mar 17, 2024 · 3 comments

Comments

@charles-cooper
Copy link

in long running test suites (especially with xdist), it's useful to run the slowest tests first so that you end up with better load. the following code seems to do so:

def pytest_collection_modifyitems(config, items):
    try:
        with open(".test_durations") as f:
            s = f.read()
            durations = json.loads(s)
    except FileNotFoundError:
        durations = {}

    timings = {}
    for item in items:
        path, _, reqname = item.location
        request_id = f"{path}::{reqname}"
        timing = durations.get(request_id, None)
        if timing is not None:
            timings[item] = timing

    meantime = sum(timings.values()) / len(timings)
    # sort by highest time first
    items.sort(key=lambda item: -timings.get(item, meantime))

but i was thinking it might be good for this to be available in the pytest-split plugin itself, so we don't need to add this same function across different projects.

alternatively, i was thinking publishing this as a plugin myself, if pytest-split does not want to add it.

@jerry-git
Copy link
Owner

Hey 👋 ! There's already support for different splitting algorithms, see

I believe what you are asking is actually quite close to "least duration" algo that already exists.

Happy to take a contribution which introduces "slowest first" splitting algorithm 🙂

@charles-cooper
Copy link
Author

yea these are interesting. the use case i am imagining is actually most useful with --splits 1 --group 1 because it reorders the items on the local worker. do you think "slowest first" makes sense with more splits?

@jerry-git
Copy link
Owner

If the main motivation is to optimise the usage with xdist, I guess there could be also use cases which prefer to have multiple test runner nodes (e.g. GHA workers). So, pytest-split for splitting the work to multiple nodes and then xdist for splitting the work inside each node.

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

No branches or pull requests

2 participants