Factories¶
Test factories: they create objects for testing purposes.
Added in version 7.0.
Important
These factories are documented to help plugin authors to use them. However
Sopel recommends the usage of pytest and provides a set of fixtures
to get properly configurated factories in sopel.tests.pytest_plugin,
instead of trying to instanciate the factories manually.
- class sopel.tests.factories.BotFactory¶
Factory to create bots.
An instance of this factory can be used as a callable to create an instance of
Sopelwith a fake connection backend. It requires an instance ofConfig, which can be obtained through theConfigFactory.See also
The
botfactory()fixture can be used to instantiate this factory.See also
The created instance will use the
MockIRCBackendwhich does not generate network activity and stores messages sent by the bot in-memory.Note
The created instance does not connect to an external server: it uses a fake backend (
MockIRCBackend), that implements the same interface but stores the messages in-memory instead of sending them through the wire. This allows plugin authors to inspect messages sent by the bot.- __call__(settings: Config) Sopel¶
Create a test bot with a fake backend without any plugins.
- Parameters:
settings – test settings used by the test bot
- Returns:
an instance of Sopel ready for use in tests
This factory creates an instance of
Sopelusing thesettingsprovided and a fake backend which collects all messages sent by the bot.See also
The created instance will use the
MockIRCBackendwhich does not generate network activity and stores messages sent by the bot in-memory.
- preloaded(
- settings: config.Config,
- preloads: Iterable[str] | None = None,
Create a test bot with a fake backend and preload its plugins.
- Parameters:
settings – Sopel’s configuration for testing purposes
preloads – list of plugins to preload, setup, and register
- Returns:
a test instance of the bot
This method will create an instance of
Sopelusing thesettingsprovided and a fake backend which collects all messages sent by the bot.This will automatically load the
coretasksplugin, and every other plugin frompreloads:factory = BotFactory() bot = factory.preloaded(settings, ['emoticons', 'remind'])
See also
The created instance will use the
MockIRCBackendwhich does not generate network activity and stores messages sent by the bot in-memory.Note
This will automatically setup plugins: be careful with plugins that require access to external services or network connectivity during setup.
You may also need to manually call shutdown routines for the loaded plugins once the tests are concluded.
- class sopel.tests.factories.ConfigFactory(tmpdir: pathlib.Path)¶
Factory to create settings.
- Parameters:
tmpdir – a test folder to store documentation files
An instance of this factory can be used as a callable to create an instance of
Config.Changed in version 8.1: This factory used to depend on pytest’s fixture
tmpdir, but is now using Python’s standardpathlib.Pathinstead.See also
The
configfactory()fixture can be used to instantiate this factory.
- class sopel.tests.factories.IRCFactory¶
Factory to create mock IRC servers.
An instance of this factory can be used as a callable to create an instance of
sopel.tests.mocks.MockIRCServer. It requires an instance of Sopel, which can be obtained through theBotFactory.See also
The
ircfactory()fixture can be used to create this factory.- __call__( ) MockIRCServer¶
Call the factory with a test bot to return a test server.
- Parameters:
mockbot – a test instance of Sopel
join_threads – an optional flag to wait on running triggers (defaults to
True)
- Returns:
an instance of a test server
- class sopel.tests.factories.TriggerFactory¶
Factory to create triggers.
An instance of this factory can be used as a callable to create an instance of
sopel.trigger.Trigger. It requires an instance of Sopel, which can be obtained through theBotFactory, as well as the trigger’s content.See also
The
triggerfactory()fixture can be used to instantiate this factory.- __call__( ) Trigger¶
Call the factory with a test bot to return a test trigger message.
- Parameters:
mockbot – a test instance of Sopel
raw – the raw trigger’s content
pattern – an optional regex pattern (default to
.*)
- Returns:
an instance of a test trigger
- wrapper( ) SopelWrapper¶
Create a trigger and return a wrapped instance of Sopel.
- Parameters:
mockbot – a test instance of Sopel
raw – the raw trigger’s content
pattern – an optional regex pattern (default to
.*)
- Returns:
a wrapped instance of Sopel with the created trigger
This method is a shortcut over calling the factory to create a trigger and get an instance of
SopelWrapperwith it.
- class sopel.tests.factories.UserFactory¶
Factory to create mock users.
An instance of this factory can be used as a callable to create an instance of
sopel.tests.mocks.MockUser. It requires the information of the test user such as its nick, account, and hostname.See also
The
userfactory()fixture can be used to create this factory.