Skip to content

Instantly share code, notes, and snippets.

@magixx
Created May 25, 2016 20:58
Show Gist options
  • Save magixx/fd1e76d90a0e98186adfbf14dd6fbcaa to your computer and use it in GitHub Desktop.
Save magixx/fd1e76d90a0e98186adfbf14dd6fbcaa to your computer and use it in GitHub Desktop.
import pytest
from collections import OrderedDict
from pprint import pprint
@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(session, config, items):
"""
Create an ordered dict containing clustered tests based on classes
:param session:
:param config:
:param items:
:return:
"""
print 'Collecting items:'
session.grouped_items = OrderedDict()
session._first_reorder = True
# Sort the items by Class
for item in items:
class_location = ''.join(item.nodeid.rsplit(item.name, 1))
if class_location not in session.grouped_items:
session.grouped_items[class_location] = []
session.grouped_items[class_location].append(item)
# Need to rearrange the item.nextitem object???
pass
def pytest_runtestloop(session):
"""
Modified test loop to reorder item clusters as it is running
:param session:
:return:
"""
print 'TEST LOOP: %s' % session
if session.config.option.collectonly:
return True
def getnextitem(i):
# this is a function to avoid python2
# keeping sys.exc_info set when calling into a test
# python2 keeps sys.exc_info till the frame is left
try:
return session.items[i + 1]
except IndexError:
return None
for i, item in enumerate(session.items):
nextitem = getnextitem(i)
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
if session.shouldstop:
raise session.Interrupted(session.shouldstop)
return True
# def pytest_runtest_protocol(item, nextitem):
# print '\nRUNNING TEST PROTOCOL for item: %s' % item
# grouped_items = item.session.grouped_items
# items = item.session.items
#
# if item.session._first_reorder:
# # Swap an item position while about to run tests:
# print 'Before:'
# pprint(items)
# items.insert(len(items) - 2, items.pop())
# print 'After:'
# item.session._first_reorder = False
# pprint(items)
#
# # Need to figure out which items in session have run already and which are remain to be run
#
# # For each class chunk move it to the bottom or the top of items
# pass
@pytest.fixture(scope="function")
def fix_1(request):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment