main.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import os
  2. import sys
  3. import time
  4. import __res
  5. FORCE_EXIT_TESTSFAILED_ENV = 'FORCE_EXIT_TESTSFAILED'
  6. def main():
  7. import library.python.pytest.context as context
  8. context.Ctx["YA_PYTEST_START_TIMESTAMP"] = time.time()
  9. profile = None
  10. if '--profile-pytest' in sys.argv:
  11. sys.argv.remove('--profile-pytest')
  12. import pstats
  13. import cProfile
  14. profile = cProfile.Profile()
  15. profile.enable()
  16. # Reset influencing env. vars
  17. # For more info see library/python/testing/yatest_common/yatest/common/errors.py
  18. if FORCE_EXIT_TESTSFAILED_ENV in os.environ:
  19. del os.environ[FORCE_EXIT_TESTSFAILED_ENV]
  20. if "Y_PYTHON_CLEAR_ENTRY_POINT" in os.environ:
  21. if "Y_PYTHON_ENTRY_POINT" in os.environ:
  22. del os.environ["Y_PYTHON_ENTRY_POINT"]
  23. del os.environ["Y_PYTHON_CLEAR_ENTRY_POINT"]
  24. listing_mode = '--collect-only' in sys.argv
  25. yatest_runner = os.environ.get('YA_TEST_RUNNER') == '1'
  26. import pytest
  27. import library.python.pytest.plugins.collection as collection
  28. import library.python.pytest.plugins.ya as ya
  29. import library.python.pytest.plugins.conftests as conftests
  30. import _pytest.assertion
  31. from _pytest.monkeypatch import MonkeyPatch
  32. from . import rewrite
  33. m = MonkeyPatch()
  34. m.setattr(_pytest.assertion.rewrite, "AssertionRewritingHook", rewrite.AssertionRewritingHook)
  35. # see https://st.yandex-team.ru/DEVTOOLSSUPPORT-50337
  36. m.setattr(_pytest.assertion.truncate, "DEFAULT_MAX_LINES", 16)
  37. m.setattr(_pytest.assertion.truncate, "DEFAULT_MAX_CHARS", 32 * 80)
  38. prefix = '__tests__.'
  39. test_modules = [
  40. # fmt: off
  41. name[len(prefix) :]
  42. for name in sys.extra_modules
  43. if name.startswith(prefix) and not name.endswith('.conftest')
  44. # fmt: on
  45. ]
  46. doctest_packages = __res.find("PY_DOCTEST_PACKAGES") or ""
  47. if isinstance(doctest_packages, bytes):
  48. doctest_packages = doctest_packages.decode('utf-8')
  49. doctest_packages = doctest_packages.split()
  50. def is_doctest_module(name):
  51. for package in doctest_packages:
  52. if name == package or name.startswith(str(package) + "."):
  53. return True
  54. return False
  55. doctest_modules = [
  56. # fmt: off
  57. name
  58. for name in sys.extra_modules
  59. if is_doctest_module(name)
  60. # fmt: on
  61. ]
  62. def remove_user_site(paths):
  63. site_paths = ('site-packages', 'site-python')
  64. def is_site_path(path):
  65. for p in site_paths:
  66. if path.find(p) != -1:
  67. return True
  68. return False
  69. new_paths = list(paths)
  70. for p in paths:
  71. if is_site_path(p):
  72. new_paths.remove(p)
  73. return new_paths
  74. sys.path = remove_user_site(sys.path)
  75. rc = pytest.main(
  76. plugins=[
  77. collection.CollectionPlugin(test_modules, doctest_modules),
  78. ya,
  79. conftests,
  80. ]
  81. )
  82. if rc == 5:
  83. # don't care about EXIT_NOTESTSCOLLECTED
  84. rc = 0
  85. if rc == 1 and yatest_runner and not listing_mode and not os.environ.get(FORCE_EXIT_TESTSFAILED_ENV) == '1':
  86. # XXX it's place for future improvements
  87. # Test wrapper should terminate with 0 exit code if there are common test failures
  88. # and report it with trace-file machinery.
  89. # However, there are several case when we don't want to suppress exit_code:
  90. # - listing machinery doesn't use trace-file currently and rely on stdout and exit_code
  91. # - RestartTestException and InfrastructureException required non-zero exit_code to be processes correctly
  92. rc = 0
  93. if profile:
  94. profile.disable()
  95. ps = pstats.Stats(profile, stream=sys.stderr).sort_stats('cumulative')
  96. ps.print_stats()
  97. if '--output-dir' in sys.argv:
  98. output_dir = sys.argv[sys.argv.index('--output-dir') + 1]
  99. prof_filename = os.path.join(output_dir, 'pytest.profile')
  100. ps.dump_stats(prof_filename)
  101. try:
  102. import gprof2dot
  103. except ImportError as e:
  104. sys.stderr.write("Failed to generate call graph: {}\n".format(e))
  105. gprof2dot = None
  106. if gprof2dot:
  107. import shlex
  108. dot_filename = os.path.join(output_dir, 'pytest.profile.dot')
  109. args = [
  110. prof_filename,
  111. '--format=pstats',
  112. '--output={}'.format(dot_filename),
  113. ]
  114. if 'PYTEST_GPROF2DOT_ARGS' in os.environ:
  115. x = os.environ['PYTEST_GPROF2DOT_ARGS']
  116. args.extend(shlex.split(x))
  117. gprof2dot.main(argv=args)
  118. sys.exit(rc)
  119. if __name__ == '__main__':
  120. main()