tester.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. import sys
  3. def _show_skimage_info():
  4. import skimage
  5. print(f"skimage version {skimage.__version__}")
  6. class PytestTester:
  7. """
  8. Pytest test runner.
  9. This class is made available in ``skimage._shared.testing``, and a test
  10. function is typically added to a package's __init__.py like so::
  11. from skimage._shared.testing import PytestTester
  12. test = PytestTester(__name__)
  13. del PytestTester
  14. Calling this test function finds and runs all tests associated with the
  15. module and all its sub-modules.
  16. Attributes
  17. ----------
  18. module_name : str
  19. Full path to the package to test.
  20. Parameters
  21. ----------
  22. module_name : module name
  23. The name of the module to test.
  24. """
  25. def __init__(self, module_name):
  26. self.module_name = module_name
  27. def __call__(
  28. self,
  29. label='fast',
  30. verbose=1,
  31. extra_argv=None,
  32. doctests=False,
  33. coverage=False,
  34. durations=-1,
  35. tests=None,
  36. ):
  37. """
  38. Run tests for module using pytest.
  39. Parameters
  40. ----------
  41. label : {'fast', 'full'}, optional
  42. Identifies the tests to run. When set to 'fast', tests decorated
  43. with `pytest.mark.slow` are skipped, when 'full', the slow marker
  44. is ignored.
  45. verbose : int, optional
  46. Verbosity value for test outputs, in the range 1-3. Default is 1.
  47. extra_argv : list, optional
  48. List with any extra arguments to pass to pytests.
  49. doctests : bool, optional
  50. .. note:: Not supported
  51. coverage : bool, optional
  52. If True, report coverage of scikit-image code. Default is False.
  53. Requires installation of (pip) pytest-cov.
  54. durations : int, optional
  55. If < 0, do nothing, If 0, report time of all tests, if > 0,
  56. report the time of the slowest `timer` tests. Default is -1.
  57. tests : test or list of tests
  58. Tests to be executed with pytest '--pyargs'
  59. Returns
  60. -------
  61. result : bool
  62. Return True on success, false otherwise.
  63. """
  64. import pytest
  65. module = sys.modules[self.module_name]
  66. module_path = os.path.abspath(module.__path__[0])
  67. # setup the pytest arguments
  68. pytest_args = ["-l"]
  69. # offset verbosity. The "-q" cancels a "-v".
  70. pytest_args += ["-q"]
  71. # Filter out annoying import messages. Want these in both develop and
  72. # release mode.
  73. pytest_args += [
  74. "-W ignore:Not importing directory",
  75. "-W ignore:numpy.dtype size changed",
  76. "-W ignore:numpy.ufunc size changed",
  77. ]
  78. if doctests:
  79. raise ValueError("Doctests not supported")
  80. if extra_argv:
  81. pytest_args += list(extra_argv)
  82. if verbose > 1:
  83. pytest_args += ["-" + "v" * (verbose - 1)]
  84. if coverage:
  85. pytest_args += ["--cov=" + module_path]
  86. if label == "fast":
  87. pytest_args += ["-m", "not slow"]
  88. elif label != "full":
  89. pytest_args += ["-m", label]
  90. if durations >= 0:
  91. pytest_args += [f"--durations={durations}"]
  92. if tests is None:
  93. tests = [self.module_name]
  94. pytest_args += ["--pyargs"] + list(tests)
  95. # run tests.
  96. _show_skimage_info()
  97. try:
  98. code = pytest.main(pytest_args)
  99. except SystemExit as exc:
  100. code = exc.code
  101. return code == 0