testserver.TestServerSession

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

An in-memory Shiny session driven from ordinary (non-async) test code.

Construct one with test_server 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.

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. Set inputs to simulate user interactions, and read outputs between them:

with test_server(app_path) as ts:
    ts.set_inputs(a=1, b=2)
    assert ts.get_output("name") == "foo"
    ts.set_inputs(a=3, b=4)
    assert ts.get_output("name") == "bar"

Values registered with shiny.testmode.export_test_values are read with get_export. To keep values for assertions after the block, capture a TestServerValues with to_values, or a plain dictionary with dict(session), while the session is still open.

This class drives its own event loop on the calling thread, so it cannot be used from inside a running event loop — in an async test, use test_server_async instead.

See Also

Attributes

Name Description
error A summary of the first error, or None when is_ok is True.
is_ok True when no reactive errors and no fatal errors have 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.TestServerSession.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
TestServerSession This session, so calls can be chained.

Raises

Name Type Description
RuntimeError If the session is not running.

get_export

testserver.TestServerSession.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.TestServerSession.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.TestServerSession.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.TestServerSession.keys()

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

make_scope

testserver.TestServerSession.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
TestServerScope A TestServerScope reading and writing the same live session.

See Also

root_scope

testserver.TestServerSession.root_scope()

Return this session, which is already the root.

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

set_inputs

testserver.TestServerSession.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
TestServerSession 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.

to_values

testserver.TestServerSession.to_values()

Capture the session's current state.

Returns

Name Type Description
TestServerValues A TestServerValues snapshot of copies, so it stays valid after the with block ends.