main.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. prefix = '__tests__.'
  36. test_modules = [
  37. # fmt: off
  38. name[len(prefix) :]
  39. for name in sys.extra_modules
  40. if name.startswith(prefix) and not name.endswith('.conftest')
  41. # fmt: on
  42. ]
  43. doctest_packages = __res.find("PY_DOCTEST_PACKAGES") or ""
  44. if isinstance(doctest_packages, bytes):
  45. doctest_packages = doctest_packages.decode('utf-8')
  46. doctest_packages = doctest_packages.split()
  47. def is_doctest_module(name):
  48. for package in doctest_packages:
  49. if name == package or name.startswith(str(package) + "."):
  50. return True
  51. return False
  52. doctest_modules = [
  53. # fmt: off
  54. name
  55. for name in sys.extra_modules
  56. if is_doctest_module(name)
  57. # fmt: on
  58. ]
  59. def remove_user_site(paths):
  60. site_paths = ('site-packages', 'site-python')
  61. def is_site_path(path):
  62. for p in site_paths:
  63. if path.find(p) != -1:
  64. return True
  65. return False
  66. new_paths = list(paths)
  67. for p in paths:
  68. if is_site_path(p):
  69. new_paths.remove(p)
  70. return new_paths
  71. sys.path = remove_user_site(sys.path)
  72. rc = pytest.main(
  73. plugins=[
  74. collection.CollectionPlugin(test_modules, doctest_modules),
  75. ya,
  76. conftests,
  77. ]
  78. )
  79. if rc == 5:
  80. # don't care about EXIT_NOTESTSCOLLECTED
  81. rc = 0
  82. if rc == 1 and yatest_runner and not listing_mode and not os.environ.get(FORCE_EXIT_TESTSFAILED_ENV) == '1':
  83. # XXX it's place for future improvements
  84. # Test wrapper should terminate with 0 exit code if there are common test failures
  85. # and report it with trace-file machinery.
  86. # However, there are several case when we don't want to suppress exit_code:
  87. # - listing machinery doesn't use trace-file currently and rely on stdout and exit_code
  88. # - RestartTestException and InfrastructureException required non-zero exit_code to be processes correctly
  89. rc = 0
  90. if profile:
  91. profile.disable()
  92. ps = pstats.Stats(profile, stream=sys.stderr).sort_stats('cumulative')
  93. ps.print_stats()
  94. if '--output-dir' in sys.argv:
  95. output_dir = sys.argv[sys.argv.index('--output-dir') + 1]
  96. prof_filename = os.path.join(output_dir, 'pytest.profile')
  97. ps.dump_stats(prof_filename)
  98. try:
  99. import gprof2dot
  100. except ImportError as e:
  101. sys.stderr.write("Failed to generate call graph: {}\n".format(e))
  102. gprof2dot = None
  103. if gprof2dot:
  104. import shlex
  105. dot_filename = os.path.join(output_dir, 'pytest.profile.dot')
  106. args = [
  107. prof_filename,
  108. '--format=pstats',
  109. '--output={}'.format(dot_filename),
  110. ]
  111. if 'PYTEST_GPROF2DOT_ARGS' in os.environ:
  112. x = os.environ['PYTEST_GPROF2DOT_ARGS']
  113. args.extend(shlex.split(x))
  114. gprof2dot.main(argv=args)
  115. sys.exit(rc)
  116. if __name__ == '__main__':
  117. main()