Use warnings.catch_warnings instead of WarningsRecorder

This has the benefical side-effect of not calling the original
warnings.showwarnings function, which in its original form
only writes the formatted warning to sys.stdout.

Calling the original warnings.showwarnings has the effect that nested WarningsRecorder all catch the warnings:

with WarningsRecorder() as rec1:
    with WarningsRecorder() as rec2:
        warnings.warn(UserWarning, 'some warning')

(both rec1 and rec2 sees the warning)

When running tests with `testdir`, the main pytest session would then see the warnings created by
the internal code being tested (if any), and the main pytest session would end up with warnings as well.
This commit is contained in:
Bruno Oliveira
2017-02-18 12:57:26 -02:00
parent a7643a5fbe
commit 82785fcd40
2 changed files with 8 additions and 26 deletions
+3 -3
View File
@@ -12,7 +12,7 @@ def pyfile_with_warnings(testdir):
def test_normal_flow(testdir, pyfile_with_warnings):
result = testdir.runpytest_subprocess()
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*== pytest-warning summary ==*',
@@ -35,7 +35,7 @@ def test_as_errors(testdir, pyfile_with_warnings, method):
[pytest]
filterwarnings= error
''')
result = testdir.runpytest_subprocess(*args)
result = testdir.runpytest(*args)
result.stdout.fnmatch_lines([
'E PendingDeprecationWarning: functionality is pending deprecation',
'test_as_errors.py:3: PendingDeprecationWarning',
@@ -52,7 +52,7 @@ def test_ignore(testdir, pyfile_with_warnings, method):
filterwarnings= ignore
''')
result = testdir.runpytest_subprocess(*args)
result = testdir.runpytest(*args)
result.stdout.fnmatch_lines([
'* 1 passed in *',
])