Hooks

Slash leverages the gossip library to implement hooks. Hooks are endpoints to which you can register callbacks to be called in specific points in a test session lifetime.

All built-in hooks are members of the slash gossip group. As a convenience, the hook objects are all kept as globals in the slash.hooks module.

The slash gossip group is set to be both strict (See Gossip strict registrations) and has exception policy set to RaiseDefer (See Gossip error handling).

Registering Hooks

Hooks can be registered through slash.hooks:

import slash

@slash.hooks.session_start.register
def handler():
    print("Session has started: ", slash.context.session)

Which is roughly equivalent to:

import gossip

@gossip.register("slash.session_start")
def handler():
      print("Session has started: ", slash.context.session)

Hook Errors

By default, exceptions propagate from hooks and on to the test, but first all hooks are attempted. In some cases though you may want to debug the exception close to its raising point. Setting debug.debug_hook_handlers to True will cause the debugger to be triggered as soon as the hook dispatcher encounteres the exception. This is done via gossip’s error handling mechanism.

Hooks and Plugins

Hooks are especially useful in conjunction with Plugins. By default, plugin method names correspond to hook names on which they are automatically registered upon activation.

See also

Plugins

Advanced Usage

You may want to further customize hook behavior in your project. Mose of these customizations are available through gossip.

Available Hooks

The following hooks are available from the slash.hooks module:

slash.hooks.after_session_end

Called right after session_end hook

slash.hooks.after_session_start

Second entry point for session start, useful for plugins relying on other plugins’ session_start routine

slash.hooks.app_quit

Called right before the app quits

slash.hooks.before_interactive_shell(namespace)

Called before starting interactive shell

slash.hooks.before_session_cleanup

Called right before session cleanup begins

slash.hooks.before_session_start

Entry point which is called before session_start, useful for configuring plugins and other global resources

slash.hooks.before_test_cleanups

Called right before a test cleanups are executed

slash.hooks.before_worker_start(worker_config)

Called in parallel execution mode, before the parent starts the child worker

slash.hooks.configure

Configuration hook that happens during commandline parsing, and before plugins are activated. It is a convenient point to override plugin activation settings

slash.hooks.entering_debugger(exc_info)

Called right before entering debugger

slash.hooks.error_added(error, result)

Called when an error is added to a result (either test result or global)

slash.hooks.exception_caught_after_debugger

Called whenever an exception is caught, and a debugger has already been run

slash.hooks.exception_caught_before_debugger

Called whenever an exception is caught, but a debugger hasn’t been entered yet

slash.hooks.fact_set(name, value)

Called when a fact is set for a test

slash.hooks.interruption_added(result, exception)

Called when an exception is encountered that triggers test or session interruption

slash.hooks.log_file_closed(path, result)

Called right after a log file was closed

slash.hooks.prepare_notification(message)

Called with a message object prior to it being sent via the notifications plugin (if enabled)

slash.hooks.result_summary

Called at the end of the execution, when printing results

slash.hooks.session_end

Called right before the session ends, regardless of the reason for termination

slash.hooks.session_interrupt

Called when the session is interrupted unexpectedly

slash.hooks.session_start

Called right after session starts

slash.hooks.test_avoided(reason)

Called when a test is skipped completely (not even started)

slash.hooks.test_distributed(test_logical_id, worker_session_id)

Called in parallel mode, after the parent sent a test to child)

slash.hooks.test_end

Called right before a test ends, regardless of the reason for termination

slash.hooks.test_error

Called on test error

slash.hooks.test_failure

Called on test failure

slash.hooks.test_interrupt

Called when a test is interrupted by a KeyboardInterrupt or other similar means

slash.hooks.test_skip(reason)

Called on test skip

slash.hooks.test_start

Called right after a test starts

slash.hooks.test_success

Called on test success

slash.hooks.tests_loaded(tests)

Called when Slash finishes loading a batch of tests for execution (not necessarily al tests)

slash.hooks.warning_added(warning)

Called when a warning is captured by Slash

slash.hooks.worker_connected(session_id)

Called on new worker startup