logfail: Update tests to use testdir.
This makes more sophisticated tests easier.
This commit is contained in:
parent
f5d9e967ef
commit
f7e40f73ab
@ -20,50 +20,111 @@
|
|||||||
"""Tests for the LogFailHandler test helper."""
|
"""Tests for the LogFailHandler test helper."""
|
||||||
|
|
||||||
|
|
||||||
|
import os.path
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_capturelog # pylint: disable=import-error
|
import pytest_capturelog # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
def test_log_debug():
|
pytest_plugins = 'pytester'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def log_testdir(testdir):
|
||||||
|
"""Testdir which uses our logfail.py as a conftest."""
|
||||||
|
log_fn = os.path.join(os.path.dirname(__file__), 'logfail.py')
|
||||||
|
with open(log_fn) as f:
|
||||||
|
testdir.makeconftest(f.read())
|
||||||
|
return testdir
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_debug(testdir):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def test_foo():
|
||||||
logging.debug('foo')
|
logging.debug('foo')
|
||||||
|
""")
|
||||||
|
res = testdir.inline_run()
|
||||||
|
res.assertoutcome(passed=1)
|
||||||
|
|
||||||
|
|
||||||
def test_log_warning():
|
def test_log_warning(testdir):
|
||||||
with pytest.raises(pytest.fail.Exception):
|
testdir.makepyfile("""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def test_foo():
|
||||||
logging.warning('foo')
|
logging.warning('foo')
|
||||||
|
""")
|
||||||
|
res = testdir.inline_run()
|
||||||
|
res.assertoutcome(failed=1)
|
||||||
|
|
||||||
|
|
||||||
def test_log_expected(caplog):
|
def test_log_expected(log_testdir):
|
||||||
|
"""For some reason this fails with inline_run."""
|
||||||
|
log_testdir.makepyfile("""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def test_foo(caplog):
|
||||||
with caplog.atLevel(logging.ERROR):
|
with caplog.atLevel(logging.ERROR):
|
||||||
logging.error('foo')
|
logging.error('foo')
|
||||||
|
""")
|
||||||
|
res = log_testdir.runpytest('-p capturelog')
|
||||||
|
res.stdout.fnmatch_lines(['*1 passed in*'])
|
||||||
|
|
||||||
|
|
||||||
def test_log_expected_logger(caplog):
|
def test_log_expected_logger(testdir):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def test_foo(caplog):
|
||||||
logger = 'logfail_test_logger'
|
logger = 'logfail_test_logger'
|
||||||
with caplog.atLevel(logging.ERROR, logger):
|
with caplog.atLevel(logging.ERROR, logger):
|
||||||
logging.getLogger(logger).error('foo')
|
logging.getLogger(logger).error('foo')
|
||||||
|
""")
|
||||||
|
res = testdir.inline_run()
|
||||||
|
res.assertoutcome(passed=1)
|
||||||
|
|
||||||
|
|
||||||
def test_log_expected_wrong_level(caplog):
|
def test_log_expected_wrong_level(testdir):
|
||||||
with pytest.raises(pytest.fail.Exception):
|
testdir.makepyfile("""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def test_foo(caplog):
|
||||||
with caplog.atLevel(logging.ERROR):
|
with caplog.atLevel(logging.ERROR):
|
||||||
logging.critical('foo')
|
logging.critical('foo')
|
||||||
|
""")
|
||||||
|
res = testdir.inline_run()
|
||||||
|
res.assertoutcome(failed=1)
|
||||||
|
|
||||||
|
|
||||||
def test_log_expected_logger_wrong_level(caplog):
|
def test_log_expected_logger_wrong_level(testdir):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def test_foo(caplog):
|
||||||
logger = 'logfail_test_logger'
|
logger = 'logfail_test_logger'
|
||||||
with pytest.raises(pytest.fail.Exception):
|
|
||||||
with caplog.atLevel(logging.ERROR, logger):
|
with caplog.atLevel(logging.ERROR, logger):
|
||||||
logging.getLogger(logger).critical('foo')
|
logging.getLogger(logger).critical('foo')
|
||||||
|
""")
|
||||||
|
res = testdir.inline_run()
|
||||||
|
res.assertoutcome(failed=1)
|
||||||
|
|
||||||
|
|
||||||
def test_log_expected_wrong_logger(caplog):
|
def test_log_expected_wrong_logger(testdir):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def test_foo(caplog):
|
||||||
logger = 'logfail_test_logger'
|
logger = 'logfail_test_logger'
|
||||||
with pytest.raises(pytest.fail.Exception):
|
|
||||||
with caplog.atLevel(logging.ERROR, logger):
|
with caplog.atLevel(logging.ERROR, logger):
|
||||||
logging.error('foo')
|
logging.error('foo')
|
||||||
|
""")
|
||||||
|
res = testdir.inline_run()
|
||||||
|
res.assertoutcome(failed=1)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
Loading…
Reference in New Issue
Block a user