Skip to content

Commit a68a7b3

Browse files
authored
Parse complete numeric suffixes for order marker prefixes (#269)
* Fix complete numeric suffix parsing for marker prefixes * Accept a neg placeholder for negative marker prefix indices
1 parent e729615 commit a68a7b3

4 files changed

Lines changed: 188 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# pytest-order Release Notes
22

3+
## Unreleased
4+
5+
### Fixes
6+
* `--order-marker-prefix` uses the complete marker suffix as the order index,
7+
so multi-digit and negative indices work; negative indices can be written
8+
as `neg` (for example `m_neg1` for -1)
9+
310
## [Version 1.5.0](https://pypi.org/project/pytest-order/1.5.0/) (2026-06-13)
411
Adds option to abort tests after a failed ordering.
512

‎docs/source/configuration.rst‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,29 @@ gives you now::
494494
test_module.py:18: test_c PASSED
495495
test_module.py:6: test_a PASSED
496496

497+
The complete suffix after the prefix is used as the integer order index. For
498+
example, ``m11`` has index 11. A marker without an integer suffix is ignored.
499+
An explicit ``order`` marker takes precedence.
500+
501+
A minus sign cannot be written in ``@pytest.mark.<name>``, so for a negative
502+
index write ``neg`` in its place, optionally preceded by an underscore:
503+
``m_neg1`` (or ``mneg1``) has index -1. You can also use the minus sign itself
504+
by looking the marker up by name:
505+
506+
.. code:: python
507+
508+
import pytest
509+
510+
511+
@pytest.mark.m_neg1
512+
def test_last():
513+
assert True
514+
515+
516+
@getattr(pytest.mark, "m-2")
517+
def test_second_to_last():
518+
assert True
519+
497520
.. note::
498521
As usually, you are responsible for registering your own markers, either in the
499522
code or in the ``pytest.ini`` file. If you forget this, pytest will give you warnings about unknown markers.

‎src/pytest_order/sorter.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ def mark_binning(
113113
if not has_order and self.settings.marker_prefix:
114114
for key in keys:
115115
if key.startswith(self.settings.marker_prefix):
116+
suffix = key[len(self.settings.marker_prefix) :]
117+
negative = re.fullmatch(r"_?neg(\d+)", suffix)
116118
try:
117-
index = int(key[len(self.settings.marker_prefix)])
119+
index = -int(negative[1]) if negative else int(suffix)
118120
item.order = index
119121
except ValueError:
120122
pass

‎tests/test_marker_prefix.py‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,158 @@ def test_c():
161161
"test_marker.py::test_a PASSED",
162162
]
163163
)
164+
165+
166+
@pytest.mark.parametrize(
167+
"suffix, expected",
168+
[
169+
("11", ["test_b", "test_a"]),
170+
("12", ["test_b", "test_a"]),
171+
("", ["test_b", "test_a"]),
172+
("1special", ["test_b", "test_a"]),
173+
("other", ["test_b", "test_a"]),
174+
("0", ["test_a", "test_b"]),
175+
],
176+
)
177+
def test_marker_prefix_uses_complete_numeric_suffix(test_path, suffix, expected):
178+
test_path.makeini(
179+
f"""
180+
[pytest]
181+
console_output_style = classic
182+
markers =
183+
my{suffix}: custom order
184+
my2: custom order
185+
"""
186+
)
187+
test_path.makepyfile(
188+
test_marker=f"""
189+
import pytest
190+
191+
@getattr(pytest.mark, "my{suffix}")
192+
def test_a():
193+
pass
194+
195+
@pytest.mark.my2
196+
def test_b():
197+
pass
198+
"""
199+
)
200+
result = test_path.runpytest("-v", "--order-marker-prefix=my")
201+
result.assert_outcomes(passed=2)
202+
result.stdout.fnmatch_lines([f"test_marker.py::{name} PASSED" for name in expected])
203+
204+
205+
def test_marker_prefix_supports_negative_indices(test_path):
206+
test_path.makeini(
207+
"""
208+
[pytest]
209+
console_output_style = classic
210+
markers =
211+
my-1: custom order
212+
my-2: custom order
213+
my2: custom order
214+
"""
215+
)
216+
test_path.makepyfile(
217+
test_marker="""
218+
import pytest
219+
220+
@getattr(pytest.mark, "my-1")
221+
def test_a():
222+
pass
223+
224+
@getattr(pytest.mark, "my-2")
225+
def test_b():
226+
pass
227+
228+
def test_c():
229+
pass
230+
231+
@pytest.mark.my2
232+
def test_d():
233+
pass
234+
"""
235+
)
236+
result = test_path.runpytest("-v", "--order-marker-prefix=my")
237+
result.assert_outcomes(passed=4)
238+
result.stdout.fnmatch_lines(
239+
[
240+
"test_marker.py::test_d PASSED",
241+
"test_marker.py::test_c PASSED",
242+
"test_marker.py::test_b PASSED",
243+
"test_marker.py::test_a PASSED",
244+
]
245+
)
246+
247+
248+
@pytest.mark.parametrize(
249+
"suffix, expected",
250+
[
251+
("_neg1", ["test_c", "test_b", "test_a"]),
252+
("neg1", ["test_c", "test_b", "test_a"]),
253+
("_neg", ["test_c", "test_a", "test_b"]),
254+
("neg-1", ["test_c", "test_a", "test_b"]),
255+
("_negative1", ["test_c", "test_a", "test_b"]),
256+
],
257+
)
258+
def test_marker_prefix_supports_neg_placeholder(test_path, suffix, expected):
259+
test_path.makeini(
260+
f"""
261+
[pytest]
262+
console_output_style = classic
263+
markers =
264+
my{suffix}: custom order
265+
my2: custom order
266+
"""
267+
)
268+
test_path.makepyfile(
269+
test_marker=f"""
270+
import pytest
271+
272+
@getattr(pytest.mark, "my{suffix}")
273+
def test_a():
274+
pass
275+
276+
def test_b():
277+
pass
278+
279+
@pytest.mark.my2
280+
def test_c():
281+
pass
282+
"""
283+
)
284+
result = test_path.runpytest("-v", "--order-marker-prefix=my")
285+
result.assert_outcomes(passed=3)
286+
result.stdout.fnmatch_lines([f"test_marker.py::{name} PASSED" for name in expected])
287+
288+
289+
@pytest.mark.parametrize("suffix", ["", "0", "11", "1special", "-1", "_neg1"])
290+
def test_explicit_order_overrides_marker_prefix(test_path, suffix):
291+
test_path.makeini(
292+
f"""
293+
[pytest]
294+
console_output_style = classic
295+
markers =
296+
my{suffix}: custom order
297+
my2: custom order
298+
"""
299+
)
300+
test_path.makepyfile(
301+
test_marker=f"""
302+
import pytest
303+
304+
@pytest.mark.order(3)
305+
@getattr(pytest.mark, "my{suffix}")
306+
def test_a():
307+
pass
308+
309+
@pytest.mark.my2
310+
def test_b():
311+
pass
312+
"""
313+
)
314+
result = test_path.runpytest("-v", "--order-marker-prefix=my")
315+
result.assert_outcomes(passed=2)
316+
result.stdout.fnmatch_lines(
317+
["test_marker.py::test_b PASSED", "test_marker.py::test_a PASSED"]
318+
)

0 commit comments

Comments
 (0)