Skip to content

Add testing API for py-shiny #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pstorozenko opened this issue Feb 26, 2024 · 3 comments · Fixed by #1448
Closed

Add testing API for py-shiny #1156

pstorozenko opened this issue Feb 26, 2024 · 3 comments · Fixed by #1448
Labels
testing Related to testing shiny apps

Comments

@pstorozenko
Copy link

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:

@schloerke schloerke added testing Related to testing shiny apps and removed needs-triage labels Apr 3, 2024
@schloerke
Copy link
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.

@pstorozenko
Copy link
Author

pstorozenko commented Apr 3, 2024

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 :)

@pstorozenko
Copy link
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
Labels
testing Related to testing shiny apps
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants