def test_disable_option(self): parser = OptionParser() c = LogCapture() c.addOptions(parser) options, args = parser.parse_args( ['test_can_be_disabled_long', '--nologcapture']) c.configure(options, Config()) assert not c.enabled env = {'NOSE_NOLOGCAPTURE': 1} c = LogCapture() parser = OptionParser() c.addOptions(parser, env) options, args = parser.parse_args(['test_can_be_disabled']) c.configure(options, Config()) assert not c.enabled
def test_warning_when_prediction_method_and_no_probabilities(): """ Test compute_eval_from_predictions logs a warning if a prediction method is provided but the predictions file doesn't contain probabilities. """ lc = LogCapture() lc.begin() pred_path = join(_my_dir, 'other', 'test_compute_eval_from_predictions_predictions.tsv') input_path = join(_my_dir, 'other', 'test_compute_eval_from_predictions.jsonlines') # we need to capture stdout since that's what main() writes to compute_eval_from_predictions_cmd = [input_path, pred_path, 'pearson', 'unweighted_kappa', '--method', 'highest'] try: old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = mystdout = StringIO() sys.stderr = mystderr = StringIO() cefp.main(compute_eval_from_predictions_cmd) score_rows = mystdout.getvalue().strip().split('\n') err = mystderr.getvalue() print(err) finally: sys.stdout = old_stdout sys.stderr = old_stderr log_msg = ("skll.utilities.compute_eval_from_predictions: WARNING: A prediction " "method was provided, but the predictions file doesn't contain " "probabilities. Ignoring prediction method 'highest'.") eq_(lc.handler.buffer[-1], log_msg)
def test_unicode_messages_handled(self): msg = u'Ivan Krsti\u0107' c = LogCapture() parser = OptionParser() c.addOptions(parser, {}) options, args = parser.parse_args([]) c.configure(options, Config()) c.start() log = logging.getLogger("foobar.something") log.debug(msg) log.debug("ordinary string log") c.end() class Dummy: pass test = Dummy() try: raise Exception(msg) except: err = sys.exc_info() (ec, ev, tb) = c.formatError(test, err) print ev if UNICODE_STRINGS: assert msg in ev else: assert msg.encode('utf-8') in ev
def test_clears_all_existing_log_handlers(self): c = LogCapture() parser = OptionParser() c.addOptions(parser, {}) options, args = parser.parse_args(['--logging-clear-handlers']) c.configure(options, Config()) eq_(c.clear, True) def mktest(): class TC(unittest.TestCase): def runTest(self): pass test = TC() return test logging.getLogger().addHandler(StreamHandler(sys.stdout)) log = logging.getLogger("dummy") log.addHandler(StreamHandler(sys.stdout)) c.start() c.beforeTest(mktest()) c.end() if py27: expect = ["<class 'nose.plugins.logcapture.MyMemoryHandler'>"] else: expect = ['nose.plugins.logcapture.MyMemoryHandler'] eq_([str(c.__class__) for c in logging.getLogger().handlers], expect) eq_([str(c.__class__) for c in logging.getLogger("dummy").handlers], [])
def setUpClass(cls): cls.maxDiff = None cls.tempdir = tempfile.mkdtemp() # Log capturing cls.log = LogCapture() cls.log.logformat = '%(levelname)s: %(message)s' cls.log.begin()
def setUp(self): self.data_dir = TestDataLocator.get_data_dir() + os.sep self.maxDiff = None # Log capturing self.log = LogCapture() self.log.logformat = '%(levelname)s: %(message)s' self.log.begin()
def test_logging_format_option(self): env = {'NOSE_LOGFORMAT': '++%(message)s++'} c = LogCapture() parser = OptionParser() c.addOptions(parser, env) options, args = parser.parse_args(['logging_format']) c.configure(options, Config()) eq_('++%(message)s++', c.logformat)
def test_logging_datefmt_option(self): env = {'NOSE_LOGDATEFMT': '%H:%M:%S'} c = LogCapture() parser = OptionParser() c.addOptions(parser, env) options, args = parser.parse_args(['logging_datefmt']) c.configure(options, Config()) eq_('%H:%M:%S', c.logdatefmt)
def test_custom_formatter(self): c = LogCapture() c.logformat = '++%(message)s++' c.start() log = logging.getLogger("foobar.something") log.debug("Hello") c.end() records = c.formatLogRecords() eq_(1, len(records)) eq_("++Hello++", records[0])
class NoseSeleniumBase(PluginTester, TestCase): activate = '--with-nose-selenium' # enables the plugin plugins = [NoseSelenium(), LogCapture()] def makeSuite(self): class TC(TestCase): def runTest(self): raise ValueError("We should never get here") return TestSuite([TC()])
def test_get_logger(self): lc = LogCapture() lc.begin() logger = logging.getLogger('giftwrap') logger.setLevel(logging.INFO) logger.debug('test-debug') logger.info('test-info') lc.end() self.assertEqual("giftwrap: INFO: test-info", lc.handler.buffer[0])
def test_default_options(self): c = LogCapture() parser = OptionParser() c.addOptions(parser) options, args = parser.parse_args(['default_options']) c.configure(options, Config()) assert c.enabled eq_(LogCapture.logformat, c.logformat) eq_(LogCapture.clear, c.clear) eq_(LogCapture.filters, c.filters)
def test_captures_logging(self): c = LogCapture() parser = OptionParser() c.addOptions(parser, {}) options, args = parser.parse_args([]) c.configure(options, Config()) c.start() log = logging.getLogger("foobar.something") log.debug("Hello") c.end() eq_(1, len(c.handler.buffer)) eq_("Hello", c.handler.buffer[0].msg)
def setUp(self): self.data_dir = TestDataLocator.get_data_dir() self.maxDiff = None # Log capturing self.log = LogCapture() self.log.logformat = '%(levelname)s: %(message)s' self.log.begin() fname = os.path.join(self.data_dir, "paper_has_author.orm") model = NormaLoader(fname).model self.paper_has_author = ORMMinusModel(model=model) self.solution1 = self.paper_has_author.solution
def test_loglevel(self): c = LogCapture() parser = OptionParser() c.addOptions(parser, {}) options, args = parser.parse_args(['--logging-level', 'INFO']) c.configure(options, Config()) c.start() log = logging.getLogger("loglevel") log.debug("Hello") log.info("Goodbye") c.end() records = c.formatLogRecords() eq_(1, len(c.handler.buffer)) eq_("Goodbye", c.handler.buffer[0].msg) eq_("loglevel: INFO: Goodbye", records[0])
def test_consistent_mutables(self): c = LogCapture() parser = OptionParser() c.addOptions(parser) c.start() log = logging.getLogger("mutable") mutable = { 'value': 1 } log.debug("%r", mutable) repr_1 = repr(mutable) mutable['value'] = 2 log.debug("%r", mutable) repr_2 = repr(mutable) c.end() records = c.formatLogRecords() eq_("mutable: DEBUG: %s" % (repr_1,), records[0]) eq_("mutable: DEBUG: %s" % (repr_2,), records[1])
def test_logging_filter_exclude_and_include(self): env = {'NOSE_LOGFILTER': 'foo,-foo.bar'} c = LogCapture() parser = OptionParser() c.addOptions(parser, env) options, args = parser.parse_args(['foo']) print options, args c.configure(options, Config()) c.start() for name in ['foo.yes', 'foo.bar', 'foo.bar.no', 'blah']: log = logging.getLogger(name) log.info("Hello %s" % name) c.end() records = c.formatLogRecords() eq_(1, len(records)) assert records[0].startswith('foo.yes:'), records[0]
def test_logging_filter_exclude(self): env = {'NOSE_LOGFILTER': '-foo,-bar'} c = LogCapture() parser = OptionParser() c.addOptions(parser, env) options, args = parser.parse_args(['foo']) print options, args c.configure(options, Config()) c.start() for name in ['foobar.something', 'foo', 'foo.x', 'abara', 'bar.quux']: log = logging.getLogger(name) log.info("Hello %s" % name) c.end() records = c.formatLogRecords() eq_(2, len(records)) assert records[0].startswith('foobar.something:'), records[0] assert records[1].startswith('abara:'), records[1]
def test_non_propagating_loggers_handled(self): c = LogCapture() parser = OptionParser() c.addOptions(parser, {}) options, args = parser.parse_args([]) c.configure(options, Config()) logger = logging.getLogger('foo.yes') logger.propagate = False c.start() logger.debug("test message") c.end() records = c.formatLogRecords() eq_(1, len(records)) assert records[0].startswith('foo.yes:'), records[0]
def test_builtin_logging_filtering(self): c = LogCapture() c.logformat = '++%(message)s++' c.start() log = logging.getLogger("foobar.something") filtered = [] class filter(object): def filter(record): filtered.append(record) return len(filtered) == 1 filter = staticmethod(filter) c.handler.addFilter(filter) log.debug("Hello") log.debug("World") c.end() eq_(2, len(filtered)) records = c.formatLogRecords() eq_(1, len(records)) eq_("++Hello++", records[0])
def test(ctx, paths='', failfast=False, verbose=False, skip_coverage=False): """ Runs the unit tests Usage: inv dev.test --paths='api' --failfast """ import unittest import nose from coverage import Coverage from nose.plugins.capture import Capture from nose.plugins.logcapture import LogCapture from config import set_config config = set_config('testing') from tests import prepare_database as tests_prepare_database class ConfiguringPlugin(nose.plugins.Plugin): enabled = True def configure(self, options, conf): pass def begin(self): tests_prepare_database() runner = unittest.TextTestRunner(verbosity=2 if verbose else 1) argv = ['nosetests'] if failfast: argv.append('--stop') for path in paths.split(','): prefix = 'tests.' if not path: prefix = prefix[:-1] argv.append(prefix + path) plugins = [ConfiguringPlugin()] if config.nose.log_capturing: argv += [ '--logging-clear-handlers', '--logging-format=(%(thread)d) %(name)s: %(levelname)s: %(message)s' ] plugins.append(LogCapture()) if config.nose.stdout_capturing: plugins.append(Capture()) os.chdir(os.path.join(config.project_dir, os.path.pardir)) if not skip_coverage: cov = Coverage( source=[config.project_dir], omit=[ 'src/admin/*', 'src/celery_tasks/*', 'src/db/*', 'src/tasks/*', 'src/tests/*', ], ) cov.start() nose.main( argv=argv, testRunner=runner, plugins=plugins, exit=False, ) if not skip_coverage: directory = os.path.join(config.project_dir, '.coverage_report') print(f'\nSaving coverage report to "{os.path.abspath(directory)}"\n') cov.stop() cov.save() cov.html_report(directory=directory, title='WSP Coverage Report')
def test_enabled_by_default(self): c = LogCapture() assert c.enabled