slack.tests.plugin - Pytest fixtures

Slack-sansio provide a pytest plugin with some fixtures to facilitate testing of the slack API.

Installation

To load the plugin add the snippet below to your conftest.py.

pytest_plugins = "slack.tests.plugin",

Available fixtures

slack.tests.plugin.slack_event()

Fixture returning data sent by the slack event API.

This fixture can be parametrized to return one or more available event from slack.tests.data.Events and slack.tests.data.Messages.

@pytest.mark.parametrize('slack_event', ('channel_deleted', 'simple'), indirect=True)
def test_events(slack_event):
    assert True

To only get slack.tests.data.Messages members you can parametrize the test that way:

@pytest.mark.parametrize("slack_event", {**slack.tests.data.Messages.__members__}, indirect=True)
async def test_messages(self, slack_event):
    assert slack_event["event"]["type"] == "message"
slack.tests.plugin.slack_action()

Fixture returning data sent by the slack API when using an interactive message or dialog.

This fixture can be parametrized to return one or more available action from slack.tests.data.InteractiveMessage or slack.tests.data.DialogSubmission.

@pytest.mark.parametrize('slack_action', ('button_ok', 'button_cancel'), indirect=True)
def test_actions(slack_action):
    assert True
slack.tests.plugin.slack_command()

Fixture returning data sent by the slack API when using a slash command.

This fixture can be parametrized to return one or more available command from slack.tests.data.Commands.

@pytest.mark.parametrize('slack_command', ('text', 'no_text'), indirect=True)
def test_commands(slack_command):
    assert True
slack.tests.plugin.slack_client()

Fixture returning a fake slack client.

By default the client return to any request:
  • status: 200
  • body: {'ok': True}
  • headers: {'content-type': 'application/json; charset=utf-8'}

This can be parametrize with the _request parameter.

@pytest.mark.asyncio
@pytest.mark.parametrize('slack_client', ({'_request': {'body': {'ok': True, 'hello': 'world'}}},),
 indirect=True)
async def test_client(slack_client):
    data = await slack_client.query(slack.methods.AUTH_TEST)
    assert data == {'ok': True, 'hello': 'world'}

The body parameter of a request can be a string corresponding to one of the methods available in slack.tests.data.Methods.

For multiple requests you can set the _request parameter to a list.

@pytest.mark.asyncio
@pytest.mark.parametrize('slack_client', ({'_request': [
    {'body': 'channels'},
    {'status': 500}
]},), indirect=True)
async def test_client(slack_client):
    data1 = await slack_client.query(slack.methods.AUTH_TEST)
    with pytest.raises(slack.exceptions.HTTPException):
        await slack_client.query(slack.methods.AUTH_TEST)

Available data

class slack.tests.data.Events[source]

List of available event for testing

  • channel_deleted
  • pin_added
  • reaction_added
class slack.tests.data.Messages[source]

List of available message for testing

  • simple
  • snippet
  • shared
  • threaded
  • bot
  • bot_edit
  • attachment
  • edit
  • edit_threaded
  • mention
  • none_text
  • channel_topic
class slack.tests.data.Commands[source]

List of available command for testing

  • text
  • no_text
class slack.tests.data.InteractiveMessage[source]

List of available interactive message action for testing

  • button_ok
  • button_cancel
class slack.tests.data.DialogSubmission[source]

List of available dialog submission action for testing

  • dialog_submission
class slack.tests.data.Methods[source]

List of available methods for testing

  • channels
  • channels_iter (channels with a cursor)
  • users_info
  • auth_test
  • rtm_connect