__init__.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. from ._version import __version__
  3. import operator
  4. import pytest
  5. orders_map = {
  6. 'first': 0,
  7. 'second': 1,
  8. 'third': 2,
  9. 'fourth': 3,
  10. 'fifth': 4,
  11. 'sixth': 5,
  12. 'seventh': 6,
  13. 'eighth': 7,
  14. 'last': -1,
  15. 'second_to_last': -2,
  16. 'third_to_last': -3,
  17. 'fourth_to_last': -4,
  18. 'fifth_to_last': -5,
  19. 'sixth_to_last': -6,
  20. 'seventh_to_last': -7,
  21. 'eighth_to_last': -8,
  22. }
  23. def pytest_configure(config):
  24. """Register the "run" marker."""
  25. config_line = (
  26. 'run: specify ordering information for when tests should run '
  27. 'in relation to one another. Provided by pytest-ordering. '
  28. 'See also: http://pytest-ordering.readthedocs.org/'
  29. )
  30. config.addinivalue_line('markers', config_line)
  31. def pytest_collection_modifyitems(session, config, items):
  32. grouped_items = {}
  33. for item in items:
  34. for mark_name, order in orders_map.items():
  35. mark = item.get_closest_marker(mark_name)
  36. if mark:
  37. item.add_marker(pytest.mark.run(order=order))
  38. break
  39. mark = item.get_closest_marker('run')
  40. if mark:
  41. order = mark.kwargs.get('order')
  42. else:
  43. order = None
  44. grouped_items.setdefault(order, []).append(item)
  45. sorted_items = []
  46. unordered_items = [grouped_items.pop(None, [])]
  47. start_list = sorted((i for i in grouped_items.items() if i[0] >= 0),
  48. key=operator.itemgetter(0))
  49. end_list = sorted((i for i in grouped_items.items() if i[0] < 0),
  50. key=operator.itemgetter(0))
  51. sorted_items.extend([i[1] for i in start_list])
  52. sorted_items.extend(unordered_items)
  53. sorted_items.extend([i[1] for i in end_list])
  54. items[:] = [item for sublist in sorted_items for item in sublist]