Skip to content

Commit 6668b12

Browse files
committed
options: add --suite-iterate option
Add --suite-iterate|-I option to execute testing suites multiple times. The --skip-file option was using -I so we replaced it with -K. For continuity we also changed --skip-file into -k. Reviewed-by: Petr Vorel <[email protected]> Signed-off-by: Andrea Cervesato <[email protected]>
1 parent 3315f2d commit 6668b12

File tree

5 files changed

+112
-12
lines changed

5 files changed

+112
-12
lines changed

libkirk/data.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def name(self):
3737
"""
3838
return self._name
3939

40+
@name.setter
41+
def name(self, value: str):
42+
"""
43+
Set the suite name.
44+
"""
45+
if not value:
46+
raise ValueError("empty suite name")
47+
48+
self._name = value
49+
4050
@property
4151
def tests(self):
4252
"""

libkirk/main.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ def _env_config(value: str) -> dict:
130130
return config
131131

132132

133+
def _iterate_config(value: str) -> int:
134+
"""
135+
Return the iterate value.
136+
"""
137+
if not value:
138+
return 1
139+
140+
ret = 1
141+
try:
142+
ret = int(value)
143+
except TypeError as err:
144+
raise argparse.ArgumentTypeError("Invalid number") from err
145+
146+
if ret <= 1:
147+
return 1
148+
149+
return ret
150+
151+
133152
def _discover_sut(path: str) -> None:
134153
"""
135154
Discover new SUT implementations.
@@ -315,6 +334,7 @@ async def session_run() -> None:
315334
pattern=run_pattern,
316335
report_path=args.json_report,
317336
restore=restore_dir,
337+
suite_iterate=args.suite_iterate,
318338
)
319339
except asyncio.CancelledError:
320340
await session.stop()
@@ -396,12 +416,12 @@ def run(cmd_args: list = None) -> None:
396416
help="List of key=value environment values separated by ':'")
397417
parser.add_argument(
398418
"--skip-tests",
399-
"-i",
419+
"-k",
400420
type=str,
401421
help="Skip specific tests")
402422
parser.add_argument(
403423
"--skip-file",
404-
"-I",
424+
"-K",
405425
type=str,
406426
help="Skip specific tests using a skip file (newline separated item)")
407427
parser.add_argument(
@@ -416,6 +436,12 @@ def run(cmd_args: list = None) -> None:
416436
type=int,
417437
default=3600,
418438
help="Timeout before stopping a single execution")
439+
parser.add_argument(
440+
"--suite-iterate",
441+
"-I",
442+
type=_iterate_config,
443+
default=1,
444+
help="Number of times to repeat testing suites")
419445

420446
# tests execution arguments
421447
parser.add_argument(

libkirk/session.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import os
99
import re
10+
import copy
1011
import logging
1112
import asyncio
1213
import libkirk
@@ -208,6 +209,23 @@ async def _get_suites_objects(self, names: list) -> list:
208209

209210
return suites_obj
210211

212+
@staticmethod
213+
def _apply_iterate(suites_obj: list, suite_iterate: int) -> list:
214+
"""
215+
Return testing suites after applying iterate parameters.
216+
"""
217+
if suite_iterate <= 1:
218+
return suites_obj
219+
220+
suites_list = []
221+
for suite in suites_obj:
222+
for i in range(0, suite_iterate):
223+
obj = copy.deepcopy(suite)
224+
obj.name = f"{suite.name}[{i}]"
225+
suites_list.append(obj)
226+
227+
return suites_list
228+
211229
async def _read_suites(
212230
self,
213231
suites: list,
@@ -319,15 +337,7 @@ async def stop(self) -> None:
319337
await libkirk.events.fire("session_stopped")
320338
self._stop = False
321339

322-
# consider changing the arguments handling if new ones must be added
323-
# pylint: disable=too-many-positional-arguments
324-
async def run(
325-
self,
326-
command: str = None,
327-
suites: list = None,
328-
pattern: str = None,
329-
report_path: str = None,
330-
restore: str = None) -> None:
340+
async def run(self, **kwargs: dict) -> None:
331341
"""
332342
Run a new session and store results inside a JSON file.
333343
:param command: single command to run before suites
@@ -340,7 +350,16 @@ async def run(
340350
:type report_path: str
341351
:param restore: temporary directory generated by a previous session
342352
:type restore: str
353+
:param suite_iterate: execute all suites multiple times
354+
:type suite_iterate: int
343355
"""
356+
command = kwargs.get("command", None)
357+
suites = kwargs.get("suites", None)
358+
pattern = kwargs.get("pattern", None)
359+
report_path = kwargs.get("report_path", None)
360+
restore = kwargs.get("restore", None)
361+
suite_iterate = kwargs.get("suite_iterate", 1)
362+
344363
async with self._run_lock:
345364
await libkirk.events.fire("session_started", self._tmpdir.abspath)
346365

@@ -358,6 +377,10 @@ async def run(
358377
if suites:
359378
suites_obj = await self._read_suites(
360379
suites, pattern, restore)
380+
381+
suites_obj = self._apply_iterate(
382+
suites_obj, suite_iterate)
383+
361384
await self._scheduler.schedule(suites_obj)
362385
except KirkException as err:
363386
if not self._stop:

libkirk/tests/test_main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_run_command(self, tmpdir):
6767

6868
assert excinfo.value.code == libkirk.main.RC_OK
6969

70-
@pytest.mark.skipif(sys.version_info < (3,8), reason="requires python3.8+")
70+
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8+")
7171
def test_run_command_timeout(self, tmpdir):
7272
"""
7373
Test --run-command option with timeout.
@@ -362,3 +362,22 @@ def test_env(self, tmpdir):
362362

363363
report_d = self.read_report(temp, 1)
364364
assert report_d["results"][0]["test"]["log"] == "ciao"
365+
366+
def test_suite_iterate(self, tmpdir):
367+
"""
368+
Test --suite-iterate option.
369+
"""
370+
temp = tmpdir.mkdir("temp")
371+
cmd_args = [
372+
"--tmp-dir", str(temp),
373+
"--framework", "dummy",
374+
"--run-suite", "suite01",
375+
"--suite-iterate", "4",
376+
]
377+
378+
with pytest.raises(SystemExit) as excinfo:
379+
libkirk.main.run(cmd_args=cmd_args)
380+
381+
assert excinfo.value.code == libkirk.main.RC_OK
382+
383+
self.read_report(temp, 8)

libkirk/tests/test_session.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,25 @@ async def test_run_skip_tests(self, tmpdir, sut, dummy_framework):
125125
with open(report, "r") as report_file:
126126
report_data = json.loads(report_file.read())
127127
assert len(report_data["results"]) == 0
128+
129+
@pytest.mark.parametrize(
130+
"iterate,expect",
131+
[
132+
(0, 4),
133+
(1, 4),
134+
(3, 12),
135+
]
136+
)
137+
async def test_run_suite_iterate(self, tmpdir, session, iterate, expect):
138+
"""
139+
Test run method when executing a testing suite multiple times.
140+
"""
141+
report = str(tmpdir / "report.json")
142+
await session.run(
143+
suites=["suite01", "suite02"],
144+
suite_iterate=iterate,
145+
report_path=report)
146+
147+
with open(report, "r", encoding="utf-8") as report_file:
148+
report_data = json.loads(report_file.read())
149+
assert len(report_data["results"]) == expect

0 commit comments

Comments
 (0)