conftest.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # See https://hypothesis.readthedocs.io/en/latest/settings.html
  18. hypothesis.settings.register_profile(
  19. name="numpy-profile", deadline=None, print_blob=True,
  20. )
  21. # We try loading the profile defined by np.test(), which disables the
  22. # database and forces determinism, and fall back to the profile defined
  23. # above if we're running pytest directly. The odd dance is required
  24. # because np.test() executes this file *after* its own setup code.
  25. try:
  26. hypothesis.settings.load_profile("np.test() profile")
  27. except hypothesis.errors.InvalidArgument: # profile not found
  28. hypothesis.settings.load_profile("numpy-profile")
  29. def pytest_configure(config):
  30. config.addinivalue_line("markers",
  31. "valgrind_error: Tests that are known to error under valgrind.")
  32. config.addinivalue_line("markers",
  33. "leaks_references: Tests that are known to leak references.")
  34. config.addinivalue_line("markers",
  35. "slow: Tests that are very slow.")
  36. config.addinivalue_line("markers",
  37. "slow_pypy: Tests that are very slow on pypy.")
  38. def pytest_addoption(parser):
  39. parser.addoption("--available-memory", action="store", default=None,
  40. help=("Set amount of memory available for running the "
  41. "test suite. This can result to tests requiring "
  42. "especially large amounts of memory to be skipped. "
  43. "Equivalent to setting environment variable "
  44. "NPY_AVAILABLE_MEM. Default: determined"
  45. "automatically."))
  46. def pytest_sessionstart(session):
  47. available_mem = session.config.getoption('available_memory')
  48. if available_mem is not None:
  49. os.environ['NPY_AVAILABLE_MEM'] = available_mem
  50. #FIXME when yield tests are gone.
  51. @pytest.hookimpl()
  52. def pytest_itemcollected(item):
  53. """
  54. Check FPU precision mode was not changed during test collection.
  55. The clumsy way we do it here is mainly necessary because numpy
  56. still uses yield tests, which can execute code at test collection
  57. time.
  58. """
  59. global _old_fpu_mode
  60. mode = get_fpu_mode()
  61. if _old_fpu_mode is None:
  62. _old_fpu_mode = mode
  63. elif mode != _old_fpu_mode:
  64. _collect_results[item] = (_old_fpu_mode, mode)
  65. _old_fpu_mode = mode
  66. @pytest.fixture(scope="function", autouse=True)
  67. def check_fpu_mode(request):
  68. """
  69. Check FPU precision mode was not changed during the test.
  70. """
  71. old_mode = get_fpu_mode()
  72. yield
  73. new_mode = get_fpu_mode()
  74. if old_mode != new_mode:
  75. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  76. " during the test".format(old_mode, new_mode))
  77. collect_result = _collect_results.get(request.node)
  78. if collect_result is not None:
  79. old_mode, new_mode = collect_result
  80. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  81. " when collecting the test".format(old_mode,
  82. new_mode))
  83. @pytest.fixture(autouse=True)
  84. def add_np(doctest_namespace):
  85. doctest_namespace['np'] = numpy