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.

conftest.py 1.6KB

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