Skip to content

Commit af3ae92

Browse files
committed
allow to pass custom local app file names for tests
1 parent ee1a47e commit af3ae92

File tree

4 files changed

+159
-2
lines changed

4 files changed

+159
-2
lines changed

shiny/pytest/_pytest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from pathlib import PurePath
4-
from typing import Generator
4+
from typing import Generator, Optional
55

66
import pytest
77

@@ -17,5 +17,7 @@ def local_app(request: pytest.FixtureRequest) -> Generator[ShinyAppProc, None, N
1717
Parameters:
1818
request (pytest.FixtureRequest): The request object for the fixture.
1919
"""
20-
sa_gen = shiny_app_gen(PurePath(request.path).parent / "app.py")
20+
# Get the app_file from the parametrize marker if available
21+
app_file = getattr(request, "param", "app.py")
22+
sa_gen = shiny_app_gen(PurePath(request.path).parent / app_file)
2123
yield next(sa_gen)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from shiny import App, reactive, render, ui
2+
3+
# Define the UI
4+
app_ui = ui.page_fluid(
5+
# Add Font Awesome CSS in the head section
6+
ui.tags.head(
7+
ui.HTML(
8+
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">'
9+
)
10+
),
11+
# Create accordion with panels
12+
ui.accordion(
13+
# Basic panel
14+
ui.accordion_panel(
15+
"Panel A", "This is a basic accordion panel with default settings."
16+
),
17+
# Panel with custom icon
18+
ui.accordion_panel(
19+
"Panel B",
20+
"This panel has a custom star icon and is open by default.",
21+
icon=ui.HTML('<i class="fa-solid fa-star" style="color: gold;"></i>'),
22+
),
23+
# Basic panel that starts closed
24+
ui.accordion_panel(
25+
"Panel C", "This is another basic panel that starts closed."
26+
),
27+
# Panel with longer content
28+
ui.accordion_panel(
29+
"Panel D",
30+
ui.markdown(
31+
"""
32+
This panel contains longer content to demonstrate scrolling:
33+
34+
- Item 1
35+
- Item 2
36+
- Item 3
37+
38+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
39+
eiusmod tempor incididunt ut labore et dolore magna aliqua.
40+
"""
41+
),
42+
),
43+
id="acc_demo",
44+
open=["Panel B", "Panel D"],
45+
multiple=True,
46+
),
47+
# Output for showing which panels are open
48+
ui.output_text("selected_panels"),
49+
)
50+
51+
52+
# Define the server
53+
def server(input, output, session):
54+
@render.text
55+
def selected_panels():
56+
return f"Currently open panels: {input.acc_demo()}"
57+
58+
59+
# Create and return the app
60+
app = App(app_ui, server)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from shiny import reactive
2+
from shiny.express import input, ui, render
3+
4+
# Add Font Awesome CSS in the head section
5+
ui.head_content(
6+
ui.HTML(
7+
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">'
8+
)
9+
)
10+
11+
# Create a list of accordion panels with different configurations
12+
with ui.accordion(id="acc_demo", open=["Panel B", "Panel D"], multiple=True):
13+
# Basic panel
14+
with ui.accordion_panel("Panel A"):
15+
"This is a basic accordion panel with default settings."
16+
17+
# Panel with custom icon
18+
with ui.accordion_panel(
19+
"Panel B", icon=ui.HTML('<i class="fa-solid fa-star" style="color: gold;"></i>')
20+
):
21+
"This panel has a custom star icon and is open by default."
22+
23+
# Basic panel that starts closed
24+
with ui.accordion_panel("Panel C"):
25+
"This is another basic panel that starts closed."
26+
27+
# Panel with longer content
28+
with ui.accordion_panel("Panel D"):
29+
ui.markdown(
30+
"""
31+
This panel contains longer content to demonstrate scrolling:
32+
33+
- Item 1
34+
- Item 2
35+
- Item 3
36+
37+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
38+
eiusmod tempor incididunt ut labore et dolore magna aliqua.
39+
"""
40+
)
41+
42+
43+
# Show which panels are currently open
44+
@render.text
45+
def selected_panels():
46+
return f"Currently open panels: {input.acc_demo()}"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
from playwright.sync_api import Page
3+
from shiny.playwright import controller
4+
from shiny.run import ShinyAppProc
5+
6+
7+
@pytest.mark.parametrize("local_app", ["app-core.py", "app-express.py"], indirect=True)
8+
def test_accordion_demo(page: Page, local_app: ShinyAppProc) -> None:
9+
page.goto(local_app.url)
10+
11+
# Test accordion
12+
accordion = controller.Accordion(page, "acc_demo")
13+
14+
# Test initial state - Panel B and D should be open by default
15+
accordion.expect_multiple(True)
16+
17+
# Test individual panels
18+
panel_a = accordion.accordion_panel("Panel A")
19+
panel_b = accordion.accordion_panel("Panel B")
20+
panel_c = accordion.accordion_panel("Panel C")
21+
panel_d = accordion.accordion_panel("Panel D")
22+
23+
# Test initial states (open/closed)
24+
panel_a.expect_open(False)
25+
panel_b.expect_open(True) # Should be open by default
26+
panel_c.expect_open(False)
27+
panel_d.expect_open(True) # Should be open by default
28+
29+
# Test panel labels
30+
panel_a.expect_label("Panel A")
31+
panel_b.expect_label("Panel B")
32+
panel_c.expect_label("Panel C")
33+
panel_d.expect_label("Panel D")
34+
35+
# Test panel content
36+
panel_a.expect_body("This is a basic accordion panel with default settings.")
37+
panel_b.expect_body("This panel has a custom star icon and is open by default.")
38+
panel_c.expect_body("This is another basic panel that starts closed.")
39+
40+
# Test opening and closing panels
41+
panel_c.set(True) # Open panel C
42+
panel_c.expect_open(True)
43+
44+
panel_b.set(False) # Close panel B
45+
panel_b.expect_open(False)
46+
47+
# Test the output text showing currently open panels
48+
output_text = controller.OutputText(page, "selected_panels")
49+
output_text.expect_value("Currently open panels: ('Panel C', 'Panel D')")

0 commit comments

Comments
 (0)