Skip to content

Commit e3ed2df

Browse files
fix(tests): dynamically determine the path to the shiny app (#1485)
Co-authored-by: Barret Schloerke <[email protected]>
1 parent 68df9ce commit e3ed2df

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
* Expose `shiny.playwright`, `shiny.run`, and `shiny.pytest` modules that allow users to testing their Shiny apps. (#1448, #1456, #1481)
2323
* `shiny.playwright` contains `controller` and `expect` submodules. `controller` will contain many classes to interact with (and verify!) your Shiny app using Playwright. `expect` contains expectation functions that enhance standard Playwright expectation methods.
2424
* `shiny.run` contains the `run_shiny_app` command and the return type `ShinyAppProc`. `ShinyAppProc` can be used to type the Shiny app pytest fixtures.
25-
* `shiny.pytest` contains pytest test fixtures. The `local_app` pytest fixture is automatically available and runs a sibling `app.py` file. Where as `create_app_fixture(PATH_TO_APP)` allows for a Shiny app to be instantiated from a different folder.
25+
* `shiny.pytest` contains pytest test fixtures. The `local_app` pytest fixture is automatically available and runs a sibling `app.py` file. Where as `create_app_fixture(PATH_TO_APP)` allows for a relative path to a Shiny app to be instantiated from a different folder.
2626

2727
* Added CLI command `shiny add test` to add a test file to an existing Shiny app. (#1461)
2828

shiny/pytest/_fixture.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from pathlib import PurePath
3+
from pathlib import Path, PurePath
44
from typing import Literal, Union
55

66
import pytest
@@ -29,13 +29,22 @@ def create_app_fixture(
2929
----------
3030
app
3131
The path to the Shiny app file.
32+
33+
If `app` is a `Path` or `PurePath` instance and `Path(app).is_file()` returns `True`, then this value will be used directly.
34+
Note, `app`'s file path will be checked from where corresponding `pytest` test is collected, not necessarily where `create_app_fixture()` is called.
35+
36+
Otherwise, all `app` paths will be considered to be relative paths from where the test function was collected.
37+
38+
To be sure that your app path is always relative, supply a `str` value.
3239
scope
3340
The scope of the fixture.
3441
"""
3542

3643
@pytest.fixture(scope=scope)
37-
def fixture_func():
38-
sa_gen = shiny_app_gen(app)
44+
def fixture_func(request: pytest.FixtureRequest):
45+
app_purepath_exists = isinstance(app, PurePath) and Path(app).is_file()
46+
app_path = app if app_purepath_exists else request.path.parent / app
47+
sa_gen = shiny_app_gen(app_path)
3948
yield next(sa_gen)
4049

4150
return fixture_func

0 commit comments

Comments
 (0)