conftest.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import pytest
  4. if sys.gettrace():
  5. @pytest.fixture(autouse=True)
  6. def restore_tracing():
  7. """Restore tracing function (when run with Coverage.py).
  8. https://bugs.python.org/issue37011
  9. """
  10. orig_trace = sys.gettrace()
  11. yield
  12. if sys.gettrace() != orig_trace:
  13. sys.settrace(orig_trace)
  14. @pytest.hookimpl(hookwrapper=True, tryfirst=True)
  15. def pytest_collection_modifyitems(config, items):
  16. """Prefer faster tests.
  17. Use a hookwrapper to do this in the beginning, so e.g. --ff still works
  18. correctly.
  19. """
  20. fast_items = []
  21. slow_items = []
  22. slowest_items = []
  23. neutral_items = []
  24. spawn_names = {"spawn_pytest", "spawn"}
  25. for item in items:
  26. try:
  27. fixtures = item.fixturenames
  28. except AttributeError:
  29. # doctest at least
  30. # (https://github.com/pytest-dev/pytest/issues/5070)
  31. neutral_items.append(item)
  32. else:
  33. if "testdir" in fixtures:
  34. if spawn_names.intersection(item.function.__code__.co_names):
  35. item.add_marker(pytest.mark.uses_pexpect)
  36. slowest_items.append(item)
  37. else:
  38. slow_items.append(item)
  39. item.add_marker(pytest.mark.slow)
  40. else:
  41. marker = item.get_closest_marker("slow")
  42. if marker:
  43. slowest_items.append(item)
  44. else:
  45. fast_items.append(item)
  46. items[:] = fast_items + neutral_items + slow_items + slowest_items
  47. yield