From 4c5ad461b3fe77635871c40de7f580eec9a322d1 Mon Sep 17 00:00:00 2001 From: Filippo Carra <2720589+karrukola@users.noreply.github.com> Date: Sat, 1 Mar 2025 17:24:35 +0100 Subject: [PATCH 1/3] feat: add feature to pad iteration number Signed-off-by: Filippo Carra <2720589+karrukola@users.noreply.github.com> --- README.rst | 33 +++++++++++++++++++++++++++++++++ pytest_repeat.py | 22 ++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 2ebe9800..9d52480c 100644 --- a/README.rst +++ b/README.rst @@ -88,6 +88,39 @@ For example: This will attempt to run test_file.py 1000 times, but will stop as soon as a failure occurs. +Padding test iteration count +---------------------------- + +Use the :code:`--padding` command line option to specify the character to be used +to pad the test iteration number, i.e. by prepending a :code:`0` or a space. +This feature helps sorting the results in a generated report, i.e. the one made by +pytest-html. + +.. code-block:: bash + + $ pytest --count=10 --padding=" " test_file.py + + ============================ test session starts ============================ + [...] + test_file.py .......... [100%] + + ================================== PASSES =================================== + ========================== short test summary info ========================== + PASSED test_file.py::test_do_nothing[ 1-10] + PASSED test_file.py::test_do_nothing[ 2-10] + PASSED test_file.py::test_do_nothing[ 3-10] + PASSED test_file.py::test_do_nothing[ 4-10] + PASSED test_file.py::test_do_nothing[ 5-10] + PASSED test_file.py::test_do_nothing[ 6-10] + PASSED test_file.py::test_do_nothing[ 7-10] + PASSED test_file.py::test_do_nothing[ 8-10] + PASSED test_file.py::test_do_nothing[ 9-10] + PASSED test_file.py::test_do_nothing[10-10] + ============================ 10 passed in 0.02s ============================= + +If more than one character is given, the setting is ignored. + + UnitTest Style Tests -------------------- diff --git a/pytest_repeat.py b/pytest_repeat.py index 008b692b..be23241b 100644 --- a/pytest_repeat.py +++ b/pytest_repeat.py @@ -23,6 +23,14 @@ def pytest_addoption(parser): choices=('function', 'class', 'module', 'session'), help='Scope for repeating tests') + parser.addoption( + '--padding', + action='store', + type=str, + default=None, + help='Character to be used for padding and aligning test repetions' + ) + def pytest_configure(config): config.addinivalue_line( @@ -60,8 +68,18 @@ def pytest_generate_tests(metafunc): if count > 1: metafunc.fixturenames.append("__pytest_repeat_step_number") - def make_progress_id(i, n=count): - return '{0}-{1}'.format(i + 1, n) + pad_char = metafunc.config.option.padding + if pad_char is not None and len(pad_char) != 1: + warnings.warn("Padding should be by a character only") + if pad_char is not None and len(pad_char) == 1: + n_digits = len(str(count)) + + def make_progress_id(i, n=count): + return f"{i + 1:{pad_char}>{n_digits}}-{n}" + else: + + def make_progress_id(i, n=count): + return '{0}-{1}'.format(i + 1, n) scope = metafunc.config.option.repeat_scope metafunc.parametrize( From 10e09e2cd8058429e33a6a6f353d7be6f2e67e5e Mon Sep 17 00:00:00 2001 From: Filippo Carra <2720589+karrukola@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:38:13 +0100 Subject: [PATCH 2/3] fix: correct option name Signed-off-by: Filippo Carra <2720589+karrukola@users.noreply.github.com> --- README.rst | 2 +- pytest_repeat.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 9d52480c..fc83a3b6 100644 --- a/README.rst +++ b/README.rst @@ -98,7 +98,7 @@ pytest-html. .. code-block:: bash - $ pytest --count=10 --padding=" " test_file.py + $ pytest --count=10 --repeat-pad=" " test_file.py ============================ test session starts ============================ [...] diff --git a/pytest_repeat.py b/pytest_repeat.py index be23241b..3980282c 100644 --- a/pytest_repeat.py +++ b/pytest_repeat.py @@ -24,7 +24,7 @@ def pytest_addoption(parser): help='Scope for repeating tests') parser.addoption( - '--padding', + '--repeat-pad', action='store', type=str, default=None, @@ -68,7 +68,7 @@ def pytest_generate_tests(metafunc): if count > 1: metafunc.fixturenames.append("__pytest_repeat_step_number") - pad_char = metafunc.config.option.padding + pad_char = metafunc.config.option.repeat_pad if pad_char is not None and len(pad_char) != 1: warnings.warn("Padding should be by a character only") if pad_char is not None and len(pad_char) == 1: From ea2e687c0b6b6539df5a8e231b2beeac284edfbb Mon Sep 17 00:00:00 2001 From: Filippo Carra <2720589+karrukola@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:42:55 +0100 Subject: [PATCH 3/3] docs: fix typo Signed-off-by: Filippo Carra <2720589+karrukola@users.noreply.github.com> --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index fc83a3b6..9bab580b 100644 --- a/README.rst +++ b/README.rst @@ -91,7 +91,7 @@ occurs. Padding test iteration count ---------------------------- -Use the :code:`--padding` command line option to specify the character to be used +Use the :code:`--repeat-pad` command line option to specify the character to be used to pad the test iteration number, i.e. by prepending a :code:`0` or a space. This feature helps sorting the results in a generated report, i.e. the one made by pytest-html.