2016-11-21 18:26:43 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2017-02-25 23:02:48 +08:00
|
|
|
WARNINGS_SUMMARY_HEADER = 'warnings summary'
|
|
|
|
|
2016-11-21 18:26:43 +08:00
|
|
|
@pytest.fixture
|
2017-03-05 07:53:42 +08:00
|
|
|
def pyfile_with_warnings(testdir, request):
|
|
|
|
"""
|
|
|
|
Create a test file which calls a function in a module which generates warnings.
|
|
|
|
"""
|
|
|
|
testdir.syspathinsert()
|
|
|
|
test_name = request.function.__name__
|
|
|
|
module_name = test_name.lstrip('test_') + '_module'
|
|
|
|
testdir.makepyfile(**{
|
|
|
|
module_name: '''
|
|
|
|
import warnings
|
|
|
|
def foo():
|
|
|
|
warnings.warn(PendingDeprecationWarning("functionality is pending deprecation"))
|
|
|
|
warnings.warn(DeprecationWarning("functionality is deprecated"))
|
|
|
|
return 1
|
|
|
|
''',
|
|
|
|
test_name: '''
|
|
|
|
import {module_name}
|
|
|
|
def test_func():
|
|
|
|
assert {module_name}.foo() == 1
|
|
|
|
'''.format(module_name=module_name)
|
|
|
|
})
|
2016-11-21 18:26:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_normal_flow(testdir, pyfile_with_warnings):
|
2017-03-05 07:53:42 +08:00
|
|
|
"""
|
|
|
|
Check that the warnings section is displayed, containing test node ids followed by
|
|
|
|
all warnings generated by that test node.
|
|
|
|
"""
|
2017-02-18 22:57:26 +08:00
|
|
|
result = testdir.runpytest()
|
2016-11-21 18:26:43 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2017-02-25 23:02:48 +08:00
|
|
|
'*== %s ==*' % WARNINGS_SUMMARY_HEADER,
|
2016-11-21 18:26:43 +08:00
|
|
|
|
2017-03-05 07:53:42 +08:00
|
|
|
'*test_normal_flow.py::test_func',
|
|
|
|
|
|
|
|
'*normal_flow_module.py:3: PendingDeprecationWarning: functionality is pending deprecation',
|
2016-11-21 18:26:43 +08:00
|
|
|
' warnings.warn(PendingDeprecationWarning("functionality is pending deprecation"))',
|
|
|
|
|
2017-03-05 07:53:42 +08:00
|
|
|
'*normal_flow_module.py:4: DeprecationWarning: functionality is deprecated',
|
2016-11-21 18:26:43 +08:00
|
|
|
' warnings.warn(DeprecationWarning("functionality is deprecated"))',
|
2017-02-25 23:02:48 +08:00
|
|
|
'* 1 passed, 2 warnings*',
|
2016-11-21 18:26:43 +08:00
|
|
|
])
|
2017-03-05 07:53:42 +08:00
|
|
|
assert result.stdout.str().count('test_normal_flow.py::test_func') == 1
|
2016-11-21 18:26:43 +08:00
|
|
|
|
|
|
|
|
2017-03-05 03:07:37 +08:00
|
|
|
def test_setup_teardown_warnings(testdir, pyfile_with_warnings):
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix():
|
|
|
|
warnings.warn(UserWarning("warning during setup"))
|
|
|
|
yield
|
|
|
|
warnings.warn(UserWarning("warning during teardown"))
|
|
|
|
|
|
|
|
def test_func(fix):
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'*== %s ==*' % WARNINGS_SUMMARY_HEADER,
|
|
|
|
|
|
|
|
'*test_setup_teardown_warnings.py:6: UserWarning: warning during setup',
|
|
|
|
' warnings.warn(UserWarning("warning during setup"))',
|
|
|
|
|
|
|
|
'*test_setup_teardown_warnings.py:8: UserWarning: warning during teardown',
|
|
|
|
' warnings.warn(UserWarning("warning during teardown"))',
|
|
|
|
'* 1 passed, 2 warnings*',
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2016-11-21 18:26:43 +08:00
|
|
|
@pytest.mark.parametrize('method', ['cmdline', 'ini'])
|
|
|
|
def test_as_errors(testdir, pyfile_with_warnings, method):
|
|
|
|
args = ('-W', 'error') if method == 'cmdline' else ()
|
|
|
|
if method == 'ini':
|
|
|
|
testdir.makeini('''
|
|
|
|
[pytest]
|
|
|
|
filterwarnings= error
|
|
|
|
''')
|
2017-02-18 22:57:26 +08:00
|
|
|
result = testdir.runpytest(*args)
|
2016-11-21 18:26:43 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'E PendingDeprecationWarning: functionality is pending deprecation',
|
2017-03-05 07:53:42 +08:00
|
|
|
'as_errors_module.py:3: PendingDeprecationWarning',
|
2016-11-21 18:26:43 +08:00
|
|
|
'* 1 failed in *',
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('method', ['cmdline', 'ini'])
|
|
|
|
def test_ignore(testdir, pyfile_with_warnings, method):
|
|
|
|
args = ('-W', 'ignore') if method == 'cmdline' else ()
|
|
|
|
if method == 'ini':
|
|
|
|
testdir.makeini('''
|
|
|
|
[pytest]
|
|
|
|
filterwarnings= ignore
|
|
|
|
''')
|
|
|
|
|
2017-02-18 22:57:26 +08:00
|
|
|
result = testdir.runpytest(*args)
|
2016-11-21 18:26:43 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'* 1 passed in *',
|
|
|
|
])
|
2017-02-25 23:02:48 +08:00
|
|
|
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
|
2016-11-21 18:26:43 +08:00
|
|
|
|