Skip to content

Instantly share code, notes, and snippets.

@alrocar
Created February 13, 2023 21:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alrocar/36f6549339686bb739f24acf1e5f60ef to your computer and use it in GitHub Desktop.
Save alrocar/36f6549339686bb739f24acf1e5f60ef to your computer and use it in GitHub Desktop.
import os
import requests
def pytest_collection_modifyitems(session, config, items):
token = os.environ.get('TINYBIRD_TOKEN')
if not os.environ.get('GITLAB_CI'):
return items
response = requests.get(f'https://api.tinybird.co/v0/pipes/sort_tests_by_duration_dont_delete_used_in_ci__v2.json?token={token}')
if response.status_code != 200:
print("Error while sorting tests from tinybird")
return
modules_order = response.json()['data'][0]['items']
module_mapping = {item: item.cls.__name__ for item in items if item.cls}
modules = list(set(module_mapping.values()))
modules_oo = []
for module in modules_order:
if module in modules:
modules_oo.append(module)
sorted_items = items.copy()
# Iteratively move tests of each module to the end of the test queue
for module in modules_oo:
sorted_items = [it for it in sorted_items if module_mapping.get(it, None) != module] + [
it for it in sorted_items if module_mapping.get(it, None) == module
]
items[:] = sorted_items
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment