How to use remove_all_extra_handlers method in Slash

Best Python code snippet using slash

test_logging.py

Source:test_logging.py Github

copy

Full Screen

1# pylint: disable=unused-argument,redefined-outer-name2import functools3import os4import logbook5import gossip6import pytest7import slash8from .utils import run_tests_assert_success, run_tests_in_session, TestCase9def test_console_format(suite, suite_test, config_override, tmpdir):10 config_override('log.format', 'file: {record.message}')11 config_override('log.console_format', 'console: {record.message}')12 config_override('log.root', str(tmpdir))13 suite_test.append_line('slash.logger.error("message here")')14 summary = suite.run(additional_args=['-vvv'])15 assert 'console: message here' in summary.get_console_output()16 [result] = summary.get_all_results_for_test(suite_test)17 with open(result.get_log_path()) as f:18 assert 'file: message here' in f.read()19def test_last_session_symlinks(files_dir, links_dir, session):20 test_log_file = files_dir.join(21 session.id, list(session.results.iter_test_results())[-1].test_metadata.id, "debug.log")22 assert test_log_file.check()23 session_log_file = files_dir.join(session.id, "session.log")24 assert session_log_file.check()25 assert links_dir.join("last-session").readlink() == session_log_file26 assert links_dir.join("last-session-dir").readlink() == session_log_file.dirname27 assert links_dir.join("last-test").readlink() == test_log_file28def test_global_result_get_log_path(files_dir, suite):29 summary = suite.run()30 assert summary.session.results.global_result.get_log_path() is not None31 assert summary.session.results.global_result.get_log_path().startswith(str(files_dir))32def test_log_file_colorize(files_dir, config_override, suite, suite_test):33 config_override('log.colorize', True)34 suite_test.append_line('slash.logger.notice("hey")')35 summary = suite.run()36 logfiles = [37 summary.session.results.global_result.get_log_path(),38 summary.get_all_results_for_test(suite_test)[0].get_log_path(),39 ]40 for logfile in logfiles:41 with open(logfile, 'rb') as f:42 log_data = f.read()43 assert b'\x1b[' in log_data44@pytest.mark.parametrize('level', ['info', 'notice', 'warning'])45def test_console_truncation_does_not_truncate_files(files_dir, suite, suite_test, config_override, level):46 assert slash.config.root.log.truncate_console_lines47 long_string = 'a' * 100048 suite_test.append_line('slash.logger.{level}({msg!r})'.format(msg=long_string, level=level))49 summary = suite.run()50 [result] = summary.get_all_results_for_test(suite_test)51 with open(result.get_log_path()) as logfile:52 logfile_data = logfile.read()53 assert long_string in logfile_data54@pytest.mark.parametrize('symlink_name', ['last_session_symlink', 'last_session_dir_symlink', 'last_failed_symlink'])55def test_log_symlinks_without_root_path(suite, config_override, symlink_name):56 config_override('log.{0}'.format(symlink_name), 'some/subdir')57 assert suite.run().ok()58def test_last_test_not_overriden_by_stop_on_error(links_dir, suite):59 failed_test = suite[4]60 failed_test.when_run.fail()61 # we stop on error...62 for test in suite[5:]:63 test.expect_not_run()64 summary = suite.run(additional_args=['-x'])65 [failed_result] = summary.get_all_results_for_test(failed_test)66 for link_name in ('last-test', 'last-failed'):67 assert links_dir.join(link_name).readlink() == failed_result.get_log_path()68def test_last_test_delete_log_file(links_dir, suite, suite_test):69 os.makedirs(str(links_dir))70 temp_file = os.path.abspath(str(links_dir.join('somepath')))71 with open(temp_file, 'w'):72 pass73 os.symlink(temp_file, str(links_dir.join('last-test')))74 os.unlink(temp_file)75 assert not os.path.exists(str(links_dir.join('last-test')))76 assert os.path.islink(str(links_dir.join('last-test')))77 summary = suite.run()78 with open(summary.session.results.global_result.get_log_path()) as f:79 assert 'OSError: ' not in f.read()80 # assert links_dir.join('last-test').readlink() == list(summary.session.results)[-1].get_log_path()81def test_result_log_links(files_dir, session):82 for result in session.results.iter_test_results():83 assert result.get_log_path() is not None84 assert result.get_log_path().startswith(str(files_dir))85def test_last_failed(suite, links_dir):86 suite[-5].when_run.fail()87 last_failed = suite[-2]88 last_failed.when_run.fail()89 summary = suite.run()90 [result] = summary.get_all_results_for_test(last_failed)91 fail_log = result.get_log_path()92 assert os.path.isfile(fail_log)93 assert links_dir.join('last-failed').readlink() == fail_log94def test_errors_log_for_test(suite, suite_test, errors_log_path, logs_dir):95 suite_test.when_run.fail()96 res = suite.run()[suite_test]97 with errors_log_path.open() as f:98 lines = [l for l in f.read().splitlines() if 'NOTICE' not in l]99 error_line = lines[0]100 assert 'Error added' in error_line101 with open(res.get_log_path()) as f:102 lines = f.read().splitlines()103 assert error_line in lines104@pytest.mark.parametrize('should_keep_failed_tests', [True, False])105def test_logs_deletion(suite, suite_test, errors_log_path, logs_dir, config_override, should_keep_failed_tests):106 config_override('log.cleanup.enabled', True)107 config_override('log.cleanup.keep_failed', should_keep_failed_tests)108 expected_remaining_files = [errors_log_path]109 suite_test.when_run.fail()110 summary = suite.run()111 remaining_files = []112 for dirname, _, files in os.walk(logs_dir):113 for filename in files:114 f = os.path.join(dirname, filename)115 if os.path.isfile(f) and not os.path.islink(f):116 remaining_files.append(f)117 if should_keep_failed_tests:118 assert len(remaining_files) == 3119 expected_remaining_files.extend([summary[suite_test].get_log_path(), summary.session.results.global_result.get_log_path()])120 else:121 assert len(remaining_files) == 1122 assert set(remaining_files) == set(expected_remaining_files)123def test_errors_log_for_session(suite, errors_log_path, request, logs_dir):124 @gossip.register('slash.session_start')125 def on_session_start():126 try:127 1/0 # pylint: disable=pointless-statement128 except ZeroDivisionError:129 slash.add_error()130 request.addfinalizer(on_session_start.gossip.unregister)131 results = suite.run(expect_session_errors=True).session.results132 assert len(results.global_result.get_errors()) == 1133 with errors_log_path.open() as f:134 lines = [l for l in f.read().splitlines() if 'NOTICE' not in l]135 assert 'Error added' in lines[0]136 with open(results.global_result.get_log_path()) as f:137 assert lines[0] in f.read().splitlines()138################################################################################139## Fixtures140@pytest.fixture141def session():142 session = run_tests_assert_success(SampleTest)143 return session144@pytest.fixture145def errors_log_path(request, config_override, tmpdir, logs_dir):146 subpath = 'subdir/errors.log'147 config_override('log.highlights_subpath', subpath)148 return logs_dir.join('files').join(subpath)149@pytest.fixture150def links_dir(logs_dir):151 return logs_dir.join("links")152@pytest.fixture153def files_dir(logs_dir):154 return logs_dir.join("files")155_TOKEN = "logging-test"156_SESSION_START_MARK = "session-start-mark"157_SESSION_END_MARK = "session-end-mark"158_silenced_logger = logbook.Logger("silenced_logger")159################################################################################160## Legacy Tests161class LogFormattingTest(TestCase):162 def setUp(self):163 super(LogFormattingTest, self).setUp()164 self.log_path = self.get_new_path()165 self.override_config(166 "log.root", self.log_path167 )168 self.override_config(169 "log.format", "-- {record.message} --"170 )171 self.override_config("log.subpath", "debug.log")172 def test(self):173 self.session = run_tests_assert_success(SampleTest)174 with open(os.path.join(self.log_path, "debug.log")) as logfile:175 for line in logfile:176 self.assertTrue(line.startswith("-- "))177 self.assertTrue(line.endswith(" --\n"))178class LoggingTest(TestCase):179 def test(self):180 self.log_path = self.get_new_path()181 self.override_config(182 "log.root",183 self.log_path,184 )185 self.override_config(186 "log.subpath",187 os.path.join("{context.session.id}",188 "{context.test.__slash__.test_index0:03}-{context.test_id}", "debug.log")189 )190 self.override_config(191 "log.session_subpath",192 os.path.join("{context.session.id}", "debug.log")193 )194 self.override_config(195 "log.silence_loggers",196 [_silenced_logger.name]197 )198 self.addCleanup(gossip.unregister_token, _TOKEN)199 slash.hooks.session_start.register( # pylint: disable=no-member200 functools.partial(_mark, _SESSION_START_MARK), token=_TOKEN)201 slash.hooks.session_end.register( # pylint: disable=no-member202 functools.partial(_mark, _SESSION_END_MARK), token=_TOKEN)203 self.addCleanup(gossip.unregister_token, _TOKEN)204 self.session = run_tests_assert_success(SampleTest)205 self.tests_metadata = [206 result.test_metadata for result in self.session.results.iter_test_results()]207 self._test_all_run()208 self._test_test_logs_written()209 self._test_session_logs()210 self._test_no_silenced_logger_records()211 def _test_all_run(self):212 methods = [213 method_name for method_name in dir(SampleTest)214 if method_name.startswith("test")215 ]216 self.assertTrue(methods)217 self.assertEqual(len(self.tests_metadata), len(methods))218 def _test_test_logs_written(self):219 for test_metadata in self.tests_metadata:220 test_dir = "{0:03}-{1}".format(test_metadata.test_index0, test_metadata.id)221 log_path = os.path.join(222 self.log_path, self.session.id, test_dir, "debug.log")223 with open(log_path) as f:224 data = f.read()225 for other_test in self.tests_metadata:226 if other_test.id != test_metadata.id:227 self.assertNotIn(other_test.id, data)228 self.assertNotIn(_SESSION_START_MARK, data)229 self.assertNotIn(_SESSION_END_MARK, data)230 def _test_session_logs(self):231 with open(os.path.join(self.log_path, self.session.id, "debug.log")) as f:232 data = f.read()233 self.assertIn(_SESSION_START_MARK, data)234 self.assertIn(_SESSION_END_MARK, data)235 for test_id in (t.id for t in self.tests_metadata):236 self.assertNotIn(test_id, data)237 def _test_no_silenced_logger_records(self):238 for path, _, filenames in os.walk(self.log_path):239 for filename in filenames:240 assert filename.endswith(".log")241 filename = os.path.join(path, filename)242 with open(filename) as f:243 assert _silenced_logger.name not in f.read(244 ), "Silenced logs appear in log file {0}".format(filename)245class ExtraLoggersTest(TestCase):246 def setUp(self):247 super(ExtraLoggersTest, self).setUp()248 self.session = slash.Session()249 self.handler = logbook.TestHandler()250 self.addCleanup(slash.log.remove_all_extra_handlers)251 slash.log.add_log_handler(self.handler)252 def test(self):253 with self.session:254 run_tests_in_session(SampleTest, session=self.session)255 for test_result in self.session.results.iter_test_results():256 for record in self.handler.records:257 if test_result.test_id in record.message:258 break259 else:260 self.fail(261 "Test id {} does not appear in logger".format(test_result.test_id))262class SampleTest(slash.Test):263 def test_1(self):264 _mark()265 def test_2(self):266 _silenced_logger.error("error")267 _silenced_logger.info("info")268 _silenced_logger.debug("debug")269 _mark()270def _mark(text=None):271 if text is None:272 text = slash.context.test_id273 slash.logger.debug(text)274class TestLocaltimeLogging(TestCase):275 def setUp(self):276 super(TestLocaltimeLogging, self).setUp()277 self.assertFalse(slash.config.root.log.localtime)278 self.path = self.get_new_path()279 self.override_config(280 "log.localtime", True)281 self.override_config(282 "log.root", self.path)283 def test_local_time(self):284 with slash.Session() as session: # pylint: disable=unused-variable285 slash.logger.info("Hello")...

