A test fixture for running a server and imperatively displaying views This fixture is typically used alongside async web drivers like ``playwight``. Example: .. code-block:: async with BackendFixture() as server: server.mount(MyComponent)
| 23 | |
| 24 | |
| 25 | class BackendFixture: |
| 26 | """A test fixture for running a server and imperatively displaying views |
| 27 | |
| 28 | This fixture is typically used alongside async web drivers like ``playwight``. |
| 29 | |
| 30 | Example: |
| 31 | .. code-block:: |
| 32 | |
| 33 | async with BackendFixture() as server: |
| 34 | server.mount(MyComponent) |
| 35 | """ |
| 36 | |
| 37 | _records: list[logging.LogRecord] |
| 38 | _server_future: asyncio.Task[Any] |
| 39 | _exit_stack = AsyncExitStack() |
| 40 | |
| 41 | def __init__( |
| 42 | self, |
| 43 | host: str = "127.0.0.1", |
| 44 | port: int | None = None, |
| 45 | app: Any | None = None, |
| 46 | implementation: BackendType[Any] | None = None, |
| 47 | options: Any | None = None, |
| 48 | timeout: float | None = None, |
| 49 | ) -> None: |
| 50 | self.host = host |
| 51 | self.port = port or find_available_port(host) |
| 52 | self.mount, self._root_component = _hotswap() |
| 53 | self.timeout = ( |
| 54 | REACTPY_TESTING_DEFAULT_TIMEOUT.current if timeout is None else timeout |
| 55 | ) |
| 56 | |
| 57 | if app is not None and implementation is None: |
| 58 | msg = "If an application instance its corresponding server implementation must be provided too." |
| 59 | raise ValueError(msg) |
| 60 | |
| 61 | self._app = app |
| 62 | self.implementation = implementation or default_server |
| 63 | self._options = options |
| 64 | |
| 65 | @property |
| 66 | def log_records(self) -> list[logging.LogRecord]: |
| 67 | """A list of captured log records""" |
| 68 | return self._records |
| 69 | |
| 70 | def url(self, path: str = "", query: Any | None = None) -> str: |
| 71 | """Return a URL string pointing to the host and point of the server |
| 72 | |
| 73 | Args: |
| 74 | path: the path to a resource on the server |
| 75 | query: a dictionary or list of query parameters |
| 76 | """ |
| 77 | return urlunparse( |
| 78 | [ |
| 79 | "http", |
| 80 | f"{self.host}:{self.port}", |
| 81 | path, |
| 82 | "", |
no outgoing calls