/testing/web-platform/tests/tools/pytest/testing/test_recwarn.py

https://github.com/rillian/firefox · Python · 227 lines · 183 code · 36 blank · 8 comment · 26 complexity · 31515e739ec88119afaff1c2f246aebc MD5 · raw file

  1. import warnings
  2. import py
  3. import pytest
  4. from _pytest.recwarn import WarningsRecorder
  5. def test_recwarn_functional(testdir):
  6. reprec = testdir.inline_runsource("""
  7. import warnings
  8. oldwarn = warnings.showwarning
  9. def test_method(recwarn):
  10. assert warnings.showwarning != oldwarn
  11. warnings.warn("hello")
  12. warn = recwarn.pop()
  13. assert isinstance(warn.message, UserWarning)
  14. def test_finalized():
  15. assert warnings.showwarning == oldwarn
  16. """)
  17. res = reprec.countoutcomes()
  18. assert tuple(res) == (2, 0, 0), res
  19. class TestWarningsRecorderChecker(object):
  20. def test_recording(self, recwarn):
  21. showwarning = py.std.warnings.showwarning
  22. rec = WarningsRecorder()
  23. with rec:
  24. assert py.std.warnings.showwarning != showwarning
  25. assert not rec.list
  26. py.std.warnings.warn_explicit("hello", UserWarning, "xyz", 13)
  27. assert len(rec.list) == 1
  28. py.std.warnings.warn(DeprecationWarning("hello"))
  29. assert len(rec.list) == 2
  30. warn = rec.pop()
  31. assert str(warn.message) == "hello"
  32. l = rec.list
  33. rec.clear()
  34. assert len(rec.list) == 0
  35. assert l is rec.list
  36. pytest.raises(AssertionError, "rec.pop()")
  37. assert showwarning == py.std.warnings.showwarning
  38. def test_typechecking(self):
  39. from _pytest.recwarn import WarningsChecker
  40. with pytest.raises(TypeError):
  41. WarningsChecker(5)
  42. with pytest.raises(TypeError):
  43. WarningsChecker(('hi', RuntimeWarning))
  44. with pytest.raises(TypeError):
  45. WarningsChecker([DeprecationWarning, RuntimeWarning])
  46. def test_invalid_enter_exit(self):
  47. # wrap this test in WarningsRecorder to ensure warning state gets reset
  48. with WarningsRecorder():
  49. with pytest.raises(RuntimeError):
  50. rec = WarningsRecorder()
  51. rec.__exit__(None, None, None) # can't exit before entering
  52. with pytest.raises(RuntimeError):
  53. rec = WarningsRecorder()
  54. with rec:
  55. with rec:
  56. pass # can't enter twice
  57. class TestDeprecatedCall(object):
  58. """test pytest.deprecated_call()"""
  59. def dep(self, i, j=None):
  60. if i == 0:
  61. py.std.warnings.warn("is deprecated", DeprecationWarning,
  62. stacklevel=1)
  63. return 42
  64. def dep_explicit(self, i):
  65. if i == 0:
  66. py.std.warnings.warn_explicit("dep_explicit", category=DeprecationWarning,
  67. filename="hello", lineno=3)
  68. def test_deprecated_call_raises(self):
  69. with pytest.raises(AssertionError) as excinfo:
  70. pytest.deprecated_call(self.dep, 3, 5)
  71. assert str(excinfo).find("did not produce") != -1
  72. def test_deprecated_call(self):
  73. pytest.deprecated_call(self.dep, 0, 5)
  74. def test_deprecated_call_ret(self):
  75. ret = pytest.deprecated_call(self.dep, 0)
  76. assert ret == 42
  77. def test_deprecated_call_preserves(self):
  78. onceregistry = py.std.warnings.onceregistry.copy()
  79. filters = py.std.warnings.filters[:]
  80. warn = py.std.warnings.warn
  81. warn_explicit = py.std.warnings.warn_explicit
  82. self.test_deprecated_call_raises()
  83. self.test_deprecated_call()
  84. assert onceregistry == py.std.warnings.onceregistry
  85. assert filters == py.std.warnings.filters
  86. assert warn is py.std.warnings.warn
  87. assert warn_explicit is py.std.warnings.warn_explicit
  88. def test_deprecated_explicit_call_raises(self):
  89. with pytest.raises(AssertionError):
  90. pytest.deprecated_call(self.dep_explicit, 3)
  91. def test_deprecated_explicit_call(self):
  92. pytest.deprecated_call(self.dep_explicit, 0)
  93. pytest.deprecated_call(self.dep_explicit, 0)
  94. def test_deprecated_call_as_context_manager_no_warning(self):
  95. with pytest.raises(pytest.fail.Exception) as ex:
  96. with pytest.deprecated_call():
  97. self.dep(1)
  98. assert str(ex.value) == "DID NOT WARN"
  99. def test_deprecated_call_as_context_manager(self):
  100. with pytest.deprecated_call():
  101. self.dep(0)
  102. def test_deprecated_call_pending(self):
  103. def f():
  104. py.std.warnings.warn(PendingDeprecationWarning("hi"))
  105. pytest.deprecated_call(f)
  106. def test_deprecated_call_specificity(self):
  107. other_warnings = [Warning, UserWarning, SyntaxWarning, RuntimeWarning,
  108. FutureWarning, ImportWarning, UnicodeWarning]
  109. for warning in other_warnings:
  110. def f():
  111. py.std.warnings.warn(warning("hi"))
  112. with pytest.raises(AssertionError):
  113. pytest.deprecated_call(f)
  114. def test_deprecated_function_already_called(self, testdir):
  115. """deprecated_call should be able to catch a call to a deprecated
  116. function even if that function has already been called in the same
  117. module. See #1190.
  118. """
  119. testdir.makepyfile("""
  120. import warnings
  121. import pytest
  122. def deprecated_function():
  123. warnings.warn("deprecated", DeprecationWarning)
  124. def test_one():
  125. deprecated_function()
  126. def test_two():
  127. pytest.deprecated_call(deprecated_function)
  128. """)
  129. result = testdir.runpytest()
  130. result.stdout.fnmatch_lines('*=== 2 passed in *===')
  131. class TestWarns(object):
  132. def test_strings(self):
  133. # different messages, b/c Python suppresses multiple identical warnings
  134. source1 = "warnings.warn('w1', RuntimeWarning)"
  135. source2 = "warnings.warn('w2', RuntimeWarning)"
  136. source3 = "warnings.warn('w3', RuntimeWarning)"
  137. pytest.warns(RuntimeWarning, source1)
  138. pytest.raises(pytest.fail.Exception,
  139. lambda: pytest.warns(UserWarning, source2))
  140. pytest.warns(RuntimeWarning, source3)
  141. def test_function(self):
  142. pytest.warns(SyntaxWarning,
  143. lambda msg: warnings.warn(msg, SyntaxWarning), "syntax")
  144. def test_warning_tuple(self):
  145. pytest.warns((RuntimeWarning, SyntaxWarning),
  146. lambda: warnings.warn('w1', RuntimeWarning))
  147. pytest.warns((RuntimeWarning, SyntaxWarning),
  148. lambda: warnings.warn('w2', SyntaxWarning))
  149. pytest.raises(pytest.fail.Exception,
  150. lambda: pytest.warns(
  151. (RuntimeWarning, SyntaxWarning),
  152. lambda: warnings.warn('w3', UserWarning)))
  153. def test_as_contextmanager(self):
  154. with pytest.warns(RuntimeWarning):
  155. warnings.warn("runtime", RuntimeWarning)
  156. with pytest.raises(pytest.fail.Exception):
  157. with pytest.warns(RuntimeWarning):
  158. warnings.warn("user", UserWarning)
  159. with pytest.raises(pytest.fail.Exception):
  160. with pytest.warns(UserWarning):
  161. warnings.warn("runtime", RuntimeWarning)
  162. with pytest.warns(UserWarning):
  163. warnings.warn("user", UserWarning)
  164. def test_record(self):
  165. with pytest.warns(UserWarning) as record:
  166. warnings.warn("user", UserWarning)
  167. assert len(record) == 1
  168. assert str(record[0].message) == "user"
  169. def test_record_only(self):
  170. with pytest.warns(None) as record:
  171. warnings.warn("user", UserWarning)
  172. warnings.warn("runtime", RuntimeWarning)
  173. assert len(record) == 2
  174. assert str(record[0].message) == "user"
  175. assert str(record[1].message) == "runtime"
  176. def test_double_test(self, testdir):
  177. """If a test is run again, the warning should still be raised"""
  178. testdir.makepyfile('''
  179. import pytest
  180. import warnings
  181. @pytest.mark.parametrize('run', [1, 2])
  182. def test(run):
  183. with pytest.warns(RuntimeWarning):
  184. warnings.warn("runtime", RuntimeWarning)
  185. ''')
  186. result = testdir.runpytest()
  187. result.stdout.fnmatch_lines(['*2 passed in*'])