testserver.AsyncTestServerSession

testserver.AsyncTestServerSession(
    app=None,
    *,
    client_data=None,
    timeout_secs=5.0,
)

An in-memory Shiny session driven from async test code.

Construct one with test_server_async rather than directly. The session runs the app's server function against a mock connection — no browser and no network server — so inputs can be set and outputs asserted in process.

Use it as an async context manager to keep the session alive across several user interactions:

async with test_server_async(server) as session:
    await session.set_inputs(x=10)
    assert session.outputs["doubled"] == "20"

async with is the only supported way to run a session: it starts the app on entry and always tears it down on exit, including when the test fails. Values registered with shiny.testmode.export_test_values are read with get_export.

See Also

Attributes

Name Description
error A summary of the first error, or None when is_ok is True.
is_ok True when nothing errored and no fatal error occurred.
ns The namespace these ids are read in; Root ("") for a session.

Methods

Name Description
flush Re-read the session’s output, export, and error values.
get_export Return one exported test value.
get_input Return one input value.
get_output Return one output value.
keys Return the keys dict(session) produces. See TestServerValues.
make_scope Return a view of this session namespaced to a module instance.
root_scope Return this session, which is already the root.
set_inputs Set input values and wait for the resulting reactive flush.
to_values Capture the session’s current state.

flush

testserver.AsyncTestServerSession.flush()

Re-read the session's output, export, and error values.

set_inputs already does this, so an explicit call is only needed after something outside the test changes reactive state (for example an effect driven by a timer).

Returns

Name Type Description
AsyncTestServerSession This session, so calls can be chained.

get_export

testserver.AsyncTestServerSession.get_export(name)

Return one exported test value.

Parameters

Name Type Description Default
name str A name passed to shiny.testmode.export_test_values. required

Returns

Name Type Description
TestServerValue A TestServerValue. It compares equal to the value itself, so session.get_export("doubled") == 40 works.

Raises

Name Type Description
KeyError If that name was not exported.

get_input

testserver.AsyncTestServerSession.get_input(name)

Return one input value.

Parameters

Name Type Description Default
name str An input id. required

Returns

Name Type Description
TestServerValue A TestServerValue. It compares equal to the value itself, so session.get_input("n") == 10 works.

Raises

Name Type Description
KeyError If the session has not received that input.

get_output

testserver.AsyncTestServerSession.get_output(name)

Return one output value.

Parameters

Name Type Description Default
name str An output id. required

Returns

Name Type Description
TestServerValue A TestServerValue, with status "silent" if the output’s latest render produced nothing. It compares equal to the value itself, so session.get_output("txt") == "hi" works.

Raises

Name Type Description
KeyError If the app has no such output.

keys

testserver.AsyncTestServerSession.keys()

Return the keys dict(session) produces. See TestServerValues.

make_scope

testserver.AsyncTestServerSession.make_scope(id)

Return a view of this session namespaced to a module instance.

Mirrors shiny.Session.make_scope: inside a module the app's server code uses bare ids, and so does the returned view, while the session underneath keeps the namespaced ones.

Parameters

Name Type Description Default
id str The id the module instance was given, as in counter_server("counter"). required

Returns

Name Type Description
AsyncTestServerScope An AsyncTestServerScope reading and writing the same live session.

See Also

root_scope

testserver.AsyncTestServerSession.root_scope()

Return this session, which is already the root.

Mirrors shiny.Session.root_scope, so code handed either a session or an AsyncTestServerScope can reach the session without a type check.

set_inputs

testserver.AsyncTestServerSession.set_inputs(**kwargs)

Set input values and wait for the resulting reactive flush.

Simulates a user interaction: the values are sent to the session as an input update, and the call returns once the reactive graph has settled and the values read by get_input, get_output, and get_export have been refreshed.

Parameters

Name Type Description Default
**kwargs Any Input values keyed by input id. Ids that are not valid Python identifiers – a module’s namespaced "counter-n", say – go through an unpacked dictionary: set_inputs(**{"counter-n": 7}). {}

Returns

Name Type Description
AsyncTestServerSession This session, so calls can be chained.

Raises

Name Type Description
TimeoutError If the flush does not complete within timeout_secs.
RuntimeError If the session is not running, or has already ended because of a fatal error.

to_values

testserver.AsyncTestServerSession.to_values()

Capture the session's current state.

Returns

Name Type Description
TestServerValues A TestServerValues snapshot of copies, which stays valid after the session is closed.