Skip to content

Commit

Permalink
options: add --randomize option
Browse files Browse the repository at this point in the history
Shuffle testing suites tests list when --randomize|-x option is defined.

Signed-off-by: Andrea Cervesato <[email protected]>
  • Loading branch information
acerv committed Feb 26, 2025
1 parent 09b4f06 commit c15c305
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libkirk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ async def session_run() -> None:
restore=restore_dir,
suite_iterate=args.suite_iterate,
skip_tests=skip_tests,
randomize=args.randomize,
)
except asyncio.CancelledError:
await session.stop()
Expand Down Expand Up @@ -468,6 +469,11 @@ def run(cmd_args: list = None) -> None:
"-p",
action="store_true",
help="Force parallelization execution of all tests")
parser.add_argument(
"--randomize",
"-x",
action="store_true",
help="Force parallelization execution of all tests")

# session arguments
parser.add_argument(
Expand Down
8 changes: 8 additions & 0 deletions libkirk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import re
import copy
import random
import logging
import asyncio
import libkirk
Expand Down Expand Up @@ -374,6 +375,8 @@ async def run(self, **kwargs: dict) -> None:
:type restore: str
:param suite_iterate: execute all suites multiple times
:type suite_iterate: int
:param randomize: randomize all tests if True
:type randomize: bool
"""
command = kwargs.get("command", None)
suites = kwargs.get("suites", None)
Expand All @@ -382,6 +385,7 @@ async def run(self, **kwargs: dict) -> None:
report_path = kwargs.get("report_path", None)
restore = kwargs.get("restore", None)
suite_iterate = kwargs.get("suite_iterate", 1)
randomize = kwargs.get("randomize", False)

async with self._run_lock:
await libkirk.events.fire("session_started", self._tmpdir.abspath)
Expand All @@ -404,6 +408,10 @@ async def run(self, **kwargs: dict) -> None:
suites_obj = self._apply_iterate(
suites_obj, suite_iterate)

if randomize:
for suite in suites_obj:
random.shuffle(suite.tests)

await self._scheduler.schedule(suites_obj)
except KirkException as err:
if not self._stop:
Expand Down
19 changes: 19 additions & 0 deletions libkirk/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,22 @@ def test_suite_iterate(self, tmpdir):
assert excinfo.value.code == libkirk.main.RC_OK

self.read_report(temp, 8)

def test_randomize(self, tmpdir):
"""
Test --randomize option.
"""
temp = tmpdir.mkdir("temp")
cmd_args = [
"--tmp-dir", str(temp),
"--framework", "dummy",
"--run-suite", "suite01",
"--randomize",
]

with pytest.raises(SystemExit) as excinfo:
libkirk.main.run(cmd_args=cmd_args)

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

self.read_report(temp, 2)
24 changes: 24 additions & 0 deletions libkirk/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,27 @@ async def test_run_suite_iterate(self, tmpdir, session, iterate, expect):
with open(report, "r", encoding="utf-8") as report_file:
report_data = json.loads(report_file.read())
assert len(report_data["results"]) == expect

async def test_run_randomize(self, tmpdir, session):
"""
Test run method when executing shuffled tests.
"""
num_of_suites = 5

report = str(tmpdir / "report.json")
await session.run(
suites=["suite01"] * num_of_suites,
randomize=True,
report_path=report)

report_data = None
with open(report, "r", encoding="utf-8") as report_file:
report_data = json.loads(report_file.read())

assert len(report_data["results"]) == 2 * num_of_suites

tests_names = []
for test in report_data["results"]:
tests_names.append(test["test_fqn"])

assert ["test01", "test02"] * num_of_suites != tests_names

0 comments on commit c15c305

Please sign in to comment.