conftests.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import os
  2. import importlib
  3. import sys
  4. import inspect
  5. import yatest.common as yc
  6. from pytest import hookimpl
  7. from yatest_lib.ya import Ya
  8. from library.python.pytest.plugins.fixtures import metrics, links # noqa
  9. orig_getfile = inspect.getfile
  10. def getfile(object):
  11. if inspect.ismodule(object) and getattr(object, '__orig_file__', None):
  12. res = object.__orig_file__
  13. else:
  14. res = orig_getfile(object)
  15. return res
  16. inspect.getfile = getfile
  17. conftest_modules = []
  18. @hookimpl(trylast=True)
  19. def pytest_load_initial_conftests(early_config, parser, args):
  20. yc.runtime._set_ya_config(ya=Ya())
  21. if hasattr(sys, 'extra_modules'):
  22. conftests = filter(lambda name: name.endswith(".conftest"), sys.extra_modules)
  23. else:
  24. conftests = []
  25. def conftest_key(name):
  26. if not name.startswith("__tests__."):
  27. # Make __tests__ come last
  28. return "_." + name
  29. return name
  30. for name in sorted(conftests, key=conftest_key):
  31. mod = importlib.import_module(name)
  32. if os.getenv("CONFTEST_LOAD_POLICY") != "LOCAL":
  33. mod.__orig_file__ = mod.__file__
  34. mod.__file__ = ""
  35. conftest_modules.append(mod)
  36. early_config.pluginmanager.consider_conftest(mod)
  37. def getconftestmodules(*args, **kwargs):
  38. return conftest_modules
  39. def pytest_sessionstart(session):
  40. # Override filesystem based relevant conftest discovery on the call path
  41. assert session.config.pluginmanager
  42. session.config.pluginmanager._getconftestmodules = getconftestmodules