__init__.py 1.4 KB

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