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.Eventsandslack.tests.data.Messages.@pytest.mark.parametrize("slack_event", ("channel_deleted", "simple"), indirect=True) def test_events(slack_event): assert slack_event["event"]["type"] in ("channel_deleted", "message")
To only get
slack.tests.data.Messagesmembers you can parametrize the test that way:@pytest.mark.parametrize( "slack_event", {**slack.tests.data.Messages.__members__}, indirect=True ) def test_messages(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.InteractiveMessageorslack.tests.data.DialogSubmission.@pytest.mark.parametrize("slack_action", ("button_ok", "button_cancel"), indirect=True) def test_actions(slack_action): action = slack.actions.Action.from_http(slack_action) assert action["type"] == "interactive_message"
-
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): command = slack.commands.Command(slack_command) assert command["command"] == "/test"
-
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'}
- status:
Parametrize a reponse:
@pytest.mark.asyncio @pytest.mark.parametrize( "slack_client", ({"body": {"ok": True, "hello": "world"}},), indirect=True ) async def test_client_custom_body(slack_client): data = await slack_client.query(slack.methods.AUTH_TEST) assert data == {"ok": True, "hello": "world"}
The
bodyparameter of a request can be a string corresponding to one of the methods available inslack.tests.data.Methods.@pytest.mark.asyncio @pytest.mark.parametrize("slack_client", ({"body": "auth_test"},), indirect=True) async def test_client_body(slack_client): data = await slack_client.query(slack.methods.AUTH_TEST) assert data["team"] == "TestTeam Workspace"
Parametrize multiple responses:
@pytest.mark.asyncio @pytest.mark.parametrize("slack_client", ({"status": [200, 500]},), indirect=True) async def test_client_status(slack_client): await slack_client.query(slack.methods.AUTH_TEST) with pytest.raises(slack.exceptions.HTTPException): await slack_client.query(slack.methods.AUTH_TEST)
@pytest.mark.asyncio @pytest.mark.parametrize( "slack_client", ({"body": ["channels_iter", "channels"]},), indirect=True ) async def test_client_iter(slack_client): async for channel in slack_client.iter(slack.methods.CHANNELS_LIST): print(channel)
Parametrize multiple run:
@pytest.mark.asyncio @pytest.mark.parametrize( "slack_client,ok", (({"status": 200}, True), ({"status": 500}, False)), indirect=["slack_client"], ) async def test_client_multiple_run(slack_client, ok): if ok: await slack_client.query(slack.methods.AUTH_TEST) else: 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.InteractiveMessage[source]¶ List of available interactive message action for testing
- button_ok
- button_cancel