Skip to content

Commit c467853

Browse files
committed
Add option --order-scope-level to set the scope at directory level
- defines a scope based on directory depth, between session and module scope - closes #8
1 parent fd9f5d7 commit c467853

File tree

14 files changed

+359
-12
lines changed

14 files changed

+359
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
### New features
66
- added support for class level relative markers,
77
see [#7](https://github.com/pytest-dev/pytest-order/issues/7)
8+
- added option `--order-scope-level` which allows to groups tests on the
9+
same directory level,
10+
see [#8](https://github.com/pytest-dev/pytest-order/issues/8)
811

912
### Fixes
1013
- fixed sorting of dependency markers that depend on an item with the same

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ Features
7878
- ordering of tests relative to each other (via the `before` and `after`
7979
marker attributes)
8080
- session-, module- and class-scope ordering via the ``order-scope`` option
81-
- hierarchical ordering via the ``group-order-scope`` option
81+
- directory scope ordering via the ``order-scope-level`` option
82+
- hierarchical module and class-level ordering via the ``group-order-scope``
83+
option
8284
- ordering tests with `pytest-dependency` markers if using the
8385
``order-dependencies`` option
8486
- sparse ordering of tests via the ``sparse-ordering`` option
85-
- invocation of the plugin before other plugins if the
86-
``indulgent-ordering`` option is used
8787

8888
A usage guide for each feature can be
8989
found in the [documentation](https://pytest-dev.github.io/pytest-order/dev/).

docs/source/index.rst

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,120 @@ Here is what you get using session and module-scoped sorting:
556556
tests/test_module2.py:5: test2 PASSED
557557

558558

559+
``--order-scope-level``
560+
-----------------------
561+
This is an alternative option to define the order scope. It defines the
562+
directory level which is used as the order scope, counting from the root
563+
directory. The resulting scope is between the session and module
564+
scopes defined via ``--order-scope``, where ``--order-scope-level=0`` is the
565+
same as session scope, while setting the level to the number of test
566+
directory levels would result in module scope.
567+
568+
Consider the following directory structure:
569+
570+
::
571+
572+
order_scope_level
573+
feature1
574+
__init__.py
575+
test_a.py
576+
test_b.py
577+
feature2
578+
__init__.py
579+
test_a.py
580+
test_b.py
581+
582+
with the test contents:
583+
584+
**test_a.py**:
585+
586+
.. code::
587+
588+
import pytest
589+
590+
@pytest.mark.order(4)
591+
def test_four():
592+
pass
593+
594+
@pytest.mark.order(3)
595+
def test_three():
596+
pass
597+
598+
**test_b.py**:
599+
600+
.. code::
601+
602+
import pytest
603+
604+
@pytest.mark.order(2)
605+
def test_two():
606+
pass
607+
608+
@pytest.mark.order(1)
609+
def test_one():
610+
pass
611+
612+
The idea here is to test each feature separately, while ordering the tests
613+
across the test modules for each feature.
614+
615+
If we use session scope, we get:
616+
617+
::
618+
619+
$ pytest -v order_scope_level
620+
============================= test session starts ==============================
621+
...
622+
623+
order_scope_level/feature1/test_a.py::test_one PASSED
624+
order_scope_level/feature2/test_a.py::test_one PASSED
625+
order_scope_level/feature1/test_a.py::test_two PASSED
626+
order_scope_level/feature2/test_a.py::test_two PASSED
627+
order_scope_level/feature1/test_b.py::test_three PASSED
628+
order_scope_level/feature2/test_b.py::test_three PASSED
629+
order_scope_level/feature1/test_b.py::test_four PASSED
630+
order_scope_level/feature2/test_b.py::test_four PASSED
631+
632+
which mixes the features.
633+
634+
Using module scope instead separates the features, but does not order the
635+
modules as wanted:
636+
637+
::
638+
639+
$ pytest -v --order-scope=module order_scope_level
640+
============================= test session starts ==============================
641+
...
642+
643+
order_scope_level/feature1/test_a.py::test_three PASSED
644+
order_scope_level/feature1/test_a.py::test_four PASSED
645+
order_scope_level/feature1/test_b.py::test_one PASSED
646+
order_scope_level/feature1/test_b.py::test_two PASSED
647+
order_scope_level/feature2/test_a.py::test_three PASSED
648+
order_scope_level/feature2/test_a.py::test_four PASSED
649+
order_scope_level/feature2/test_b.py::test_one PASSED
650+
order_scope_level/feature2/test_b.py::test_two PASSED
651+
652+
To get the wanted behavior, we can use ``--order-scope-level=2``, which keeps
653+
the first two directory levels:
654+
655+
::
656+
657+
$ pytest tests -v --order-scope-level=2 order_scope_level
658+
============================= test session starts ==============================
659+
...
660+
661+
order_scope_level/feature1/test_b.py::test_one PASSED
662+
order_scope_level/feature1/test_b.py::test_two PASSED
663+
order_scope_level/feature1/test_a.py::test_three PASSED
664+
order_scope_level/feature1/test_a.py::test_four PASSED
665+
order_scope_level/feature2/test_b.py::test_one PASSED
666+
order_scope_level/feature2/test_b.py::test_two PASSED
667+
order_scope_level/feature2/test_a.py::test_three PASSED
668+
order_scope_level/feature2/test_a.py::test_four PASSED
669+
670+
Note that using a level of 0 or 1 would cause the same result as session
671+
scope, and any level greater than 2 would emulate module scope.
672+
559673
``--order-group-scope``
560674
-----------------------
561675
This option is also related to the order scope. It defines the scope inside

example/order_scope_level/feature1/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
4+
@pytest.mark.order(2)
5+
def test_two():
6+
pass
7+
8+
9+
@pytest.mark.order(1)
10+
def test_one():
11+
pass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
4+
@pytest.mark.order(4)
5+
def test_four():
6+
pass
7+
8+
9+
@pytest.mark.order(3)
10+
def test_three():
11+
pass

example/order_scope_level/feature2/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
4+
@pytest.mark.order(2)
5+
def test_two():
6+
pass
7+
8+
9+
@pytest.mark.order(1)
10+
def test_one():
11+
pass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
4+
@pytest.mark.order(4)
5+
def test_four():
6+
pass
7+
8+
9+
@pytest.mark.order(3)
10+
def test_three():
11+
pass

pytest_order/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def pytest_addoption(parser):
5151
help="Defines the scope used for ordering. Possible values"
5252
"are 'session' (default), 'module', and 'class'."
5353
"Ordering is only done inside a scope.")
54+
group.addoption("--order-scope-level", action="store", type=int,
55+
dest="order_scope_level",
56+
help="Defines that the given directory level is used as "
57+
"order scope. Cannot be used with --order-scope. "
58+
"The value is a number that defines the "
59+
"hierarchical index of the directories used as "
60+
"order scope, starting with 0 at session scope.")
5461
group.addoption("--order-group-scope", action="store",
5562
dest="order_group_scope",
5663
help="Defines the scope used for order groups. Possible "

0 commit comments

Comments
 (0)