conftest.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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=hypothesis.HealthCheck.all(),
  28. )
  29. # Note that the default profile is chosen based on the presence
  30. # of pytest.ini, but can be overriden 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. def pytest_configure(config):
  37. config.addinivalue_line("markers",
  38. "valgrind_error: Tests that are known to error under valgrind.")
  39. config.addinivalue_line("markers",
  40. "leaks_references: Tests that are known to leak references.")
  41. config.addinivalue_line("markers",
  42. "slow: Tests that are very slow.")
  43. config.addinivalue_line("markers",
  44. "slow_pypy: Tests that are very slow on pypy.")
  45. def pytest_addoption(parser):
  46. parser.addoption("--available-memory", action="store", default=None,
  47. help=("Set amount of memory available for running the "
  48. "test suite. This can result to tests requiring "
  49. "especially large amounts of memory to be skipped. "
  50. "Equivalent to setting environment variable "
  51. "NPY_AVAILABLE_MEM. Default: determined"
  52. "automatically."))
  53. def pytest_sessionstart(session):
  54. available_mem = session.config.getoption('available_memory')
  55. if available_mem is not None:
  56. os.environ['NPY_AVAILABLE_MEM'] = available_mem
  57. #FIXME when yield tests are gone.
  58. @pytest.hookimpl()
  59. def pytest_itemcollected(item):
  60. """
  61. Check FPU precision mode was not changed during test collection.
  62. The clumsy way we do it here is mainly necessary because numpy
  63. still uses yield tests, which can execute code at test collection
  64. time.
  65. """
  66. global _old_fpu_mode
  67. mode = get_fpu_mode()
  68. if _old_fpu_mode is None:
  69. _old_fpu_mode = mode
  70. elif mode != _old_fpu_mode:
  71. _collect_results[item] = (_old_fpu_mode, mode)
  72. _old_fpu_mode = mode
  73. @pytest.fixture(scope="function", autouse=True)
  74. def check_fpu_mode(request):
  75. """
  76. Check FPU precision mode was not changed during the test.
  77. """
  78. old_mode = get_fpu_mode()
  79. yield
  80. new_mode = get_fpu_mode()
  81. if old_mode != new_mode:
  82. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  83. " during the test".format(old_mode, new_mode))
  84. collect_result = _collect_results.get(request.node)
  85. if collect_result is not None:
  86. old_mode, new_mode = collect_result
  87. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  88. " when collecting the test".format(old_mode,
  89. new_mode))
  90. @pytest.fixture(autouse=True)
  91. def add_np(doctest_namespace):
  92. doctest_namespace['np'] = numpy
  93. @pytest.fixture(autouse=True)
  94. def env_setup(monkeypatch):
  95. monkeypatch.setenv('PYTHONHASHSEED', '0')