_pytesttester.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 develop mode. That
  18. includes the standard ``python runtests.py`` invocation.
  19. This module is imported by every numpy subpackage, so lies at the top level to
  20. simplify circular import issues. For the same reason, it contains no numpy
  21. imports at module scope, instead importing numpy within function calls.
  22. """
  23. import sys
  24. import os
  25. __all__ = ['PytestTester']
  26. def _show_numpy_info():
  27. import numpy as np
  28. print("NumPy version %s" % np.__version__)
  29. relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous
  30. print("NumPy relaxed strides checking option:", relaxed_strides)
  31. info = np.lib.utils._opt_info()
  32. print("NumPy CPU features: ", (info if info else 'nothing enabled'))
  33. class PytestTester:
  34. """
  35. Pytest test runner.
  36. A test function is typically added to a package's __init__.py like so::
  37. from numpy._pytesttester import PytestTester
  38. test = PytestTester(__name__).test
  39. del PytestTester
  40. Calling this test function finds and runs all tests associated with the
  41. module and all its sub-modules.
  42. Attributes
  43. ----------
  44. module_name : str
  45. Full path to the package to test.
  46. Parameters
  47. ----------
  48. module_name : module name
  49. The name of the module to test.
  50. Notes
  51. -----
  52. Unlike the previous ``nose``-based implementation, this class is not
  53. publicly exposed as it performs some ``numpy``-specific warning
  54. suppression.
  55. """
  56. def __init__(self, module_name):
  57. self.module_name = 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. with warnings.catch_warnings(record=True):
  115. # Ignore the warning from importing the array_api submodule. This
  116. # warning is done on import, so it would break pytest collection,
  117. # but importing it early here prevents the warning from being
  118. # issued when it imported again.
  119. import numpy.array_api
  120. # Filter out annoying import messages. Want these in both develop and
  121. # release mode.
  122. pytest_args += [
  123. "-W ignore:Not importing directory",
  124. "-W ignore:numpy.dtype size changed",
  125. "-W ignore:numpy.ufunc size changed",
  126. "-W ignore::UserWarning:cpuinfo",
  127. ]
  128. # When testing matrices, ignore their PendingDeprecationWarnings
  129. pytest_args += [
  130. "-W ignore:the matrix subclass is not",
  131. "-W ignore:Importing from numpy.matlib is",
  132. ]
  133. if doctests:
  134. pytest_args += ["--doctest-modules"]
  135. if extra_argv:
  136. pytest_args += list(extra_argv)
  137. if verbose > 1:
  138. pytest_args += ["-" + "v"*(verbose - 1)]
  139. if coverage:
  140. pytest_args += ["--cov=" + module_path]
  141. if label == "fast":
  142. # not importing at the top level to avoid circular import of module
  143. from numpy.testing import IS_PYPY
  144. if IS_PYPY:
  145. pytest_args += ["-m", "not slow and not slow_pypy"]
  146. else:
  147. pytest_args += ["-m", "not slow"]
  148. elif label != "full":
  149. pytest_args += ["-m", label]
  150. if durations >= 0:
  151. pytest_args += ["--durations=%s" % durations]
  152. if tests is None:
  153. tests = [self.module_name]
  154. pytest_args += ["--pyargs"] + list(tests)
  155. # run tests.
  156. _show_numpy_info()
  157. try:
  158. code = pytest.main(pytest_args)
  159. except SystemExit as exc:
  160. code = exc.code
  161. return code == 0