__init__.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """
  2. Helper functions for testing.
  3. """
  4. import locale
  5. import logging
  6. import sys
  7. import warnings
  8. import matplotlib as mpl
  9. from matplotlib import cbook
  10. _log = logging.getLogger(__name__)
  11. @cbook.deprecated("3.2")
  12. def is_called_from_pytest():
  13. """Whether we are in a pytest run."""
  14. return getattr(mpl, '_called_from_pytest', False)
  15. def set_font_settings_for_testing():
  16. mpl.rcParams['font.family'] = 'DejaVu Sans'
  17. mpl.rcParams['text.hinting'] = 'none'
  18. mpl.rcParams['text.hinting_factor'] = 8
  19. def set_reproducibility_for_testing():
  20. mpl.rcParams['svg.hashsalt'] = 'matplotlib'
  21. def setup():
  22. # The baseline images are created in this locale, so we should use
  23. # it during all of the tests.
  24. try:
  25. locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
  26. except locale.Error:
  27. try:
  28. locale.setlocale(locale.LC_ALL, 'English_United States.1252')
  29. except locale.Error:
  30. _log.warning(
  31. "Could not set locale to English/United States. "
  32. "Some date-related tests may fail.")
  33. mpl.use('Agg')
  34. with cbook._suppress_matplotlib_deprecation_warning():
  35. mpl.rcdefaults() # Start with all defaults
  36. # These settings *must* be hardcoded for running the comparison tests and
  37. # are not necessarily the default values as specified in rcsetup.py.
  38. set_font_settings_for_testing()
  39. set_reproducibility_for_testing()