Skip to content

Commit

Permalink
Updated test suite in the first tutorial and removed redundant snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Apr 22, 2024
1 parent e954f52 commit c8a1e66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
34 changes: 0 additions & 34 deletions docs/userguide/snippets/deployment.py

This file was deleted.

2 changes: 1 addition & 1 deletion docs/userguide/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ we'll test them against each other.
Create a ``tests`` directory at the root of the project directory and create a module
named ``test_client_server`` there (the ``test_`` prefix is important):

.. literalinclude:: snippets/deployment.py
.. literalinclude:: ../../examples/tutorial1/tests/test_client_server.py
:language: python

In the above test module, the first thing you should note is
Expand Down
23 changes: 17 additions & 6 deletions examples/tutorial1/tests/test_client_server.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
# isort: off
import pytest
from __future__ import annotations

from asphalt.core import Context
from pytest import CaptureFixture
from collections.abc import AsyncGenerator

import pytest
from anyio import wait_all_tasks_blocked
from echo.client import ClientComponent
from echo.server import ServerComponent
from pytest import CaptureFixture

from asphalt.core import Context

pytestmark = pytest.mark.anyio


async def test_client_and_server(capsys: CaptureFixture[str]) -> None:
@pytest.fixture
async def server(capsys: CaptureFixture[str]) -> AsyncGenerator[None, None]:
async with Context():
server = ServerComponent()
await server.start()
yield


async def test_client_and_server(server: None, capsys: CaptureFixture[str]) -> None:
async with Context():
client = ClientComponent("Hello!")
await client.start()
await client.run()

# Grab the captured output of sys.stdout and sys.stderr from the capsys fixture
await wait_all_tasks_blocked()
out, err = capsys.readouterr()
assert out == "Message from client: Hello!\nServer responded: Hello!\n"
assert "Message from client: Hello!" in out
assert "Server responded: Hello!" in out

0 comments on commit c8a1e66

Please sign in to comment.