conftest.py revision 12037
112863Sgabeblack@google.com"""pytest configuration 212863Sgabeblack@google.com 312863Sgabeblack@google.comExtends output capture as needed by pybind11: ignore constructors, optional unordered lines. 412863Sgabeblack@google.comAdds docstring and exceptions message sanitizers: ignore Python 2 vs 3 differences. 512863Sgabeblack@google.com""" 612863Sgabeblack@google.com 712863Sgabeblack@google.comimport pytest 812863Sgabeblack@google.comimport textwrap 912863Sgabeblack@google.comimport difflib 1012863Sgabeblack@google.comimport re 1112863Sgabeblack@google.comimport sys 1212863Sgabeblack@google.comimport contextlib 1312863Sgabeblack@google.comimport platform 1412863Sgabeblack@google.comimport gc 1512863Sgabeblack@google.com 1612863Sgabeblack@google.com_unicode_marker = re.compile(r'u(\'[^\']*\')') 1712863Sgabeblack@google.com_long_marker = re.compile(r'([0-9])L') 1812863Sgabeblack@google.com_hexadecimal = re.compile(r'0x[0-9a-fA-F]+') 1912863Sgabeblack@google.com 2012863Sgabeblack@google.com 2112863Sgabeblack@google.comdef _strip_and_dedent(s): 2212863Sgabeblack@google.com """For triple-quote strings""" 2312863Sgabeblack@google.com return textwrap.dedent(s.lstrip('\n').rstrip()) 2412863Sgabeblack@google.com 2512863Sgabeblack@google.com 2612863Sgabeblack@google.comdef _split_and_sort(s): 2712863Sgabeblack@google.com """For output which does not require specific line order""" 2812863Sgabeblack@google.com return sorted(_strip_and_dedent(s).splitlines()) 2912863Sgabeblack@google.com 3012863Sgabeblack@google.com 3112863Sgabeblack@google.comdef _make_explanation(a, b): 3212863Sgabeblack@google.com """Explanation for a failed assert -- the a and b arguments are List[str]""" 3312863Sgabeblack@google.com return ["--- actual / +++ expected"] + [line.strip('\n') for line in difflib.ndiff(a, b)] 3412863Sgabeblack@google.com 3512863Sgabeblack@google.com 3612863Sgabeblack@google.comclass Output(object): 3712863Sgabeblack@google.com """Basic output post-processing and comparison""" 3812863Sgabeblack@google.com def __init__(self, string): 3912863Sgabeblack@google.com self.string = string 4012863Sgabeblack@google.com self.explanation = [] 4112863Sgabeblack@google.com 4212863Sgabeblack@google.com def __str__(self): 4312863Sgabeblack@google.com return self.string 4412863Sgabeblack@google.com 4512863Sgabeblack@google.com def __eq__(self, other): 4612863Sgabeblack@google.com # Ignore constructor/destructor output which is prefixed with "###" 4712863Sgabeblack@google.com a = [line for line in self.string.strip().splitlines() if not line.startswith("###")] 4812863Sgabeblack@google.com b = _strip_and_dedent(other).splitlines() 4912863Sgabeblack@google.com if a == b: 5012863Sgabeblack@google.com return True 5112863Sgabeblack@google.com else: 5212863Sgabeblack@google.com self.explanation = _make_explanation(a, b) 5312863Sgabeblack@google.com return False 5412863Sgabeblack@google.com 5512863Sgabeblack@google.com 5612863Sgabeblack@google.comclass Unordered(Output): 5712863Sgabeblack@google.com """Custom comparison for output without strict line ordering""" 5812863Sgabeblack@google.com def __eq__(self, other): 5912863Sgabeblack@google.com a = _split_and_sort(self.string) 6012863Sgabeblack@google.com b = _split_and_sort(other) 6112863Sgabeblack@google.com if a == b: 6212863Sgabeblack@google.com return True 6312863Sgabeblack@google.com else: 6412863Sgabeblack@google.com self.explanation = _make_explanation(a, b) 6512863Sgabeblack@google.com return False 6612863Sgabeblack@google.com 6712863Sgabeblack@google.com 6812863Sgabeblack@google.comclass Capture(object): 6912863Sgabeblack@google.com def __init__(self, capfd): 7012863Sgabeblack@google.com self.capfd = capfd 71 self.out = "" 72 self.err = "" 73 74 def __enter__(self): 75 self.capfd.readouterr() 76 return self 77 78 def __exit__(self, *_): 79 self.out, self.err = self.capfd.readouterr() 80 81 def __eq__(self, other): 82 a = Output(self.out) 83 b = other 84 if a == b: 85 return True 86 else: 87 self.explanation = a.explanation 88 return False 89 90 def __str__(self): 91 return self.out 92 93 def __contains__(self, item): 94 return item in self.out 95 96 @property 97 def unordered(self): 98 return Unordered(self.out) 99 100 @property 101 def stderr(self): 102 return Output(self.err) 103 104 105@pytest.fixture 106def capture(capsys): 107 """Extended `capsys` with context manager and custom equality operators""" 108 return Capture(capsys) 109 110 111class SanitizedString(object): 112 def __init__(self, sanitizer): 113 self.sanitizer = sanitizer 114 self.string = "" 115 self.explanation = [] 116 117 def __call__(self, thing): 118 self.string = self.sanitizer(thing) 119 return self 120 121 def __eq__(self, other): 122 a = self.string 123 b = _strip_and_dedent(other) 124 if a == b: 125 return True 126 else: 127 self.explanation = _make_explanation(a.splitlines(), b.splitlines()) 128 return False 129 130 131def _sanitize_general(s): 132 s = s.strip() 133 s = s.replace("pybind11_tests.", "m.") 134 s = s.replace("unicode", "str") 135 s = _long_marker.sub(r"\1", s) 136 s = _unicode_marker.sub(r"\1", s) 137 return s 138 139 140def _sanitize_docstring(thing): 141 s = thing.__doc__ 142 s = _sanitize_general(s) 143 return s 144 145 146@pytest.fixture 147def doc(): 148 """Sanitize docstrings and add custom failure explanation""" 149 return SanitizedString(_sanitize_docstring) 150 151 152def _sanitize_message(thing): 153 s = str(thing) 154 s = _sanitize_general(s) 155 s = _hexadecimal.sub("0", s) 156 return s 157 158 159@pytest.fixture 160def msg(): 161 """Sanitize messages and add custom failure explanation""" 162 return SanitizedString(_sanitize_message) 163 164 165# noinspection PyUnusedLocal 166def pytest_assertrepr_compare(op, left, right): 167 """Hook to insert custom failure explanation""" 168 if hasattr(left, 'explanation'): 169 return left.explanation 170 171 172@contextlib.contextmanager 173def suppress(exception): 174 """Suppress the desired exception""" 175 try: 176 yield 177 except exception: 178 pass 179 180 181def gc_collect(): 182 ''' Run the garbage collector twice (needed when running 183 reference counting tests with PyPy) ''' 184 gc.collect() 185 gc.collect() 186 187 188def pytest_namespace(): 189 """Add import suppression and test requirements to `pytest` namespace""" 190 try: 191 import numpy as np 192 except ImportError: 193 np = None 194 try: 195 import scipy 196 except ImportError: 197 scipy = None 198 try: 199 from pybind11_tests import have_eigen 200 except ImportError: 201 have_eigen = False 202 pypy = platform.python_implementation() == "PyPy" 203 204 skipif = pytest.mark.skipif 205 return { 206 'suppress': suppress, 207 'requires_numpy': skipif(not np, reason="numpy is not installed"), 208 'requires_scipy': skipif(not np, reason="scipy is not installed"), 209 'requires_eigen_and_numpy': skipif(not have_eigen or not np, 210 reason="eigen and/or numpy are not installed"), 211 'requires_eigen_and_scipy': skipif(not have_eigen or not scipy, 212 reason="eigen and/or scipy are not installed"), 213 'unsupported_on_pypy': skipif(pypy, reason="unsupported on PyPy"), 214 'gc_collect': gc_collect 215 } 216 217 218def _test_import_pybind11(): 219 """Early diagnostic for test module initialization errors 220 221 When there is an error during initialization, the first import will report the 222 real error while all subsequent imports will report nonsense. This import test 223 is done early (in the pytest configuration file, before any tests) in order to 224 avoid the noise of having all tests fail with identical error messages. 225 226 Any possible exception is caught here and reported manually *without* the stack 227 trace. This further reduces noise since the trace would only show pytest internals 228 which are not useful for debugging pybind11 module issues. 229 """ 230 # noinspection PyBroadException 231 try: 232 import pybind11_tests # noqa: F401 imported but unused 233 except Exception as e: 234 print("Failed to import pybind11_tests from pytest:") 235 print(" {}: {}".format(type(e).__name__, e)) 236 sys.exit(1) 237 238 239_test_import_pybind11() 240