pytest
pytest-channelstack brings channelstack to pytest: a fixture per platform, one session per test, and expect(...) checks with the same waiting rules as the Vitest matchers. This page shows how to set it up and write tests with it.
Install#
pip install pytest-channelstack
npm i -g channelstack
pytest-channelstack needs Python 3.12 or later. It installs the channelstack Python SDK, pytest and pytest-asyncio with it, and registers itself as a pytest plugin, so there is nothing to import in conftest.py.
The server is the same binary for every language, and it comes from npm. Start it, and your app, as in connecting your app:
npx channelstack
Configure pytest#
The client outlives each test, so every test and fixture must run on one event loop for the whole run. Set that once, with pytest-asyncio's own settings:
[pytest]
asyncio_mode = auto
asyncio_default_fixture_loop_scope = session
asyncio_default_test_loop_scope = session
channelstack_mode = attach[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "session"
asyncio_default_test_loop_scope = "session"
channelstack_mode = "attach"With asyncio_mode = auto, every async def test_… runs without a marker. Without the two loop-scope settings, the second test fails, because the client was made on the first test's loop.
The plugin adds three settings of its own:
| Setting | Default | What it does |
|---|---|---|
channelstack_mode, or --channelstack-mode on the command line | auto | attach fails when no server is running, start starts a private one, auto attaches if it can and starts one otherwise |
channelstack_timeout | 10s | How long an expect(...) check, and every SDK wait, waits |
channelstack_quiet_for | 750ms | How long the app must be quiet before a negative or exact-count check, or a latest_… read, reads |
The command-line flag wins over the ini file. The client finds the server the way every channelstack client does: CHANNELSTACK_URL with CHANNELSTACK_TOKEN, or else the server.json in ~/.channelstack (or CHANNELSTACK_HOME). In start mode it runs CHANNELSTACK_SERVER_BIN, or channelstack from your PATH. A server started that way has no agent connected to it, so attach is the usual choice, and in CI it fails at once when the server isn't up.
The fixtures#
| Fixture | Scope | What it is |
|---|---|---|
channelstack | the run | The client: env(), reset(), open_session() |
channelstack_session | the test | The test's session. Closing it after the test removes everything the test created |
slack | the test | The fake Slack workspace in the test's session |
github | the test | The fake GitHub org |
linear | the test | The fake Linear workspace |
notion | the test | The fake Notion workspace |
resend | the test | The fake Resend team |
google | the test | The fake Gmail mailbox |
discord | the test | The fake Discord guild |
openai | the test | The fake OpenAI, your agent's model: its scripts and calls |
A test asks for the fixtures it uses by name. Asking for slack opens the test's session on the way, so most tests name only the platform.
A full test#
import re
from channelstack import SlackFixture
from pytest_channelstack import expect
async def test_a_mention_gets_exactly_one_reply_in_the_thread(slack: SlackFixture) -> None:
ana = await slack.create_user(name="Ana")
channel = await slack.create_channel(participants=[ana, slack.app])
msg = await ana.say("@my-agent summarize yesterday's incidents", channel=channel)
await expect(msg.thread).to_have_reply_from("my-agent", text=re.compile("incident", re.I))
await expect(msg.thread).to_have_replies_from(slack.app, count=1)
await expect(channel).not_to_have_channel_message_from("my-agent")
async def test_follow_ups_in_the_thread_are_answered_without_a_new_mention(slack: SlackFixture) -> None:
ana = await slack.create_user(name="Ana")
channel = await slack.create_channel(participants=[ana, slack.app])
msg = await ana.say("@my-agent what's the status of the web deploy?", channel=channel)
await msg.thread.wait_for_reply(from_="my-agent")
await ana.reply("and the API?", thread=msg)
answer = await msg.thread.wait_for_reply(from_="my-agent")
assert re.search("api", answer.text, re.I)
async def test_stop_ends_the_stream_and_the_agent_stays_quiet(slack: SlackFixture) -> None:
ana = await slack.create_user(name="Ana")
msg = await ana.say("write a long report on Q3", to=slack.app)
await expect(msg.thread).to_have_streaming_reply_from("my-agent")
await ana.stop(thread=msg)
await expect(msg.thread).to_have_status("stopped")
await expect(msg.thread).to_have_replies_from("my-agent", count=1)
The API is the same as in TypeScript, in Python's spelling: methods and options in snake case (create_user, wait_for_reply, bad_signature=True), options as keyword arguments, and from_ where TypeScript says from. People act and resources are read, exactly as writing tests describes.
expect(...) and not_to_…#
Each check is an awaitable method on expect(subject). The Vitest matcher toHaveReplyFrom is to_have_reply_from, and Vitest's .not. is a not_to_… method of its own:
await expect(msg.thread).to_have_reply_from("my-agent")
await expect(first.thread).not_to_have_reply_from("my-agent")
The checks follow the matchers' rules. A positive check polls until it holds and fails after the timeout. A negative or exact-count check waits until the app has been quiet for the whole window, then checks once. A failure raises AssertionError with what was expected, what the thread had, and what the app left open:
AssertionError: Expected exactly 1 reply from my-agent, found 2:
my-agent: Deploying web to staging
my-agent: Deploying web to staging
Waiting and quiet explains why the checks wait the way they do.
Which checks exist#
Every Vitest matcher has a Python check, with the same semantics, options and failure messages:
| Python | Vitest | Subject |
|---|---|---|
to_have_reply_from(author, text=) | toHaveReplyFrom | A thread or a DM |
to_have_replies_from(author, count=, text=) | toHaveRepliesFrom | A thread or a DM |
to_have_channel_message_from(author) | toHaveChannelMessageFrom | A channel |
to_have_streaming_reply_from(author) | toHaveStreamingReplyFrom | A thread |
to_have_status(state) | toHaveStatus | A thread |
to_have_status_text(text) | toHaveStatusText | A thread |
to_have_reaction(emoji, by=) | toHaveReaction | A message |
to_have_button(label) | toHaveButton | A Slack message, thread or conversation |
to_have_opened_modal(title=None) | toHaveOpenedModal | A Slack person |
to_be_ephemeral_for(user) | toBeEphemeralFor | A Slack message |
to_contain_text(text, count=, from_=, as_=) | toContainText | Anything with a transcript |
to_have_comment_from(author, text=) | toHaveCommentFrom | An issue, a PR, a discussion |
to_have_comments_from(author, count=) | toHaveCommentsFrom | An issue, a PR, a discussion |
to_have_review_comment(path=, line=, from_=, text=) | toHaveReviewComment | A GitHub pull request |
to_have_review_from(author, state=) | toHaveReviewFrom | A GitHub pull request |
to_have_check_run(name, status=, conclusion=) | toHaveCheckRun | A GitHub pull request or commit |
to_have_label(name) | toHaveLabel | A GitHub issue or PR, a Linear issue |
to_be_merged() | toBeMerged | A GitHub pull request |
to_have_session_state(state) | toHaveSessionState | A Linear session or issue |
to_have_activity(type=, from_=, text=) | toHaveActivity | A Linear session or issue |
to_be_delegated_to(app) | toBeDelegatedTo | A Linear issue |
to_be_assigned_to(user) | toBeAssignedTo | A Linear issue |
to_have_state(name) | toHaveState | A Linear issue |
to_have_block(text) | toHaveBlock | A Notion page |
to_have_property_value(name, value) | toHavePropertyValue | A Notion page (a database row) |
to_be_accepted(within=) | toBeAccepted | A delivery |
to_be_rejected() | toBeRejected | A delivery that was resent forged |
to_be_retried(times=) | toBeRetried | A delivery |
to_have_received_call(operation, error=, times=) | toHaveReceivedCall | A platform fixture (slack) |
Each has a not_to_… twin. text takes a string to look for or a compiled re.Pattern, and an author is a handle (slack.app, ana) or a name. to_be_delegated_to and to_be_assigned_to take None for no one. to_have_property_value compares with ==, so unittest.mock.ANY (or any object with its own __eq__) stands in where Vitest uses expect.any(Number). A delivery check takes the delivery itself:
from channelstack import SlackFixture
from pytest_channelstack import expect
async def test_rejects_a_bad_signature(slack: SlackFixture) -> None:
ana = await slack.create_user(name="Ana")
msg = await ana.say("hi", to=slack.app)
assert msg.delivery is not None
await msg.delivery.resend(bad_signature=True)
await expect(msg.delivery).to_be_rejected()
Each check is built on a plain SDK read (delivery.refresh(), pull.reviews(), page.property(name), …), so a test can also read the value itself and assert on it. Unlike expect(...), a plain read doesn't wait for quiet, so use it for positive facts, or call it after a wait_for_… or latest_… method.
Options#
Every check takes timeout=, and every negative or exact-count check also takes quiet_for=:
await expect(msg.thread).to_have_reply_from("my-agent", timeout="30s")
await expect(msg.thread).to_have_replies_from("my-agent", count=1, quiet_for="2s")
A duration is a string ("750ms", "3s", "1m") or milliseconds as a number. Without them, a check uses channelstack_timeout and channelstack_quiet_for from the ini file. The SDK's own waiting methods (wait_for_reply, latest_reply, settled, …) take timeout= too, and default to the same ini settings.
Shared people with fixtures#
People you use in many tests are ordinary pytest fixtures, built on slack. They're created per test, like the platform fixtures they depend on:
import pytest
from channelstack import SlackConversation, SlackFixture, SlackUser
@pytest.fixture
async def ana(slack: SlackFixture) -> SlackUser:
return await slack.create_user(name="Ana")
@pytest.fixture
async def ben(slack: SlackFixture) -> SlackUser:
return await slack.create_user(name="Ben", admin=True)
@pytest.fixture
async def support(slack: SlackFixture, ana: SlackUser, ben: SlackUser) -> SlackConversation:
return await slack.create_channel(name="support", participants=[ana, ben, slack.app])
from channelstack import SlackConversation, SlackUser
from pytest_channelstack import expect
async def test_an_admin_and_a_member_see_the_same_answer(
support: SlackConversation, ana: SlackUser, ben: SlackUser
) -> None:
question = await ana.say("@my-agent who's on call?", channel=support)
await ben.reply("+1, also need this", thread=question)
await expect(question.thread).to_have_replies_from("my-agent", count=1)
A relative fixture path starts from the test file, as in Vitest: github.create_repo(files="./fixtures/web", …) reads tests/fixtures/web.
Live mode#
With live credentials in the environment, the same tests act on real sandbox workspaces (live mode). A step live mode can't do is refused with unsupported_in_live (uninstall_app, reading deliveries), or with action_requires when it needs a piece live mode doesn't drive yet (a fault, Slack's Stop, waiting for quiet: see what skips). The plugin turns both into pytest.skip, with the reason:
SKIPPED [1] tests/test_slack.py:88: live: SlackFixture.fault live needs proxy mode: …
Live, a message or comment has no delivery: msg.delivery is None, so a delivery test checks for it and calls pytest.skip itself (delivery and failures).
Any other error still fails the test, so a broken live setup turns the run red. The fake run on every pull request runs every test in full.