Skip to content

Commit 8613e7d

Browse files
Merge pull request #4 from blackary/parameterize-test
Parameterize test to separate out test on each page, and give each test auto-generated name
2 parents 7f0bc3e + 1525209 commit 8613e7d

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

st_smoke_test.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
import os
2-
import glob
32
import unittest
3+
from pathlib import Path
4+
45
from streamlit.testing.v1 import AppTest
56

67
APP_PATH = os.getenv("APP_PATH", default="streamlit_app.py")
7-
SKIP_SMOKE = os.getenv("SKIP_SMOKE", 'False').lower() in ('true', '1', 't')
8+
SKIP_SMOKE = os.getenv("SKIP_SMOKE", "False").lower() in ("true", "1", "t")
9+
10+
11+
def get_file_paths() -> list[str]:
12+
"""Get a list of file paths for the main page + each page in the pages folder."""
13+
page_folder = Path(APP_PATH).parent / "pages"
14+
if not page_folder.exists():
15+
return [APP_PATH]
16+
page_files = page_folder.glob("*.py")
17+
file_paths = [str(file.absolute().resolve()) for file in page_files]
18+
return [APP_PATH] + file_paths
19+
20+
21+
def pytest_generate_tests(metafunc):
22+
"""
23+
This is a special function that is called automatically by pytest to generate tests.
24+
https://docs.pytest.org/en/7.1.x/how-to/parametrize.html#pytest-generate-tests
25+
26+
This generates list of file paths for each page in the pages folder, which will
27+
automatically be used if a test function has an argument called "file_path".
28+
29+
Each file path will be the absolute path to each file, but the test ids will be
30+
just the file name. This is so that the test output is easier to read.
31+
32+
st_smoke_test.py::test_smoke_page[streamlit_app.py] PASSED [ 33%]
33+
st_smoke_test.py::test_smoke_page[p1.py] PASSED [ 66%]
34+
st_smoke_test.py::test_smoke_page[p2.py] PASSED [100%]
35+
"""
36+
if "file_path" in metafunc.fixturenames:
37+
metafunc.parametrize(
38+
"file_path", get_file_paths(), ids=lambda x: x.split("/")[-1]
39+
)
840

9-
@unittest.skipIf(SKIP_SMOKE, "smoke test is disabled by config")
10-
def test_smoke_main():
11-
at = AppTest.from_file(APP_PATH, default_timeout = 100).run()
12-
assert not at.exception
1341

1442
@unittest.skipIf(SKIP_SMOKE, "smoke test is disabled by config")
15-
def test_smoke_pages():
16-
pages_pattern = os.path.join(os.path.dirname(APP_PATH), "pages/*.py")
17-
page_files = glob.glob(pages_pattern)
18-
if not page_files:
19-
raise unittest.SkipTest("No pages found")
20-
for file in page_files:
21-
file_path = os.path.abspath(file)
22-
at = AppTest.from_file(file_path, default_timeout = 100).run()
23-
assert not at.exception
43+
def test_smoke_page(file_path):
44+
"""
45+
This will run a basic test on each page in the pages folder, checking to see that
46+
there are no exceptions raised while the app runs.
47+
"""
48+
at = AppTest.from_file(file_path, default_timeout=100).run()
49+
assert not at.exception

0 commit comments

Comments
 (0)