Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Pytest test running. 

3 

4This module implements the ``test()`` function for NumPy modules. The usual 

5boiler plate for doing that is to put the following in the module 

6``__init__.py`` file:: 

7 

8 from numpy._pytesttester import PytestTester 

9 test = PytestTester(__name__).test 

10 del PytestTester 

11 

12 

13Warnings filtering and other runtime settings should be dealt with in the 

14``pytest.ini`` file in the numpy repo root. The behavior of the test depends on 

15whether or not that file is found as follows: 

16 

17* ``pytest.ini`` is present (develop mode) 

18 All warnings except those explicitly filtered out are raised as error. 

19* ``pytest.ini`` is absent (release mode) 

20 DeprecationWarnings and PendingDeprecationWarnings are ignored, other 

21 warnings are passed through. 

22 

23In practice, tests run from the numpy repo are run in develop mode. That 

24includes the standard ``python runtests.py`` invocation. 

25 

26This module is imported by every numpy subpackage, so lies at the top level to 

27simplify circular import issues. For the same reason, it contains no numpy 

28imports at module scope, instead importing numpy within function calls. 

29""" 

30import sys 

31import os 

32 

33__all__ = ['PytestTester'] 

34 

35 

36 

37def _show_numpy_info(): 

38 import numpy as np 

39 

40 print("NumPy version %s" % np.__version__) 

41 relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous 

42 print("NumPy relaxed strides checking option:", relaxed_strides) 

43 

44 

45class PytestTester: 

46 """ 

47 Pytest test runner. 

48 

49 A test function is typically added to a package's __init__.py like so:: 

50 

51 from numpy._pytesttester import PytestTester 

52 test = PytestTester(__name__).test 

53 del PytestTester 

54 

55 Calling this test function finds and runs all tests associated with the 

56 module and all its sub-modules. 

57 

58 Attributes 

59 ---------- 

60 module_name : str 

61 Full path to the package to test. 

62 

63 Parameters 

64 ---------- 

65 module_name : module name 

66 The name of the module to test. 

67 

68 Notes 

69 ----- 

70 Unlike the previous ``nose``-based implementation, this class is not 

71 publicly exposed as it performs some ``numpy``-specific warning 

72 suppression. 

73 

74 """ 

75 def __init__(self, module_name): 

76 self.module_name = module_name 

77 

78 def __call__(self, label='fast', verbose=1, extra_argv=None, 

79 doctests=False, coverage=False, durations=-1, tests=None): 

80 """ 

81 Run tests for module using pytest. 

82 

83 Parameters 

84 ---------- 

85 label : {'fast', 'full'}, optional 

86 Identifies the tests to run. When set to 'fast', tests decorated 

87 with `pytest.mark.slow` are skipped, when 'full', the slow marker 

88 is ignored. 

89 verbose : int, optional 

90 Verbosity value for test outputs, in the range 1-3. Default is 1. 

91 extra_argv : list, optional 

92 List with any extra arguments to pass to pytests. 

93 doctests : bool, optional 

94 .. note:: Not supported 

95 coverage : bool, optional 

96 If True, report coverage of NumPy code. Default is False. 

97 Requires installation of (pip) pytest-cov. 

98 durations : int, optional 

99 If < 0, do nothing, If 0, report time of all tests, if > 0, 

100 report the time of the slowest `timer` tests. Default is -1. 

101 tests : test or list of tests 

102 Tests to be executed with pytest '--pyargs' 

103 

104 Returns 

105 ------- 

106 result : bool 

107 Return True on success, false otherwise. 

108 

109 Notes 

110 ----- 

111 Each NumPy module exposes `test` in its namespace to run all tests for 

112 it. For example, to run all tests for numpy.lib: 

113 

114 >>> np.lib.test() #doctest: +SKIP 

115 

116 Examples 

117 -------- 

118 >>> result = np.lib.test() #doctest: +SKIP 

119 ... 

120 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds 

121 >>> result 

122 True 

123 

124 """ 

125 import pytest 

126 import warnings 

127 

128 module = sys.modules[self.module_name] 

129 module_path = os.path.abspath(module.__path__[0]) 

130 

131 # setup the pytest arguments 

132 pytest_args = ["-l"] 

133 

134 # offset verbosity. The "-q" cancels a "-v". 

135 pytest_args += ["-q"] 

136 

137 # Filter out distutils cpu warnings (could be localized to 

138 # distutils tests). ASV has problems with top level import, 

139 # so fetch module for suppression here. 

140 with warnings.catch_warnings(): 

141 warnings.simplefilter("always") 

142 from numpy.distutils import cpuinfo 

143 

144 # Filter out annoying import messages. Want these in both develop and 

145 # release mode. 

146 pytest_args += [ 

147 "-W ignore:Not importing directory", 

148 "-W ignore:numpy.dtype size changed", 

149 "-W ignore:numpy.ufunc size changed", 

150 "-W ignore::UserWarning:cpuinfo", 

151 ] 

152 

153 # When testing matrices, ignore their PendingDeprecationWarnings 

154 pytest_args += [ 

155 "-W ignore:the matrix subclass is not", 

156 "-W ignore:Importing from numpy.matlib is", 

157 ] 

158 

159 if doctests: 

160 raise ValueError("Doctests not supported") 

161 

162 if extra_argv: 

163 pytest_args += list(extra_argv) 

164 

165 if verbose > 1: 

166 pytest_args += ["-" + "v"*(verbose - 1)] 

167 

168 if coverage: 

169 pytest_args += ["--cov=" + module_path] 

170 

171 if label == "fast": 

172 # not importing at the top level to avoid circular import of module 

173 from numpy.testing import IS_PYPY 

174 if IS_PYPY: 

175 pytest_args += ["-m", "not slow and not slow_pypy"] 

176 else: 

177 pytest_args += ["-m", "not slow"] 

178 

179 elif label != "full": 

180 pytest_args += ["-m", label] 

181 

182 if durations >= 0: 

183 pytest_args += ["--durations=%s" % durations] 

184 

185 if tests is None: 

186 tests = [self.module_name] 

187 

188 pytest_args += ["--pyargs"] + list(tests) 

189 

190 

191 # run tests. 

192 _show_numpy_info() 

193 

194 try: 

195 code = pytest.main(pytest_args) 

196 except SystemExit as exc: 

197 code = exc.code 

198 

199 return code == 0