You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.7 KiB

4 years ago
  1. """
  2. Pytest configuration and fixtures for the Numpy test suite.
  3. """
  4. from __future__ import division, absolute_import, print_function
  5. import warnings
  6. import pytest
  7. import numpy
  8. import importlib
  9. from numpy.core._multiarray_tests import get_fpu_mode
  10. _old_fpu_mode = None
  11. _collect_results = {}
  12. #FIXME when yield tests are gone.
  13. @pytest.hookimpl()
  14. def pytest_itemcollected(item):
  15. """
  16. Check FPU precision mode was not changed during test collection.
  17. The clumsy way we do it here is mainly necessary because numpy
  18. still uses yield tests, which can execute code at test collection
  19. time.
  20. """
  21. global _old_fpu_mode
  22. mode = get_fpu_mode()
  23. if _old_fpu_mode is None:
  24. _old_fpu_mode = mode
  25. elif mode != _old_fpu_mode:
  26. _collect_results[item] = (_old_fpu_mode, mode)
  27. _old_fpu_mode = mode
  28. @pytest.fixture(scope="function", autouse=True)
  29. def check_fpu_mode(request):
  30. """
  31. Check FPU precision mode was not changed during the test.
  32. """
  33. old_mode = get_fpu_mode()
  34. yield
  35. new_mode = get_fpu_mode()
  36. if old_mode != new_mode:
  37. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  38. " during the test".format(old_mode, new_mode))
  39. collect_result = _collect_results.get(request.node)
  40. if collect_result is not None:
  41. old_mode, new_mode = collect_result
  42. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  43. " when collecting the test".format(old_mode,
  44. new_mode))
  45. @pytest.fixture(autouse=True)
  46. def add_np(doctest_namespace):
  47. doctest_namespace['np'] = numpy