Full Screen

Full Screen

log.py

Source:log.py Github

copy

Full Screen

...230 """231 Adds a log handler to be entered for sessions and for tests232 """233 _extra_handlers.append(handler)234def remove_all_extra_handlers():235 del _extra_handlers[:]236_extra_handlers = []237class VERBOSITIES(object):238 DEBUG = logbook.DEBUG239 INFO = logbook.INFO240 NOTICE = logbook.NOTICE241 WARNING = logbook.WARNING242 ERROR = logbook.ERROR243 CRITICAL = logbook.CRITICAL244def _make_bubbling_handler(handler):245 return _BubblingWrapper(handler)246class _BubblingWrapper(logbook.Handler):247 def __init__(self, handler):248 super(_BubblingWrapper, self).__init__(bubble=True)...

Full Screen

Full Screen

conftest.py

Source:conftest.py Github

copy

Full Screen

1# pylint: disable=redefined-outer-name2import itertools3import os4import random5import tempfile6from uuid import uuid47from forge import Forge8import gossip9import pytest10import slash11import slash.plugins12import logbook13from slash.loader import Loader14from slash.core.result import GlobalResult, Result15from vintage import get_no_deprecations_context16from .utils.cartesian import Cartesian17from .utils.suite_writer import Suite18from .utils.suite_builder import SuiteBuilder19from .utils.garbage_collection import GarbageCollectionMarker20@pytest.fixture(scope='session', autouse=True)21def unittest_mode_logging():22 slash.config.root.log.unittest_mode = True23@pytest.fixture(scope='session', autouse=True)24def random_seed():25 random.seed(0xdeadface)26@pytest.fixture(scope='session', autouse=True)27def no_user_config(request):28 tmpdir = tempfile.mkdtemp()29 slash.conf.config.root.run.user_customization_file_path = os.path.join(30 tmpdir, 'slashrc')31 @request.addfinalizer32 def cleanup(): # pylint: disable=unused-variable33 os.rmdir(tmpdir)34@pytest.fixture(autouse=True)35def logbook_console_handler(request):36 handler = logbook.StderrHandler(level=logbook.TRACE)37 handler.push_application()38 request.addfinalizer(handler.pop_application)39@pytest.fixture40def no_plugins(request):41 slash.plugins.manager.uninstall_all()42 @request.addfinalizer43 def cleanup(): # pylint: disable=unused-variable44 slash.plugins.manager.uninstall_all()45 slash.plugins.manager.install_builtin_plugins()46@pytest.fixture47def forge(request):48 returned = Forge()49 @request.addfinalizer50 def cleanup(): # pylint: disable=unused-variable51 returned.verify()52 returned.restore_all_replacements()53 return returned54@pytest.fixture(autouse=True)55def backup_config(request):56 slash.config.backup()57 request.addfinalizer(slash.config.restore)58 return slash.config59@pytest.fixture60def config_override():61 def _override(path, value):62 slash.config.assign_path(path, value)63 return _override64@pytest.fixture65def cartesian():66 return Cartesian()67@pytest.fixture(scope="function", autouse=True)68def cleanup_hook_registrations(request):69 @request.addfinalizer70 def _cleanup():71 for hook in gossip.get_group("slash").get_hooks():72 hook.unregister_all()73 assert not gossip.get_group("slash").get_subgroups()74@pytest.fixture75def checkpoint():76 return Checkpoint()77@pytest.fixture78def checkpoint1():79 return Checkpoint()80@pytest.fixture81def checkpoint2():82 return Checkpoint()83_timestamp = itertools.count(1000000)84class Checkpoint(object):85 called_count = 086 args = kwargs = timestamp = None87 def __call__(self, *args, **kwargs):88 self.called_count += 189 self.args = args90 self.kwargs = kwargs91 self.timestamp = next(_timestamp)92 @property93 def called(self):94 return self.called_count > 095@pytest.fixture96def suite_test(suite, test_type, is_last_test):97 returned = suite.add_test(type=test_type)98 if not is_last_test:99 _ = suite.add_test(type=test_type)100 return returned101@pytest.fixture102def last_suite_test(suite, test_type):103 return suite.add_test(type=test_type)104@pytest.fixture(params=[GlobalResult, Result])105def result(request):106 return request.param()107@pytest.fixture(params=[True, False])108def is_last_test(request):109 return request.param110@pytest.fixture(params=['method', 'function'])111def test_type(request):112 return request.param113@pytest.fixture114def suite():115 returned = Suite()116 returned.populate()117 return returned118@pytest.fixture119def suite_builder(tmpdir):120 return SuiteBuilder(str(tmpdir.join('suite_builder')))121@pytest.fixture122def parallel_suite_test(parallel_suite, test_type, is_last_test):123 returned = parallel_suite.add_test(type=test_type)124 if not is_last_test:125 _ = parallel_suite.add_test(type=test_type)126 return returned127@pytest.fixture128def parallel_suite():129 returned = Suite(debug_info=False, is_parallel=True)130 returned.populate()131 return returned132@pytest.fixture133def runnable_test_dir(tmpdir):134 tests_dir = tmpdir.join(str(uuid4()))135 filename = str(uuid4()).replace('-', '') + '.py'136 with tests_dir.join(filename).open('w', ensure=True) as f:137 f.write('def test_something():\n pass')138 return tests_dir139@pytest.fixture140def slash_session():141 return slash.Session()142@pytest.fixture143def test_loader():144 return Loader()145@pytest.fixture146def active_slash_session(request):147 returned = slash.Session()148 returned.__enter__()149 @request.addfinalizer150 def finalize(): # pylint: disable=unused-variable151 returned.__exit__(None, None, None)152 return returned153@pytest.fixture(params=["slashconf", "module"])154def defined_fixture(request, suite, suite_test):155 if request.param == 'slashconf':156 return suite.slashconf.add_fixture()157 elif request.param == 'module':158 return suite_test.file.add_fixture()159 raise NotImplementedError() # pragma: no cover160@pytest.fixture161def gc_marker():162 return GarbageCollectionMarker()163@pytest.fixture(autouse=True, scope="function")164def reset_gossip(request):165 @request.addfinalizer166 def cleanup(): # pylint: disable=unused-variable167 for group in list(gossip.get_groups()):168 if group.name == 'slash':169 continue170 group.undefine()171 for hook in gossip.get_all_hooks():172 if hook.group.name != 'slash':173 hook.undefine()174 else:175 hook.unregister_all()176@pytest.fixture # pylint: disable=unused-argument177def plugin(no_plugins): # pylint: disable=unused-argument178 class StartSessionPlugin(slash.plugins.PluginInterface):179 _activate_called = False180 _deactivate_called = False181 def __init__(self):182 super(StartSessionPlugin, self).__init__()183 self.session_start_call_count = 0184 def get_name(self):185 return "start session"186 def session_start(self):187 self.session_start_call_count += 1188 def activate(self):189 self._activate_called = True190 def deactivate(self):191 self._deactivate_called = True192 return StartSessionPlugin()193@pytest.fixture(params=['slashconf', 'file'])194def get_fixture_location(request):195 def getter(test):196 if request.param == 'slashconf':197 return test.suite.slashconf198 elif request.param == 'file':199 return test.file200 else:201 raise NotImplementedError() # pragma: no cover202 return getter203@pytest.fixture204def restore_plugins_on_cleanup(request):205 request.addfinalizer(slash.plugins.manager.install_builtin_plugins)206 request.addfinalizer(slash.plugins.manager.uninstall_all)207@pytest.fixture208def logs_dir(config_override, tmpdir, relative_symlinks):209 returned = tmpdir.join('logs')210 config_override("log.root", str(returned.join("files")))211 config_override("log.last_session_symlink",212 str("../links/last-session" if relative_symlinks else returned.join("links", "last-session")))213 config_override("log.last_session_dir_symlink",214 str("../links/last-session-dir" if relative_symlinks else returned.join("links", "last-session-dir")))215 config_override("log.last_test_symlink",216 str("../links/last-test" if relative_symlinks else returned.join("links", "last-test")))217 config_override("log.last_failed_symlink",218 str("../links/last-failed" if relative_symlinks else returned.join("links", "last-failed")))219 return returned220@pytest.fixture(params=[True, False])221def relative_symlinks(request):222 return request.param223@pytest.fixture224def session_log(logs_dir):225 return logs_dir.join('links').join('last-session')226@pytest.fixture227def unique_string1():228 return str(uuid4())229@pytest.fixture(params=[True, False])230def yield_fixture_decorator(request):231 should_use_explicitly = request.param232 if should_use_explicitly:233 return slash.yield_fixture234 return slash.fixture235@pytest.fixture236def disable_vintage_deprecations():237 with get_no_deprecations_context():238 yield239@pytest.fixture # pylint: disable=unused-argument240def xunit_filename(tmpdir, request, config_override): # pylint: disable=unused-argument241 xunit_filename = str(tmpdir.join('xunit.xml'))242 slash.plugins.manager.activate('xunit')243 slash.config.root.plugin_config.xunit.filename = xunit_filename244 @request.addfinalizer245 def deactivate(): # pylint: disable=unused-variable246 slash.plugins.manager.deactivate('xunit')247 return xunit_filename248@pytest.fixture249def test_handler(request):250 handler = logbook.TestHandler()251 request.addfinalizer(slash.log.remove_all_extra_handlers)252 slash.log.add_log_handler(handler)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful