_pytesttester.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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__).test
  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. class PytestTester:
  32. """
  33. Pytest test runner.
  34. A test function is typically added to a package's __init__.py like so::
  35. from numpy._pytesttester import PytestTester
  36. test = PytestTester(__name__).test
  37. del PytestTester
  38. Calling this test function finds and runs all tests associated with the
  39. module and all its sub-modules.
  40. Attributes
  41. ----------
  42. module_name : str
  43. Full path to the package to test.
  44. Parameters
  45. ----------
  46. module_name : module name
  47. The name of the module to test.
  48. Notes
  49. -----
  50. Unlike the previous ``nose``-based implementation, this class is not
  51. publicly exposed as it performs some ``numpy``-specific warning
  52. suppression.
  53. """
  54. def __init__(self, module_name):
  55. self.module_name = module_name
  56. def __call__(self, label='fast', verbose=1, extra_argv=None,
  57. doctests=False, coverage=False, durations=-1, tests=None):
  58. """
  59. Run tests for module using pytest.
  60. Parameters
  61. ----------
  62. label : {'fast', 'full'}, optional
  63. Identifies the tests to run. When set to 'fast', tests decorated
  64. with `pytest.mark.slow` are skipped, when 'full', the slow marker
  65. is ignored.
  66. verbose : int, optional
  67. Verbosity value for test outputs, in the range 1-3. Default is 1.
  68. extra_argv : list, optional
  69. List with any extra arguments to pass to pytests.
  70. doctests : bool, optional
  71. .. note:: Not supported
  72. coverage : bool, optional
  73. If True, report coverage of NumPy code. Default is False.
  74. Requires installation of (pip) pytest-cov.
  75. durations : int, optional
  76. If < 0, do nothing, If 0, report time of all tests, if > 0,
  77. report the time of the slowest `timer` tests. Default is -1.
  78. tests : test or list of tests
  79. Tests to be executed with pytest '--pyargs'
  80. Returns
  81. -------
  82. result : bool
  83. Return True on success, false otherwise.
  84. Notes
  85. -----
  86. Each NumPy module exposes `test` in its namespace to run all tests for
  87. it. For example, to run all tests for numpy.lib:
  88. >>> np.lib.test() #doctest: +SKIP
  89. Examples
  90. --------
  91. >>> result = np.lib.test() #doctest: +SKIP
  92. ...
  93. 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds
  94. >>> result
  95. True
  96. """
  97. import pytest
  98. import warnings
  99. # Imported after pytest to enable assertion rewriting
  100. import hypothesis
  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. # This configuration is picked up by numpy.conftest, and ensures that
  149. # running `np.test()` is deterministic and does not write any files.
  150. # See https://hypothesis.readthedocs.io/en/latest/settings.html
  151. hypothesis.settings.register_profile(
  152. name="np.test() profile",
  153. deadline=None, print_blob=True, database=None, derandomize=True,
  154. suppress_health_check=hypothesis.HealthCheck.all(),
  155. )
  156. # run tests.
  157. _show_numpy_info()
  158. try:
  159. code = pytest.main(pytest_args)
  160. except SystemExit as exc:
  161. code = exc.code
  162. return code == 0