ward.hooks

Plugin API

This section contains items from this module that are intended for use by plugin authors or those contributing to Ward itself. If you’re just using Ward to write your tests, this section isn’t relevant.

class ward.hooks.SessionHooks
after_session(config: ward.config.Config, test_results: List[ward.testing.TestResult], status_code: ward.models.ExitCode)Optional[rich.console.ConsoleRenderable]

Hook that runs right before a test session ends (just before the result summary is printed to the terminal).

This hook has no default implementation. If you implement it, you will not be overriding any existing functionality.

Examples of how you could use this hook:

  • Writing additional information to the terminal after the test session finishes.

  • Writing a file (e.g. an HTML report) to disk containing the results from the test session.

  • Sending a file containing the results off somewhere for storage/processing.

If you return a rich.console.ConsoleRenderable from this function, it will be rendered to the terminal.

before_session(config: ward.config.Config)Optional[rich.console.ConsoleRenderable]

Hook which is called immediately at the start of a test session (before tests are collected).

You can implement this hook to run some setup code.

This hook has no default implementation. If you implement it, you will not be overriding any existing functionality.

Examples of how you could use this hook:

  • Printing some information to the terminal about your plugin.

  • Creating a file on disk which you’ll write to in other hooks.

If you return a `rich.console.ConsoleRenderable<https://rich.readthedocs.io/en/latest/protocol.html#console-render>`_ from this function, it will be rendered to the terminal.

preprocess_tests(config: ward.config.Config, collected_tests: List[ward.testing.Test])

Called before tests are filtered out by --search, --tags, etc, and before assertion rewriting.

Allows you to transform or filter out tests (by modifying the collected_tests list in place).

This hook has no default implementation. You can implement it without overriding existing functionality.

Examples of how you could use this hook:

  • Filter out tests

  • Reorder tests

  • Generate statistics about tests pre-run

  • Attach some data to each test to use later