http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11627
Revision: 11627 Author: valhallasw Date: 2013-06-08 21:23:39 +0000 (Sat, 08 Jun 2013) Log Message: ----------- Improved reporting when ui_tests are run as part of setup.py test
Modified Paths: -------------- branches/rewrite/tests/ui_tests.py
Modified: branches/rewrite/tests/ui_tests.py =================================================================== --- branches/rewrite/tests/ui_tests.py 2013-06-08 21:00:06 UTC (rev 11626) +++ branches/rewrite/tests/ui_tests.py 2013-06-08 21:23:39 UTC (rev 11627) @@ -15,95 +15,92 @@ import StringIO import logging
-if __name__ != "__main__": - raise Exception('This test can only be run as single file due to heavy monkey patching') +if __name__ == "__main__": + import sys
-import sys + oldstderr = sys.stderr + oldstdout = sys.stdout + oldstdin = sys.stdin
-oldstderr = sys.stderr -oldstdout = sys.stdout -oldstdin = sys.stdin + newstdout = cStringIO.StringIO() + newstderr = cStringIO.StringIO() + newstdin = StringIO.StringIO()
-newstdout = cStringIO.StringIO() -newstderr = cStringIO.StringIO() -newstdin = StringIO.StringIO() + def patch(): + sys.stdout = newstdout + sys.stderr = newstderr + sys.stdin = newstdin
-def patch(): - sys.stdout = newstdout - sys.stderr = newstderr - sys.stdin = newstdin + def unpatch(): + sys.stdout = oldstdout + sys.stderr = oldstderr + sys.stdin = oldstdin
-def unpatch(): - sys.stdout = oldstdout - sys.stderr = oldstderr - sys.stdin = oldstdin + try: + patch() + import pywikibot + finally: + unpatch()
-try: - patch() - import pywikibot -finally: - unpatch() + from pywikibot.bot import DEBUG, VERBOSE, INFO, STDOUT, INPUT, WARNING, ERROR, CRITICAL
-from pywikibot.bot import DEBUG, VERBOSE, INFO, STDOUT, INPUT, WARNING, ERROR, CRITICAL + logger = logging.getLogger('pywiki') + loggingcontext = {'caller_name': "ui_tests", + 'caller_file': "ui_tests", + 'caller_line': 0, + 'newline': "\n"}
-logger = logging.getLogger('pywiki') -loggingcontext = {'caller_name': "ui_tests", - 'caller_file': "ui_tests", - 'caller_line': 0, - 'newline': "\n"} + class TestTerminalUI(unittest.TestCase): + def setUp(self): + patch() + newstdout.truncate(0) + newstderr.truncate(0) + newstdin.truncate(0)
-class TestTerminalUI(unittest.TestCase): - def setUp(self): - patch() - newstdout.truncate(0) - newstderr.truncate(0) - newstdin.truncate(0) + def tearDown(self): + unpatch()
- def tearDown(self): - unpatch() + def testOutputLevels_logging_debug(self): + logger.log(DEBUG, 'debug', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "")
- def testOutputLevels_logging_debug(self): - logger.log(DEBUG, 'debug', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "") - self.assertEqual(newstderr.getvalue(), "") + def testOutputLevels_logging_verbose(self): + logger.log(VERBOSE, 'verbose', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "")
- def testOutputLevels_logging_verbose(self): - logger.log(VERBOSE, 'verbose', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "") - self.assertEqual(newstderr.getvalue(), "") + def testOutputLevels_logging_info(self): + logger.log(INFO, 'info', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "info\n")
- def testOutputLevels_logging_info(self): - logger.log(INFO, 'info', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "") - self.assertEqual(newstderr.getvalue(), "info\n") + def testOutputLevels_logging_stdout(self): + logger.log(STDOUT, 'stdout', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "stdout\n") + self.assertEqual(newstderr.getvalue(), "")
- def testOutputLevels_logging_stdout(self): - logger.log(STDOUT, 'stdout', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "stdout\n") - self.assertEqual(newstderr.getvalue(), "") + def testOutputLevels_logging_input(self): + logger.log(INPUT, 'input', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "input\n")
- def testOutputLevels_logging_input(self): - logger.log(INPUT, 'input', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "") - self.assertEqual(newstderr.getvalue(), "input\n") + def testOutputLevels_logging_WARNING(self): + logger.log(WARNING, 'WARNING', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "WARNING: WARNING\n")
- def testOutputLevels_logging_WARNING(self): - logger.log(WARNING, 'WARNING', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "") - self.assertEqual(newstderr.getvalue(), "WARNING: WARNING\n") + def testOutputLevels_logging_ERROR(self): + logger.log(ERROR, 'ERROR', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "ERROR: ERROR\n")
- def testOutputLevels_logging_ERROR(self): - logger.log(ERROR, 'ERROR', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "") - self.assertEqual(newstderr.getvalue(), "ERROR: ERROR\n") + def testOutputLevels_logging_CRITICAL(self): + logger.log(CRITICAL, 'CRITICAL', extra=loggingcontext) + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "CRITICAL: CRITICAL\n")
- def testOutputLevels_logging_CRITICAL(self): - logger.log(CRITICAL, 'CRITICAL', extra=loggingcontext) - self.assertEqual(newstdout.getvalue(), "") - self.assertEqual(newstderr.getvalue(), "CRITICAL: CRITICAL\n") - - -if __name__ == '__main__': + try: try: unittest.main() @@ -112,3 +109,10 @@ finally: unpatch() pywikibot.stopme() + +else: + class TestTerminalUI(unittest.TestCase): + @unittest.skip("Terminal UI tests can only be run by directly running tests/ui_tests.py") + def testCannotBeRun(self): + pass +
pywikipedia-svn@lists.wikimedia.org