testmode.export_test_values

testmode.export_test_values(**kwargs)

Register named values to include in the test-mode snapshot.

Uses the current reactive session. Each value must be a zero-argument callable (a plain function/lambda or a reactive.calc); it is evaluated lazily, in a reactive isolate, when a test snapshot is requested. Registered values appear under the export block of the snapshot served at /session/{id}/dataobj/shinytest.

Has no effect unless test mode is enabled (SHINY_TESTMODE=1), so calls can be left in production code. Re-registering a name overwrites it.

To register against a session other than the current one, wrap the call in that session's context:

from shiny.session import session_context

with session_context(other_session):
    export_test_values(my_val=lambda: ...)

Parameters

**kwargs : Callable[[], Any] = {}

Named zero-argument callables whose return values are exported.

Raises

: RuntimeError

If there is no active session.

See Also

Examples

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400

## file: app.py
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
from shiny.testmode import export_test_values

app_ui = ui.page_fluid(
    ui.input_slider("n", "N", min=0, max=100, value=20),
    ui.output_text_verbatim("txt"),
)


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.calc
    def doubled() -> int:
        return input.n() * 2

    @render.text
    def txt() -> str:
        return f"n * 2 = {doubled()}"

    # Surface the internal reactive value in the test-mode snapshot, under the
    # snapshot's `export` block. Has no effect unless test mode is enabled
    # (`App(test_mode=True)` or `SHINY_TESTMODE=1`), so the call can be left in
    # production code. The `shiny.pytest` app fixtures enable test mode by
    # default, so an end-to-end test can assert on it with the
    # `shiny.playwright.controller.AppTestValues` controller. The expected
    # value may be an exact value or a function of the actual value:
    #
    #     def is_even(value):
    #         return value % 2 == 0
    #
    #     app_values = controller.AppTestValues(page)
    #     app_values.expect_export("doubled", 40)
    #     app_values.expect_export("doubled", is_even)
    export_test_values(doubled=doubled)


app = App(app_ui, server)