|
2 | 2 | import os
|
3 | 3 | import socket
|
4 | 4 | import warnings
|
| 5 | +from collections import OrderedDict |
5 | 6 | from threading import Thread
|
6 | 7 | from contextlib import contextmanager
|
7 | 8 | from http.server import BaseHTTPRequestHandler, HTTPServer
|
@@ -645,3 +646,41 @@ def __eq__(self, other):
|
645 | 646 |
|
646 | 647 | def __ne__(self, other):
|
647 | 648 | return not self.__eq__(other)
|
| 649 | + |
| 650 | + |
| 651 | +def pytest_collection_modifyitems(config, items): |
| 652 | + """ |
| 653 | + Rearrange test items so that forked tests come before normal tests within their respective modules. |
| 654 | + Swap the last forked test with the last normal test if necessary. |
| 655 | +
|
| 656 | + Workaround to unpin pytest. See: |
| 657 | + https://github.com/pytest-dev/pytest/issues/9621, |
| 658 | + https://github.com/pytest-dev/pytest-forked/issues/67, and specifically: |
| 659 | + https://github.com/pytest-dev/pytest-forked/issues/67#issuecomment-1964718720 |
| 660 | + """ |
| 661 | + module_states = OrderedDict() |
| 662 | + |
| 663 | + for idx in range(len(items)): |
| 664 | + item = items[idx] |
| 665 | + current_module = item.module.__name__ |
| 666 | + |
| 667 | + if current_module not in module_states: |
| 668 | + module_states[current_module] = {"forked": [], "normal": []} |
| 669 | + |
| 670 | + if "forked" in item.keywords: |
| 671 | + module_states[current_module]["forked"].append(idx) |
| 672 | + else: |
| 673 | + module_states[current_module]["normal"].append(idx) |
| 674 | + |
| 675 | + # Swap the last forked test with the last normal test if necessary |
| 676 | + for states in module_states.values(): |
| 677 | + if states["forked"] and states["normal"]: |
| 678 | + last_forked_idx = states["forked"][-1] |
| 679 | + last_normal_idx = states["normal"][-1] |
| 680 | + |
| 681 | + if last_forked_idx > last_normal_idx: |
| 682 | + # Swap the items |
| 683 | + items[last_forked_idx], items[last_normal_idx] = ( |
| 684 | + items[last_normal_idx], |
| 685 | + items[last_forked_idx], |
| 686 | + ) |
0 commit comments