_pytesttester.py 6.7 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 explicily 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. from __future__ import division, absolute_import, print_function
  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. relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous
  31. print("NumPy relaxed strides checking option:", relaxed_strides)
  32. class PytestTester(object):
  33. """
  34. Pytest test runner.
  35. This class is made available in ``numpy.testing``, and a test function
  36. is typically added to a package's __init__.py like so::
  37. from numpy.testing 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. """
  51. def __init__(self, module_name):
  52. self.module_name = module_name
  53. def __call__(self, label='fast', verbose=1, extra_argv=None,
  54. doctests=False, coverage=False, durations=-1, tests=None):
  55. """
  56. Run tests for module using pytest.
  57. Parameters
  58. ----------
  59. label : {'fast', 'full'}, optional
  60. Identifies the tests to run. When set to 'fast', tests decorated
  61. with `pytest.mark.slow` are skipped, when 'full', the slow marker
  62. is ignored.
  63. verbose : int, optional
  64. Verbosity value for test outputs, in the range 1-3. Default is 1.
  65. extra_argv : list, optional
  66. List with any extra arguments to pass to pytests.
  67. doctests : bool, optional
  68. .. note:: Not supported
  69. coverage : bool, optional
  70. If True, report coverage of NumPy code. Default is False.
  71. Requires installation of (pip) pytest-cov.
  72. durations : int, optional
  73. If < 0, do nothing, If 0, report time of all tests, if > 0,
  74. report the time of the slowest `timer` tests. Default is -1.
  75. tests : test or list of tests
  76. Tests to be executed with pytest '--pyargs'
  77. Returns
  78. -------
  79. result : bool
  80. Return True on success, false otherwise.
  81. Notes
  82. -----
  83. Each NumPy module exposes `test` in its namespace to run all tests for
  84. it. For example, to run all tests for numpy.lib:
  85. >>> np.lib.test() #doctest: +SKIP
  86. Examples
  87. --------
  88. >>> result = np.lib.test() #doctest: +SKIP
  89. ...
  90. 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds
  91. >>> result
  92. True
  93. """
  94. import pytest
  95. import warnings
  96. #FIXME This is no longer needed? Assume it was for use in tests.
  97. # cap verbosity at 3, which is equivalent to the pytest '-vv' option
  98. #from . import utils
  99. #verbose = min(int(verbose), 3)
  100. #utils.verbose = verbose
  101. #
  102. module = sys.modules[self.module_name]
  103. module_path = os.path.abspath(module.__path__[0])
  104. # setup the pytest arguments
  105. pytest_args = ["-l"]
  106. # offset verbosity. The "-q" cancels a "-v".
  107. pytest_args += ["-q"]
  108. # Filter out distutils cpu warnings (could be localized to
  109. # distutils tests). ASV has problems with top level import,
  110. # so fetch module for suppression here.
  111. with warnings.catch_warnings():
  112. warnings.simplefilter("always")
  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. ]
  126. # Ignore python2.7 -3 warnings
  127. pytest_args += [
  128. r"-W ignore:sys\.exc_clear\(\) not supported in 3\.x:DeprecationWarning",
  129. r"-W ignore:in 3\.x, __setslice__:DeprecationWarning",
  130. r"-W ignore:in 3\.x, __getslice__:DeprecationWarning",
  131. r"-W ignore:buffer\(\) not supported in 3\.x:DeprecationWarning",
  132. r"-W ignore:CObject type is not supported in 3\.x:DeprecationWarning",
  133. r"-W ignore:comparing unequal types not supported in 3\.x:DeprecationWarning",
  134. r"-W ignore:the commands module has been removed in Python 3\.0:DeprecationWarning",
  135. r"-W ignore:The 'new' module has been removed in Python 3\.0:DeprecationWarning",
  136. ]
  137. if doctests:
  138. raise ValueError("Doctests not supported")
  139. if extra_argv:
  140. pytest_args += list(extra_argv)
  141. if verbose > 1:
  142. pytest_args += ["-" + "v"*(verbose - 1)]
  143. if coverage:
  144. pytest_args += ["--cov=" + module_path]
  145. if label == "fast":
  146. pytest_args += ["-m", "not slow"]
  147. elif label != "full":
  148. pytest_args += ["-m", label]
  149. if durations >= 0:
  150. pytest_args += ["--durations=%s" % durations]
  151. if tests is None:
  152. tests = [self.module_name]
  153. pytest_args += ["--pyargs"] + list(tests)
  154. # run tests.
  155. _show_numpy_info()
  156. try:
  157. code = pytest.main(pytest_args)
  158. except SystemExit as exc:
  159. code = exc.code
  160. return code == 0