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.
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).
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.
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.
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.
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}).