Skip to content

Commit 87dcf33

Browse files
committed
test group generator changed.
test group template added to project templates.
1 parent c0f8795 commit 87dcf33

4 files changed

Lines changed: 57 additions & 29 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "py-micro-hil"
7-
version = "0.1.5"
7+
version = "0.1.6"
88
description = "Hardware-in-the-Loop testing framework"
99
readme = "README.md"
1010
requires-python = ">=3.8"
@@ -47,7 +47,8 @@ where = ["src"]
4747
[tool.setuptools.package-data]
4848
py_micro_hil = [
4949
"templates/*.html",
50-
"templates/*.css"
50+
"templates/*.css",
51+
"templates/*.py"
5152
]
5253

5354
# Tools configuration

src/py_micro_hil/cli.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import importlib.metadata
44
import importlib.util
5+
from importlib.resources import files
56
from pathlib import Path
67
import argparse
78

@@ -164,28 +165,20 @@ def parse_args():
164165
return args
165166

166167

167-
def get_template_path():
168-
"""Return the path to the test group template file."""
169-
project_root = Path(__file__).resolve().parents[2]
170-
return project_root / "example" / "test_group_template.py"
168+
169+
def get_template_content() -> str:
170+
"""Load template file text from the installed package."""
171+
resource = files("py_micro_hil.templates") / "test_group_template.py"
172+
return resource.read_text()
171173

172174

173175
def create_test_group_file(group_name: str, target_dir: Path, logger: Logger) -> bool:
174176
"""
175177
Create a new test group file from the template.
176-
177178
Returns True on success, False otherwise.
178179
"""
179180

180-
template_path = get_template_path()
181-
if not template_path.exists():
182-
logger.log(
183-
f"[ERROR] ❌ Template file not found at '{template_path}'.",
184-
to_console=True,
185-
to_log_file=True,
186-
)
187-
return False
188-
181+
# ensure target directory exists
189182
if not target_dir.exists():
190183
logger.log(
191184
f"[ERROR] ❌ Test directory '{target_dir}' does not exist.",
@@ -195,6 +188,8 @@ def create_test_group_file(group_name: str, target_dir: Path, logger: Logger) ->
195188
return False
196189

197190
destination = target_dir / f"test_{group_name}.py"
191+
192+
# prevent overwriting
198193
if destination.exists():
199194
logger.log(
200195
f"[ERROR] ❌ File '{destination}' already exists. Aborting creation.",
@@ -204,10 +199,16 @@ def create_test_group_file(group_name: str, target_dir: Path, logger: Logger) ->
204199
return False
205200

206201
try:
207-
template_content = template_path.read_text()
208-
rendered_content = template_content.format(test_group_name=group_name)
209-
destination.write_text(rendered_content)
210-
except Exception as exc: # pragma: no cover - defensive
202+
# load template content from package (installation-safe)
203+
template_content = get_template_content()
204+
205+
# apply formatting
206+
rendered = template_content.format(test_group_name=group_name)
207+
208+
# write final file
209+
destination.write_text(rendered)
210+
211+
except Exception as exc:
211212
logger.log(
212213
f"[ERROR] ❌ Failed to create test group file: {exc}",
213214
to_console=True,
@@ -223,6 +224,7 @@ def create_test_group_file(group_name: str, target_dir: Path, logger: Logger) ->
223224
return True
224225

225226

227+
226228
def load_test_groups(test_directory, logger, strict_imports: bool = False):
227229
"""Dynamically loads test groups from test modules in a specified directory."""
228230
test_groups = []
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from py_micro_hil.assertions import *
2+
from py_micro_hil.framework_API import *
3+
4+
def setup_group():
5+
TEST_INFO_MESSAGE("Setting up {test_group_name}")
6+
7+
def teardown_group():
8+
TEST_INFO_MESSAGE("Tearing down {test_group_name}")
9+
10+
def test_template_test():
11+
TEST_ASSERT_EQUAL(0,0)

tests/test_cli_runner.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ def raise_not_found(*args, **kwargs):
8383

8484

8585
def test_create_test_group_file_success(tmp_path, monkeypatch):
86-
template = tmp_path / "template.py"
87-
template.write_text("# template\nclass {test_group_name}: pass\n")
8886
target_dir = tmp_path / "hil_tests"
8987
target_dir.mkdir()
9088

91-
monkeypatch.setattr(cli, "get_template_path", lambda: template)
89+
# mock template content
90+
monkeypatch.setattr(
91+
cli,
92+
"get_template_content",
93+
lambda: "# template\nclass {test_group_name}: pass\n",
94+
)
95+
9296
logger = DummyLogger()
9397

9498
created = cli.create_test_group_file("alpha", target_dir, logger)
@@ -100,15 +104,20 @@ def test_create_test_group_file_success(tmp_path, monkeypatch):
100104
assert any("Created test group template" in msg for msg in logger.messages)
101105

102106

107+
103108
def test_create_test_group_file_existing(tmp_path, monkeypatch):
104-
template = tmp_path / "template.py"
105-
template.write_text("# template\nclass {test_group_name}: pass\n")
109+
monkeypatch.setattr(
110+
cli,
111+
"get_template_content",
112+
lambda: "# template\nclass {test_group_name}: pass\n",
113+
)
114+
106115
target_dir = tmp_path / "hil_tests"
107116
target_dir.mkdir()
117+
108118
existing = target_dir / "test_alpha.py"
109119
existing.write_text("# existing")
110120

111-
monkeypatch.setattr(cli, "get_template_path", lambda: template)
112121
logger = DummyLogger()
113122

114123
created = cli.create_test_group_file("alpha", target_dir, logger)
@@ -117,12 +126,16 @@ def test_create_test_group_file_existing(tmp_path, monkeypatch):
117126
assert any("already exists" in msg for msg in logger.messages)
118127

119128

129+
120130
def test_create_test_group_file_missing_dir(tmp_path, monkeypatch):
121-
template = tmp_path / "template.py"
122-
template.write_text("# template\nclass {test_group_name}: pass\n")
131+
monkeypatch.setattr(
132+
cli,
133+
"get_template_content",
134+
lambda: "# template\nclass {test_group_name}: pass\n",
135+
)
136+
123137
target_dir = tmp_path / "missing"
124138

125-
monkeypatch.setattr(cli, "get_template_path", lambda: template)
126139
logger = DummyLogger()
127140

128141
created = cli.create_test_group_file("alpha", target_dir, logger)
@@ -131,6 +144,7 @@ def test_create_test_group_file_missing_dir(tmp_path, monkeypatch):
131144
assert any("does not exist" in msg for msg in logger.messages)
132145

133146

147+
134148
# ---------------------------------------------------------------------
135149
# log_discovered_devices
136150
# ---------------------------------------------------------------------

0 commit comments

Comments
 (0)