testing.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Distributed under the (new) BSD License. See LICENSE.txt for more info.
  3. """Functionality used for testing. This code itself is not covered in tests."""
  4. import os
  5. import sys
  6. import pytest
  7. # Get root dir
  8. THIS_DIR = os.path.abspath(os.path.dirname(__file__))
  9. ROOT_DIR = THIS_DIR
  10. for i in range(9):
  11. ROOT_DIR = os.path.dirname(ROOT_DIR)
  12. if os.path.isfile(os.path.join(ROOT_DIR, ".gitignore")):
  13. break
  14. # Functions to use from invoke tasks
  15. def test_unit(cov_report="term"):
  16. """Run all unit tests. Returns exit code."""
  17. orig_dir = os.getcwd()
  18. os.chdir(ROOT_DIR)
  19. try:
  20. _clear_imageio()
  21. _enable_faulthandler()
  22. return pytest.main(
  23. [
  24. "-v",
  25. "--cov",
  26. "imageio",
  27. "--cov-config",
  28. ".coveragerc",
  29. "--cov-report",
  30. cov_report,
  31. "tests",
  32. ]
  33. )
  34. finally:
  35. os.chdir(orig_dir)
  36. import imageio
  37. print("Tests were performed on", str(imageio))
  38. # Requirements
  39. def _enable_faulthandler():
  40. """Enable faulthandler (if we can), so that we get tracebacks
  41. on segfaults.
  42. """
  43. try:
  44. import faulthandler
  45. faulthandler.enable()
  46. print("Faulthandler enabled")
  47. except Exception:
  48. print("Could not enable faulthandler")
  49. def _clear_imageio():
  50. # Remove ourselves from sys.modules to force an import
  51. for key in list(sys.modules.keys()):
  52. if key.startswith("imageio"):
  53. del sys.modules[key]