[WIP] Minimize calls to session, module, and class-scoped fixtures by changing test order #3551
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is very rough. All the debugging logging is still in, I haven't deleted the existing code I'm replacing, many tests are failing, and it's still buggy. I decided to post this early version before sinking any more time to see if my basic approach is acceptable.
I am trying to fix #3161. I tried to figure out how the current reordering code is supposed to work and failed. I couldn't even figure out what order it's supposed to produce. Instead, I want to define the right order as the one that minimizes the number of calls to session-scoped, then module-scoped, then class-scoped fixtures and otherwise keeps as much of the original order as possible. This minimization requirement induces a partial order on the tests that I need to extend to a total order with a topological sort. This is done in three steps in reorder_items(). First, I build some data structures using the already-existing code to get information about the fixtures. Second, I create a directed graph for the partial order induced by the fixtures. Third, I use the directed graph to topological-sort the tests with an iterative depth-first search.
This algorithm is not very efficient, I could make it more efficient at the cost of making the code more complicated. Alternately, the recursive implementation of topological sort is simpler and easier to understand, but will be slower because of the costliness of function calls in Python.
I suspect that some of the existing test failures are due to the topological sort not correctly preserving the initial order in some cases, that is bugs in this code. However, I suspect in some cases the order should change and the tests should be changed to match. Changing existing test orders is why I've targeted the features branch.