_pytesttester.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. # Filter out distutils cpu warnings (could be localized to
  108. # distutils tests). ASV has problems with top level import,
  109. # so fetch module for suppression here.
  110. with warnings.catch_warnings():
  111. warnings.simplefilter("always")
  112. from numpy.distutils import cpuinfo
  113. # Filter out annoying import messages. Want these in both develop and
  114. # release mode.
  115. pytest_args += [
  116. "-W ignore:Not importing directory",
  117. "-W ignore:numpy.dtype size changed",
  118. "-W ignore:numpy.ufunc size changed",
  119. "-W ignore::UserWarning:cpuinfo",
  120. ]
  121. # When testing matrices, ignore their PendingDeprecationWarnings
  122. pytest_args += [
  123. "-W ignore:the matrix subclass is not",
  124. "-W ignore:Importing from numpy.matlib is",
  125. ]
  126. if doctests:
  127. raise ValueError("Doctests not supported")
  128. if extra_argv:
  129. pytest_args += list(extra_argv)
  130. if verbose > 1:
  131. pytest_args += ["-" + "v"*(verbose - 1)]
  132. if coverage:
  133. pytest_args += ["--cov=" + module_path]
  134. if label == "fast":
  135. # not importing at the top level to avoid circular import of module
  136. from numpy.testing import IS_PYPY
  137. if IS_PYPY:
  138. pytest_args += ["-m", "not slow and not slow_pypy"]
  139. else:
  140. pytest_args += ["-m", "not slow"]
  141. elif label != "full":
  142. pytest_args += ["-m", label]
  143. if durations >= 0:
  144. pytest_args += ["--durations=%s" % durations]
  145. if tests is None:
  146. tests = [self.module_name]
  147. pytest_args += ["--pyargs"] + list(tests)
  148. # run tests.
  149. _show_numpy_info()
  150. try:
  151. code = pytest.main(pytest_args)
  152. except SystemExit as exc:
  153. code = exc.code
  154. return code == 0