Skip to content

Commit 021ab1f

Browse files
committed
test(pytest): extra parameterized examples
1 parent 5f4de22 commit 021ab1f

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

tests/test_examples.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,29 @@
77

88

99
def pytest_generate_tests(metafunc):
10-
if "example_name" in metafunc.fixturenames:
11-
metafunc.parametrize("example_name", [item.name for item in EXAMPLE_DIR.glob("*") if item.is_dir()])
10+
if "func" in metafunc.fixturenames:
11+
targets = []
12+
ids = []
1213

14+
for example_dir in EXAMPLE_DIR.glob("*"):
15+
if not example_dir.is_dir():
16+
continue
1317

14-
def test_example(example_name: str):
15-
test_module = importlib.import_module(f"examples.{example_name}.test")
16-
test_functions = inspect.getmembers(test_module, lambda x: inspect.isfunction(x) and x.__name__.startswith("test_"))
18+
for test_file in example_dir.glob("test*.py"):
19+
test_module = importlib.import_module(
20+
test_file.relative_to(PROJECT_DIR).with_suffix("").as_posix().replace("/", ".")
21+
)
22+
test_functions = inspect.getmembers(
23+
test_module,
24+
lambda x: inspect.isfunction(x) and x.__name__.startswith("test_"),
25+
)
1726

18-
for _name, func in test_functions:
19-
func()
27+
for name, func in test_functions:
28+
targets.append(func)
29+
ids.append(f"{test_file.relative_to(EXAMPLE_DIR).with_suffix('').as_posix()}/{name}")
30+
31+
metafunc.parametrize("func", targets, ids=ids)
32+
33+
34+
def test_example(func):
35+
func()

0 commit comments

Comments
 (0)