conftest.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. def pytest_configure(config):
  11. config.addinivalue_line("markers",
  12. "valgrind_error: Tests that are known to error under valgrind.")
  13. config.addinivalue_line("markers",
  14. "slow: Tests that are very slow.")
  15. #FIXME when yield tests are gone.
  16. @pytest.hookimpl()
  17. def pytest_itemcollected(item):
  18. """
  19. Check FPU precision mode was not changed during test collection.
  20. The clumsy way we do it here is mainly necessary because numpy
  21. still uses yield tests, which can execute code at test collection
  22. time.
  23. """
  24. global _old_fpu_mode
  25. mode = get_fpu_mode()
  26. if _old_fpu_mode is None:
  27. _old_fpu_mode = mode
  28. elif mode != _old_fpu_mode:
  29. _collect_results[item] = (_old_fpu_mode, mode)
  30. _old_fpu_mode = mode
  31. @pytest.fixture(scope="function", autouse=True)
  32. def check_fpu_mode(request):
  33. """
  34. Check FPU precision mode was not changed during the test.
  35. """
  36. old_mode = get_fpu_mode()
  37. yield
  38. new_mode = get_fpu_mode()
  39. if old_mode != new_mode:
  40. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  41. " during the test".format(old_mode, new_mode))
  42. collect_result = _collect_results.get(request.node)
  43. if collect_result is not None:
  44. old_mode, new_mode = collect_result
  45. raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
  46. " when collecting the test".format(old_mode,
  47. new_mode))
  48. @pytest.fixture(autouse=True)
  49. def add_np(doctest_namespace):
  50. doctest_namespace['np'] = numpy