conftest.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import sys
  2. sys._running_pytest = True # type: ignore
  3. from sympy.external.importtools import version_tuple
  4. import pytest
  5. from sympy.core.cache import clear_cache, USE_CACHE
  6. from sympy.external.gmpy import GROUND_TYPES
  7. from sympy.utilities.misc import ARCH
  8. import re
  9. try:
  10. import hypothesis
  11. hypothesis.settings.register_profile("sympy_hypothesis_profile", deadline=None)
  12. hypothesis.settings.load_profile("sympy_hypothesis_profile")
  13. except ImportError:
  14. raise ImportError(
  15. "hypothesis is a required dependency to run the SymPy test suite. "
  16. "Install it with 'pip install hypothesis' or 'conda install -c conda-forge hypothesis'"
  17. )
  18. sp = re.compile(r"([0-9]+)/([1-9][0-9]*)")
  19. def process_split(config, items):
  20. split = config.getoption("--split")
  21. if not split:
  22. return
  23. m = sp.match(split)
  24. if not m:
  25. raise ValueError(
  26. "split must be a string of the form a/b " "where a and b are ints."
  27. )
  28. i, t = map(int, m.groups())
  29. start, end = (i - 1) * len(items) // t, i * len(items) // t
  30. if i < t:
  31. # remove elements from end of list first
  32. del items[end:]
  33. del items[:start]
  34. def pytest_report_header(config):
  35. s = "architecture: %s\n" % ARCH
  36. s += "cache: %s\n" % USE_CACHE
  37. version = ""
  38. if GROUND_TYPES == "gmpy":
  39. import gmpy2
  40. version = gmpy2.version()
  41. elif GROUND_TYPES == "flint":
  42. try:
  43. from flint import __version__
  44. except ImportError:
  45. version = "unknown"
  46. else:
  47. version = f'(python-flint=={__version__})'
  48. s += "ground types: %s %s\n" % (GROUND_TYPES, version)
  49. return s
  50. def pytest_terminal_summary(terminalreporter):
  51. if terminalreporter.stats.get("error", None) or terminalreporter.stats.get(
  52. "failed", None
  53. ):
  54. terminalreporter.write_sep(" ", "DO *NOT* COMMIT!", red=True, bold=True)
  55. def pytest_addoption(parser):
  56. parser.addoption("--split", action="store", default="", help="split tests")
  57. def pytest_collection_modifyitems(config, items):
  58. """pytest hook."""
  59. # handle splits
  60. process_split(config, items)
  61. @pytest.fixture(autouse=True, scope="module")
  62. def file_clear_cache():
  63. clear_cache()
  64. @pytest.fixture(autouse=True, scope="module")
  65. def check_disabled(request):
  66. if getattr(request.module, "disabled", False):
  67. pytest.skip("test requirements not met.")
  68. elif getattr(request.module, "ipython", False):
  69. # need to check version and options for ipython tests
  70. if (
  71. version_tuple(pytest.__version__) < version_tuple("2.6.3")
  72. and pytest.config.getvalue("-s") != "no"
  73. ):
  74. pytest.skip("run py.test with -s or upgrade to newer version.")