Fixtures with pytest

Pytest plugin for Sopel.

Added in version 7.0.

sopel.tests.pytest_plugin.botfactory() BotFactory

Fixture to get a Bot factory.

Returns:

a factory to create a mocked bot instance

Return type:

sopel.tests.factories.BotFactory

This is very useful in unit tests:

def test_bot(configfactory, botfactory):
    settings = configfactory('... skip for clarity ...')
    bot = botfactory(settings) # no plugins loaded
    # ... do something with the bot

def test_bot_loaded(configfactory, botfactory):
    settings = configfactory('... skip for clarity ...')
    bot = botfactory.preloaded(settings, ['myplugin'])
    # now the bot has `coretasks` and `myplugin` loaded
sopel.tests.pytest_plugin.configfactory(tmp_path: pathlib.Path) ConfigFactory

Fixture to get a config factory.

Parameters:

tmp_path – a temporary path directory

Returns:

a factory to create test settings

The factory will be automatically configured with a tmpdir object.

sopel.tests.pytest_plugin.ircfactory() IRCFactory

Fixture to get an IRC factory.

Returns:

a factory to create mock IRC servers

Return type:

sopel.tests.factories.IRCFactory

For example, a plugin command could be tested with this:

from sopel.tests import rawlist

def test_mycommand(configfactory, botfactory, ircfactory, userfactory):
    settings = configfactory('... skip for clarity ...')
    bot = botfactory(settings, ['myplugin'])
    irc = ircfactory(bot)
    user = userfactory('User')

    irc.say(user, '#test', '.mycommand')

    assert bot.backend.message_sent == rawlist(
        'PRIVMSG #test :My plugin replied this.'
    )
sopel.tests.pytest_plugin.triggerfactory() TriggerFactory

Fixture to get a trigger factory.

Returns:

a factory to create triggers

Return type:

sopel.tests.factories.TriggerFactory

sopel.tests.pytest_plugin.userfactory() UserFactory

Fixture to get a user factory.

Returns:

a factory to create mock users

Return type:

sopel.tests.factories.UserFactory

def test_mycommand(userfactory):
    user = userfactory('User')

    assert user.nick == 'User'
    assert user.user == 'user'
    assert user.host == 'example.com'
    assert user.prefix == 'User!user@example.com'

Internal utility functions

Warning

This is all internal code, not intended for direct use by plugins. It is subject to change between versions, even patch releases, without any advance warning.

sopel.tests.pytest_plugin.get_disable_setup()

Generate a pytest fixture to setup the plugin before running its tests.

When using @example for a plugin callable with an expected output, pytest will be used to run it as a test. In order to work, this fixture must be added to the plugin to set up the plugin before running the test.

sopel.tests.pytest_plugin.get_example_test(
handler,
msg,
results,
privmsg,
admin,
owner,
repeat,
use_regexp,
ignore=[],
)

Get a function that calls handler with fake wrapper and trigger.

Parameters:
  • handler (sopel.plugins.callables.PluginCallable) – a plugin command to test

  • msg (str) – message that is supposed to trigger the command

  • results (list) – expected output from the callable

  • privmsg (bool) – if True, make the message appear to have arrived in a private message to the bot; otherwise make it appear to have come from a channel

  • admin (bool) – make the message appear to have come from an admin

  • owner (bool) – make the message appear to have come from an owner

  • repeat (int) – how many times to repeat the test; useful for tests that return random stuff

  • use_regexp (bool) – pass True if results are in regexp format

  • ignore (list) – strings to ignore

Returns:

a test function for tested_func

Return type:

function

sopel.tests.pytest_plugin.insert_into_module(func, module_name, base_name, prefix)

Add a function into a module.

This can be used to add a test function, a setup function, or a fixture to an existing module to be used with pytest.