_pytesttester.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. """
  2. Pytest test running.
  3. This module implements the ``test()`` function for NumPy modules. The usual
  4. boiler plate for doing that is to put the following in the module
  5. ``__init__.py`` file::
  6. from numpy._pytesttester import PytestTester
  7. test = PytestTester(__name__)
  8. del PytestTester
  9. Warnings filtering and other runtime settings should be dealt with in the
  10. ``pytest.ini`` file in the numpy repo root. The behavior of the test depends on
  11. whether or not that file is found as follows:
  12. * ``pytest.ini`` is present (develop mode)
  13. All warnings except those explicitly filtered out are raised as error.
  14. * ``pytest.ini`` is absent (release mode)
  15. DeprecationWarnings and PendingDeprecationWarnings are ignored, other
  16. warnings are passed through.
  17. In practice, tests run from the numpy repo are run in development mode with
  18. ``spin``, through the standard ``spin test`` invocation or from an inplace
  19. build with ``pytest numpy``.
  20. This module is imported by every numpy subpackage, so lies at the top level to
  21. simplify circular import issues. For the same reason, it contains no numpy
  22. imports at module scope, instead importing numpy within function calls.
  23. """
  24. import sys
  25. import os
  26. __all__ = ['PytestTester']
  27. def _show_numpy_info():
  28. import numpy as np
  29. print("NumPy version %s" % np.__version__)
  30. info = np.lib._utils_impl._opt_info()
  31. print("NumPy CPU features: ", (info if info else 'nothing enabled'))
  32. class PytestTester:
  33. """
  34. Pytest test runner.
  35. A test function is typically added to a package's __init__.py like so::
  36. from numpy._pytesttester import PytestTester
  37. test = PytestTester(__name__).test
  38. del PytestTester
  39. Calling this test function finds and runs all tests associated with the
  40. module and all its sub-modules.
  41. Attributes
  42. ----------
  43. module_name : str
  44. Full path to the package to test.
  45. Parameters
  46. ----------
  47. module_name : module name
  48. The name of the module to test.
  49. Notes
  50. -----
  51. Unlike the previous ``nose``-based implementation, this class is not
  52. publicly exposed as it performs some ``numpy``-specific warning
  53. suppression.
  54. """
  55. def __init__(self, module_name):
  56. self.module_name = module_name
  57. self.__module__ = module_name
  58. def __call__(self, label='fast', verbose=1, extra_argv=None,
  59. doctests=False, coverage=False, durations=-1, tests=None):
  60. """
  61. Run tests for module using pytest.
  62. Parameters
  63. ----------
  64. label : {'fast', 'full'}, optional
  65. Identifies the tests to run. When set to 'fast', tests decorated
  66. with `pytest.mark.slow` are skipped, when 'full', the slow marker
  67. is ignored.
  68. verbose : int, optional
  69. Verbosity value for test outputs, in the range 1-3. Default is 1.
  70. extra_argv : list, optional
  71. List with any extra arguments to pass to pytests.
  72. doctests : bool, optional
  73. .. note:: Not supported
  74. coverage : bool, optional
  75. If True, report coverage of NumPy code. Default is False.
  76. Requires installation of (pip) pytest-cov.
  77. durations : int, optional
  78. If < 0, do nothing, If 0, report time of all tests, if > 0,
  79. report the time of the slowest `timer` tests. Default is -1.
  80. tests : test or list of tests
  81. Tests to be executed with pytest '--pyargs'
  82. Returns
  83. -------
  84. result : bool
  85. Return True on success, false otherwise.
  86. Notes
  87. -----
  88. Each NumPy module exposes `test` in its namespace to run all tests for
  89. it. For example, to run all tests for numpy.lib:
  90. >>> np.lib.test() #doctest: +SKIP
  91. Examples
  92. --------
  93. >>> result = np.lib.test() #doctest: +SKIP
  94. ...
  95. 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds
  96. >>> result
  97. True
  98. """
  99. import pytest
  100. import warnings
  101. module = sys.modules[self.module_name]
  102. module_path = os.path.abspath(module.__path__[0])
  103. # setup the pytest arguments
  104. pytest_args = ["-l"]
  105. # offset verbosity. The "-q" cancels a "-v".
  106. pytest_args += ["-q"]
  107. if sys.version_info < (3, 12):
  108. with warnings.catch_warnings():
  109. warnings.simplefilter("always")
  110. # Filter out distutils cpu warnings (could be localized to
  111. # distutils tests). ASV has problems with top level import,
  112. # so fetch module for suppression here.
  113. from numpy.distutils import cpuinfo
  114. # Filter out annoying import messages. Want these in both develop and
  115. # release mode.
  116. pytest_args += [
  117. "-W ignore:Not importing directory",
  118. "-W ignore:numpy.dtype size changed",
  119. "-W ignore:numpy.ufunc size changed",
  120. "-W ignore::UserWarning:cpuinfo",
  121. ]
  122. # When testing matrices, ignore their PendingDeprecationWarnings
  123. pytest_args += [
  124. "-W ignore:the matrix subclass is not",
  125. "-W ignore:Importing from numpy.matlib is",
  126. ]
  127. if doctests:
  128. pytest_args += ["--doctest-modules"]
  129. if extra_argv:
  130. pytest_args += list(extra_argv)
  131. if verbose > 1:
  132. pytest_args += ["-" + "v"*(verbose - 1)]
  133. if coverage:
  134. pytest_args += ["--cov=" + module_path]
  135. if label == "fast":
  136. # not importing at the top level to avoid circular import of module
  137. from numpy.testing import IS_PYPY
  138. if IS_PYPY:
  139. pytest_args += ["-m", "not slow and not slow_pypy"]
  140. else:
  141. pytest_args += ["-m", "not slow"]
  142. elif label != "full":
  143. pytest_args += ["-m", label]
  144. if durations >= 0:
  145. pytest_args += ["--durations=%s" % durations]
  146. if tests is None:
  147. tests = [self.module_name]
  148. pytest_args += ["--pyargs"] + list(tests)
  149. # run tests.
  150. _show_numpy_info()
  151. try:
  152. code = pytest.main(pytest_args)
  153. except SystemExit as exc:
  154. code = exc.code
  155. return code == 0