Jump to >

This documentation covers the in-development release of Djblets. You can see the latest stable docs or all previous versions.

djblets.extensions.testing.testcases

Mixins for test cases that need to test enabled extensions.

class DefaultTestsExtensionManager(key)[source]

Default extension manager for unit tests.

New in version 2.3.

class ExtensionTestCaseMixin[source]

Unit tests mixin for more easily testing extensions.

This will do the hard work of creating the fake registration information needed for an extension and to instantiate an instance for testing.

Subclasses need to define extension_class and may want to implement get_extension_manager() (by default, the first registered extension manager will be used).

Projects may want to provide their own subclass for their extensions to use that implements get_extension_manager(), so extensions won’t have to.

Changed in version 2.3: Extensions and extension managers are now cleaned up and removed after test runs.

Subclasses can more easily set up default extension state, or create new extensions as needed.

extension_mgr

The extension manager owning the extension. Tests can use this to manually enable/disable the extension, if needed.

Type

djblets.extensions.manager.ExtensionManager

extension

The extension instance being tested.

Type

djblets.extensions.extension.Extension

extension_class = None[source]

The extension class to test.

extension_metadata = {'Version': '1.0'}[source]

Optional metadata to use for the extension information.

extension_package_name = 'TestPackage'[source]

Optional package name to use for the extension information.

extension_enable_by_default = True[source]

Whether to enable the extension by default.

New in version 2.3.

Type:

bool

default_extension_manager_cls[source]

The default extension manager to use if one is not instantiated.

This will be created only if the consuming project hasn’t already instantiated an extension manager.

New in version 2.3.

Type:

type

alias of djblets.extensions.testing.testcases.DefaultTestsExtensionManager

allow_existing_extension_manager = True[source]

Whether to allow use of an existing registered extension manager.

If disabled, a new extension manager will be created for the test run. This is enabled by default for backwards-compatibility.

New in version 2.3.

Type:

bool

reset_extensions()[source]

Unregister and clean up all extensions and extension managers.

This will disable and unregister any extensions that have been set up using setup_extension(). It will also shut down and unregister all extension managers created during the test run.

This is normally called when a unit test has finished. Tests can explicitly call this to reset the extension management state.

New in version 2.3.

scanned_extensions(extension_classes=[], extension_mgr=None)[source]

Make the specified extension classes available for scanning.

This should be called before calling ExtensionManager.load() in order to ensure the specified list of extension classes are available to be scanned.

New in version 2.3.

Parameters
  • extension_classes (list, optional) –

    The list of extensions to be made available.

    Each entry can be an extension class, or a tuple of (extension_class, package_name).

    If any extension is not registered, it will be assigned a default package name of extension_package_name.

  • extension_mgr (djblets.extensions.manager.ExtensionManager, optional) – An explicit extension manager that will be used to scan, rather than the test suite’s extension_mgr attribute.

Context

The provided extensions will be available for scanning.

setup_extension(extension_cls=None, package_name=None, metadata=None, enable=True, extension_mgr=None)[source]

Set up a new extension instance.

This will construct an new extension of the provided type with the provided information.

The extension will be registered in the extension manager, and cleaned up when the unit test has finished.

New in version 2.3.

Parameters
  • extension_cls (type, optional) –

    The extension’s class.

    Defaults to extension_class.

  • package_name (unicode, optional) –

    The package name assigned as the owner of the extension.

    Defaults to extension_package_name.

  • metadata (dict, optional) –

    The extension package metadata.

    Defaults to metadata.

  • enable (bool, optional) –

    Whether to enable the extension.

    If not enabled, an instance will still be returned, but it won’t be registered as enabled in the extension manager.

  • extension_mgr (djblets.extensions.manager.ExtensionManager, optional) –

    An explicit extension manager to register this extension in.

    Defaults to the result of get_extension_manager().

Returns

The extension instance, if enabling. Otherwise, None will be returned.

Return type

djblets.extensions.extension.Extension

get_extension_manager()[source]

Return the extension manager used for the tests.

Subclasses may want to override this to pick a specific extension manager, if the project uses more than one. The default behavior is to return the first registered extension manager, or create one if one is not available.

If self.allow_existing_extension_manager is False, this will always create a new extension manager.

Changed in version 2.3: This is now guaranteed to return an extension manager instance, and that instance is guaranteed to be cleaned up after the test has finished.

Returns

The extension manager used for tests.

Return type

djblets.extensions.manager.ExtensionManager