conftest.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """
  2. Pytest configuration and fixtures for the Numpy test suite.
  3. """
  4. import os
  5. import tempfile
  6. import hypothesis
  7. import pytest
  8. import numpy
  9. from numpy.core._multiarray_tests import get_fpu_mode
  10. _old_fpu_mode = None
  11. _collect_results = {}
  12. # Use a known and persistent tmpdir for hypothesis' caches, which
  13. # can be automatically cleared by the OS or user.
  14. hypothesis.configuration.set_hypothesis_home_dir(
  15. os.path.join(tempfile.gettempdir(), ".hypothesis")
  16. )
  17. # We register two custom profiles for Numpy - for details see
  18. # https://hypothesis.readthedocs.io/en/latest/settings.html
  19. # The first is designed for our own CI runs; the latter also
  20. # forces determinism and is designed for use via np.test()
  21. hypothesis.settings.register_profile(
  22. name="numpy-profile", deadline=None, print_blob=True,
  23. )
  24. hypothesis.settings.register_profile(
  25. name="np.test() profile",
  26. deadline=None, print_blob=True, database=None, derandomize=True,
  27. suppress_health_check=list(hypothesis.HealthCheck),
  28. )
  29. # Note that the default profile is chosen based on the presence
  30. # of pytest.ini, but can be overridden by passing the
  31. # --hypothesis-profile=NAME argument to pytest.
  32. _pytest_ini = os.path.join(os.path.dirname(__file__), "..", "pytest.ini")
  33. hypothesis.settings.load_profile(
  34. "numpy-profile" if os.path.isfile(_pytest_ini) else "np.test() profile"
  35. )
  36. # The experimentalAPI is used in _umath_tests
  37. os.environ["NUMPY_EXPERIMENTAL_DTYPE_API"] = "1"
  38. def pytest_configure(config):
  39. config.addinivalue_line("markers",
  40. "valgrind_error: Tests that are known to error under valgrind.")
  41. config.addinivalue_line("markers",
  42. "leaks_references: Tests that are known to leak references.")
  43. config.addinivalue_line("markers",
  44. "slow: Tests that are very slow.")
  45. config.addinivalue_line("markers",
  46. "slow_pypy: Tests that are very slow on pypy.")
  47. def pytest_addoption(parser):
  48. parser.addoption("--available-memory", action="store", default=None,
  49. help=("Set amount of memory available for running the "
  50. "test suite. This can result to tests requiring "
  51. "especially large amounts of memory to be skipped. "
  52. "Equivalent to setting environment variable "
  53. "NPY_AVAILABLE_MEM. Default: determined"
  54. "automatically."))
  55. def pytest_sessionstart(session):
  56. available_mem = session.config.getoption('available_memory')
  57. if available_mem is not None:
  58. os.environ['NPY_AVAILABLE_MEM'] = available_mem
  59. #FIXME when yield tests are gone.
  60. @pytest.hookimpl()
  61. def pytest_itemcollected(item):
  62. """
  63. Check FPU precision mode was not changed during test collection.
  64. The clumsy way we do it here is mainly necessary because numpy
  65. still uses yield tests, which can execute code at test collection
  66. time.
  67. """
  68. global _old_fpu_mode
  69. mode = get_fpu_mode()
  70. if _old_fpu_mode is None:
  71. _old_fpu_mode = mode
  72. elif mode != _old_fpu_mode:
  73. _collect_results[item] = (_old_fpu_mode, mode)
  74. _old_fpu_mode = mode
  75. @pytest.fixture(scope="function", autouse=True)
  76. def check_fpu_mode(request):
  77. """
  78. Check FPU precision mode was not changed during the test.
  79. """
  80. old_mode = get_fpu_mode()
  81. yield
  82. new_mode = get_fpu_mode()
  83. if old_mode != new_mode:
  84. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  85. " during the test".format(old_mode, new_mode))
  86. collect_result = _collect_results.get(request.node)
  87. if collect_result is not None:
  88. old_mode, new_mode = collect_result
  89. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  90. " when collecting the test".format(old_mode,
  91. new_mode))
  92. @pytest.fixture(autouse=True)
  93. def add_np(doctest_namespace):
  94. doctest_namespace['np'] = numpy
  95. @pytest.fixture(autouse=True)
  96. def env_setup(monkeypatch):
  97. monkeypatch.setenv('PYTHONHASHSEED', '0')
  98. @pytest.fixture(params=[True, False])
  99. def weak_promotion(request):
  100. """
  101. Fixture to ensure "legacy" promotion state or change it to use the new
  102. weak promotion (plus warning). `old_promotion` should be used as a
  103. parameter in the function.
  104. """
  105. state = numpy._get_promotion_state()
  106. if request.param:
  107. numpy._set_promotion_state("weak_and_warn")
  108. else:
  109. numpy._set_promotion_state("legacy")
  110. yield request.param
  111. numpy._set_promotion_state(state)