1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
# Copyright 2015 Facundo Batista
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# For further info, check https://github.com/facundobatista/logassert
"""Tests for the main module."""
import logging
import unittest
import logassert
class FakeTestCase:
"""A fake to record if stuff failed."""
def __init__(self):
self.failed = None
def fail(self, text):
self.failed = text
class BasicUsageTestCase(unittest.TestCase):
"""Basic usage."""
def setUp(self):
self.logger = logging.getLogger()
self.logger.handlers = []
def test_simple_assert_ok(self):
ftc = FakeTestCase()
logassert.setup(ftc, '')
self.logger.debug("test")
ftc.assertLogged("test")
self.assertEqual(ftc.failed, None)
def test_simple_assert_ok_with_replaces(self):
ftc = FakeTestCase()
logassert.setup(ftc, '')
self.logger.debug("test %d %r", 65, 'foobar')
ftc.assertLogged("test", "65", "foobar")
self.assertEqual(ftc.failed, None)
def test_simple_assert_fail(self):
ftc = FakeTestCase()
logassert.setup(ftc, '')
self.logger.debug("test")
ftc.assertLogged("test2")
self.assertEqual(ftc.failed, "Tokens ('test2',) not found, all was logged is...\n"
" DEBUG 'test'")
def test_simple_assert_fail_with_replaces(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.debug("test %d %r", 65, 'foobar')
ftc.assertLogged("test", "pumba")
self.assertEqual(ftc.failed, "Tokens ('test', 'pumba') not found, all was logged is...\n"
" DEBUG \"test 65 'foobar'\"")
def test_avoid_delayed_messaging(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
class Exploding:
"""Explode on delayed str."""
should_explode = False
def __str__(self):
if self.should_explode:
raise ValueError("str exploded")
return "didn't explode"
# log something using the Exploding class
exploding = Exploding()
logger.debug("feeling lucky? %s", exploding)
# now flag the class to explode and check
exploding.should_explode = True
ftc.assertLogged("feeling lucky", "didn't explode")
class LevelsTestCase(unittest.TestCase):
"""Work aware of logging levels."""
def test_assert_different_level_ok_debug(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.debug("test")
ftc.assertLoggedDebug("test")
self.assertEqual(ftc.failed, None)
def test_assert_different_level_ok_info(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.info("test")
ftc.assertLoggedInfo("test")
self.assertEqual(ftc.failed, None)
def test_assert_different_level_ok_error(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.error("test")
ftc.assertLoggedError("test")
self.assertEqual(ftc.failed, None)
def test_assert_different_level_ok_exception(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
try:
raise ValueError("test error")
except Exception:
logger.exception("test message")
ftc.assertLoggedError("test error")
ftc.assertLoggedError("test message")
ftc.assertLoggedError("ValueError")
self.assertEqual(ftc.failed, None)
def test_assert_different_level_ok_warning(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.warning("test")
ftc.assertLoggedWarning("test")
self.assertEqual(ftc.failed, None)
def test_assert_different_level_fail_oneway(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.warning("test")
ftc.assertLoggedDebug("test")
self.assertEqual(ftc.failed, "Tokens ('test',) not found in DEBUG, all was logged is...\n"
" WARNING 'test'")
def test_assert_different_level_fail_inverse(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.debug("test")
ftc.assertLoggedWarning("test")
self.assertEqual(
ftc.failed, "Tokens ('test',) not found in WARNING, all was logged is...\n"
" DEBUG 'test'")
class NotLoggedTestCase(unittest.TestCase):
"""Also check that it wasn't logged."""
def test_simple_ok(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.debug("test")
ftc.assertNotLogged("other")
self.assertEqual(ftc.failed, None)
def test_simple_fail(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.info("test")
ftc.assertNotLogged("test")
self.assertEqual(ftc.failed,
"Tokens ('test',) found in the following record: INFO 'test'")
def test_level_debug_ok(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.info("test")
ftc.assertNotLoggedDebug("test")
self.assertEqual(ftc.failed, None)
def test_level_debug_fail(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.debug("test")
ftc.assertNotLoggedDebug("test")
self.assertEqual(ftc.failed,
"Tokens ('test',) found in the following record: DEBUG 'test'")
def test_level_info(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.debug("test")
ftc.assertNotLoggedInfo("test")
self.assertEqual(ftc.failed, None)
def test_level_warning(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.info("test")
ftc.assertNotLoggedWarning("test")
self.assertEqual(ftc.failed, None)
def test_level_error(self):
ftc = FakeTestCase()
logger = logging.getLogger()
logassert.setup(ftc, '')
logger.info("test")
ftc.assertNotLoggedError("test")
self.assertEqual(ftc.failed, None)
|