Best Python code snippet using pyatom_python
test_log.py
Source:test_log.py
1# -*- Mode: Python; test-case-name: test_log -*-2# vi:si:et:sw=4:sts=4:ts=43#4# Flumotion - a streaming media server5# Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com).6# All rights reserved.7# This file may be distributed and/or modified under the terms of8# the GNU General Public License version 2 as published by9# the Free Software Foundation.10# This file is distributed without any warranty; without even the implied11# warranty of merchantability or fitness for a particular purpose.12# See "LICENSE.GPL" in the source distribution for more information.13# Licensees having purchased or holding a valid Flumotion Advanced14# Streaming Server license may use this file in accordance with the15# Flumotion Advanced Streaming Server Commercial License Agreement.16# See "LICENSE.Flumotion" in the source distribution for more information.17# Headers in this file shall remain intact.18from twisted.trial import unittest19import log20__version__ = "$Rev: 7162 $"21class LogTester(log.Loggable):22 logCategory = 'testlog'23class LogFunctionTester(log.Loggable):24 def logFunction(self, format, *args):25 return (("override " + format), ) + args[1:]26class TestLog(unittest.TestCase):27 def setUp(self):28 self.category = self.level = self.message = None29 self.tester = LogTester()30 # we want to remove the default handler so it doesn't show up stuff31 log.reset()32 # just test for parsing semi- or non-valid FLU_DEBUG variables33 def testSetDebug(self):34 log.setDebug(":5")35 log.setDebug("*")36 log.setDebug("5")37 # test for adding a log handler38 def handler(self, level, object, category, file, line, message):39 self.level = level40 self.object = object41 self.category = category42 self.file = file43 self.line = line44 self.message = message45 def testLimitInvisible(self):46 log.setDebug("testlog:3")47 log.addLimitedLogHandler(self.handler)48 # log 2 we shouldn't get49 self.tester.log("not visible")50 assert not self.category51 assert not self.level52 assert not self.message53 self.tester.debug("not visible")54 assert not self.category55 assert not self.level56 assert not self.message57 def testLimitedVisible(self):58 log.setDebug("testlog:3")59 log.addLimitedLogHandler(self.handler)60 # log 3 we should get61 self.tester.info("visible")62 assert self.category == 'testlog'63 assert self.level == log.INFO64 assert self.message == 'visible'65 self.tester.warning("also visible")66 assert self.category == 'testlog'67 assert self.level == log.WARN68 assert self.message == 'also visible'69 def testFormatStrings(self):70 log.setDebug("testlog:3")71 log.addLimitedLogHandler(self.handler)72 self.tester.info("%d %s", 42, 'the answer')73 assert self.category == 'testlog'74 assert self.level == log.INFO75 assert self.message == '42 the answer'76 def testLimitedError(self):77 log.setDebug("testlog:3")78 log.addLimitedLogHandler(self.handler)79 self.assertRaises(SystemExit, self.tester.error, "error")80 assert self.category == 'testlog'81 assert self.level == log.ERROR82 assert self.message == 'error'83 def testLogHandlerLimitedLevels(self):84 log.setDebug("testlog:3")85 log.addLimitedLogHandler(self.handler)86 # now try debug and log again too87 log.setDebug("testlog:5")88 self.tester.debug("debug")89 assert self.category == 'testlog'90 assert self.level == log.DEBUG91 assert self.message == 'debug'92 self.tester.log("log")93 assert self.category == 'testlog'94 assert self.level == log.LOG95 assert self.message == 'log'96 # test that we get all log messages97 def testLogHandler(self):98 log.setDebug("testlog:3")99 log.addLogHandler(self.handler)100 self.tester.log("visible")101 assert self.message == 'visible'102 self.tester.warning("also visible")103 assert self.message == 'also visible'104class TestOwnLogHandler(unittest.TestCase):105 def setUp(self):106 self.category = self.level = self.message = None107 self.tester = LogFunctionTester()108 def handler(self, level, object, category, file, line, message):109 self.level = level110 self.object = object111 self.category = category112 self.file = file113 self.line = line114 self.message = message115 # test if our own log handler correctly mangles the message116 def testOwnLogHandlerLimited(self):117 log.setDebug("testlog:3")118 log.addLogHandler(self.handler)119 self.tester.log("visible")120 assert self.message == 'override visible'121 def testLogHandlerAssertion(self):122 self.assertRaises(TypeError, log.addLimitedLogHandler, None)123class TestGetExceptionMessage(unittest.TestCase):124 def func3(self):125 self.func2()126 def func2(self):127 self.func1()128 def func1(self):129 raise TypeError("I am in func1")130 def testLevel2(self):131 try:132 self.func2()133 self.fail()134 except TypeError, e:135 self.verifyException(e)136 def testLevel3(self):137 try:138 self.func3()139 self.fail()140 except TypeError, e:141 self.verifyException(e)142 def verifyException(self, e):143 message = log.getExceptionMessage(e)144 self.failUnless("func1()" in message)145 self.failUnless("test_log.py" in message)146 self.failUnless("TypeError" in message)147class TestLogSettings(unittest.TestCase):148 def testSet(self):149 old = log.getLogSettings()150 log.setDebug('*:5')151 self.assertNotEquals(old, log.getLogSettings())152 log.setLogSettings(old)153 self.assertEquals(old, log.getLogSettings())154class TestWriteMark(unittest.TestCase):155 def handler(self, level, object, category, file, line, message):156 self.level = level157 self.object = object158 self.category = category159 self.file = file160 self.line = line161 self.message = message162 def testWriteMarkInDebug(self):163 loggable = log.Loggable()164 log.setDebug("4")165 log.addLogHandler(self.handler)166 marker = 'test'167 loggable.writeMarker(marker, log.DEBUG)168 self.assertEquals(self.message, marker)169 def testWriteMarkInWarn(self):170 loggable = log.Loggable()171 log.setDebug("2")172 log.addLogHandler(self.handler)173 marker = 'test'174 loggable.writeMarker(marker, log.WARN)175 self.assertEquals(self.message, marker)176 def testWriteMarkInInfo(self):177 loggable = log.Loggable()178 log.setDebug("3")179 log.addLogHandler(self.handler)180 marker = 'test'181 loggable.writeMarker(marker, log.INFO)182 self.assertEquals(self.message, marker)183 def testWriteMarkInLog(self):184 loggable = log.Loggable()185 log.setDebug("5")186 log.addLogHandler(self.handler)187 marker = 'test'188 loggable.writeMarker(marker, log.LOG)189 self.assertEquals(self.message, marker)190 def testWriteMarkInError(self):191 loggable = log.Loggable()192 log.setDebug("4")193 log.addLogHandler(self.handler)194 marker = 'test'195 self.assertRaises(SystemExit, loggable.writeMarker, marker, log.ERROR)196 self.assertEquals(self.message, marker)197class TestLogNames(unittest.TestCase):198 def testGetLevelNames(self):199 self.assertEquals(['ERROR', 'WARN', 'INFO', 'DEBUG', 'LOG'],200 log.getLevelNames())201 def testGetLevelCode(self):202 self.assertEquals(1, log.getLevelInt('ERROR'))203 self.assertEquals(2, log.getLevelInt('WARN'))204 self.assertEquals(3, log.getLevelInt('INFO'))205 self.assertEquals(4, log.getLevelInt('DEBUG'))206 self.assertEquals(5, log.getLevelInt('LOG'))207 def testGetLevelName(self):208 self.assertEquals('ERROR', log.getLevelName(1))209 self.assertEquals('WARN', log.getLevelName(2))210 self.assertEquals('INFO', log.getLevelName(3))211 self.assertEquals('DEBUG', log.getLevelName(4))212 self.assertEquals('LOG', log.getLevelName(5))213if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!