Fixtures with pytest#

Pytest plugin for Sopel.

New in version 7.0.

sopel.tests.pytest_plugin.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(tmpdir)#

Fixture to get a config factory.

Returns:

a factory to create test settings

Return type:

sopel.tests.factories.ConfigFactory

The factory will be automatically configured with a tmpdir object.

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(
tested_func,
msg,
results,
privmsg,
admin,
owner,
repeat,
use_regexp,
ignore=[],
)#

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

Parameters:
  • tested_func (callable) – a Sopel callable that accepts a SopelWrapper and a Trigger

  • 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.

sopel.tests.pytest_plugin.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()#

Fixture to get a trigger factory.

Returns:

a factory to create triggers

Return type:

sopel.tests.factories.TriggerFactory

sopel.tests.pytest_plugin.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'