Skip to content

Add testing API for py-shiny #1156

Closed

Description

@pstorozenko

Hi!
Right now, it's very hard to test the shiny part of your dashboard.
While the internals of py-shiny are tested with playwright, the testing API with ShinyAppProc and more is not in the package.

I'm keeping my fingers crossed for a quick release of shiny.testing namespace as it would allow us creating testable (i.e. enterprise ready) shiny apps in python, just like in R. :)
Any ideas when to expect this feature?

Correct me if I'm wrong, but there's no issue on GH that tracks the progress of testing namespace, hence I created this one.


Relevant links:

Activity

  1. added
    testingRelated to testing shiny apps
    and removed on Apr 3, 2024
  2. schloerke commented on Apr 3, 2024

    @schloerke
    Collaborator

    Hi @pstorozenko 👋 !

    @karangattu and I are aiming for a testing release near conf in the early fall. We are hoping to have the existing classes (and their methods) documented and moved under shiny.test (or something similar).


    It seems reasonable to try to make basic testing possible in the near future (knowing the API might change), such as the simple UI test linked above. (Functions related to conftest)

    The other helper controls could be aded piecemeal as we document / vet them.

  3. pstorozenko commented on Apr 3, 2024

    @pstorozenko
    Author

    Great to hear that!
    In the meantime, I'm working on the blog post on how to test pyshiny apps with playwright right now, will share here and on discord once it's ready :)

  4. pstorozenko commented on Apr 11, 2024

    @pstorozenko
    Author

    For all those wanting to test your apps right now

    It's great that there's an Epic for testing shiny apps 🎉
    In the meantime you can check this repo with examples.
    And the accompanying blog post.

    For adding tests to the CI/CD pipeline you can use this snippet in conftest.py (check this SO thread).
    This assumes the app is defined as app = App(...) in the app.py file.

    import threading
    
    import pytest
    import requests
    from tenacity import retry, stop_after_delay, wait_fixed
    from uvicorn import Config, Server
    
    APP_HOST = "127.0.0.1"
    APP_PORT = 8989
    APP_URL = f"http://{APP_HOST}:{APP_PORT}"
    
    
    @retry(wait=wait_fixed(0.5), stop=stop_after_delay(10))
    def wait_for_server_to_start(url):
        response = requests.get(url)  # noqa: S113
        response.raise_for_status()  # Will raise an exception if the request is unsuccessful, i.e. server is not ready
    
    
    def run_server():
        config = Config(app="app:app", host=APP_HOST, port=APP_PORT, log_level="info")
        server = Server(config)
        server.run()
    
    
    @pytest.fixture(scope="session", autouse=True)
    def _shiny_server():
        # Start the server in a background thread
        thread = threading.Thread(target=run_server, daemon=True)
        thread.start()
    
        wait_for_server_to_start(APP_URL)
    
        return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testingRelated to testing shiny apps

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions