### Install pytest-order using pip Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/intro.rst This command installs the latest released version of the pytest-order plugin from PyPI. Ensure you have pip installed and configured correctly. ```bash pip install pytest-order ``` -------------------------------- ### Install pytest-order from GitHub sources Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/intro.rst This command installs the latest version of pytest-order directly from its main branch on GitHub. This is useful for developers who want to use the most recent, potentially unstable, features. ```bash pip install git+https://github.com/pytest-dev/pytest-order ``` -------------------------------- ### Sparse Ordering in Pytest with pytest-order Source: https://context7.com/pytest-dev/pytest-order/llms.txt This example demonstrates sparse ordering, enabled by the '--sparse-ordering' option. It allows tests with explicit order markers to be grouped together, with unordered tests filling the gaps between them. ```python import pytest @pytest.mark.order(3) def test_two(): assert True def test_three(): assert True def test_four(): assert True @pytest.mark.order(1) def test_one(): assert True # When --sparse-ordering is enabled, the execution order will group ordered tests: # test_one (1) -> test_two (3) -> [unordered tests: test_three, test_four] -> potentially others if defined. ``` -------------------------------- ### Pytest Order Example with pytest-randomly Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/other_plugins.rst Demonstrates how pytest-order works alongside pytest-randomly. Tests marked with @pytest.mark.order(n) will be ordered correctly, while other tests may appear in a randomized sequence due to pytest-randomly's execution. ```python import pytest @pytest.mark.order(1) def test_second(): assert True def test_third(): assert True def test_fourth(): assert True @pytest.mark.order(0) def test_first(): assert True ``` -------------------------------- ### Multiple Order Markers for Flexible Test Sequencing in Pytest Source: https://context7.com/pytest-dev/pytest-order/llms.txt This example demonstrates using multiple @pytest.mark.order decorators on a single test function. This allows a test to be positioned at multiple points in the execution sequence, useful for complex scenarios or hooks. ```python import pytest @pytest.mark.order(1) @pytest.mark.order(-1) def test_one_and_seven(): pass @pytest.mark.order(2) @pytest.mark.order(-2) def test_two_and_six(): pass def test_four(): pass @pytest.mark.order(before="test_four") @pytest.mark.order(after="test_four") def test_three_and_five(): pass # Execution order: test_one_and_seven (1) -> test_two_and_six (2) -> test_three_and_five (before) -> test_four -> test_three_and_five (after) -> test_two_and_six (-2) -> test_one_and_seven (-1) ``` -------------------------------- ### Combined Absolute and Relative Test Ordering in Pytest Source: https://context7.com/pytest-dev/pytest-order/llms.txt This example demonstrates combining absolute ordinal indexing with relative positioning ('after') using the @pytest.mark.order decorator. It shows how the relative positioning can override or influence the absolute index. ```python import pytest @pytest.mark.order(index=0, after="test_second") def test_first(): assert True @pytest.mark.order(1) def test_second(): assert True # The 'after' relationship takes precedence over the index # Execution order: test_second (1) -> test_first (0, but after test_second) ``` -------------------------------- ### Combine Absolute and Relative Ordering with pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst This example shows how pytest-order handles a combination of absolute ordering (using 'index') and relative ordering (using 'after'). Relative ordering takes precedence, ensuring that 'test_second' is executed before 'test_first' even though 'test_first' has a lower absolute index. ```python import pytest @pytest.mark.order(index=0, after="test_second") def test_first(): assert True @pytest.mark.order(1) def test_second(): assert True ``` -------------------------------- ### Reference Tests in Other Classes or Modules with pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst This example shows how to reference tests located in different classes within the same module, or in entirely different modules. For same-module, different-class references, prepend the class name with '::'. For cross-module references, use the test's nodeid or a sufficiently unique part of it. ```python import pytest class TestA: @pytest.mark.order(after="TestB::test_c") def test_a(self): assert True def test_b(self): assert True class TestB: def test_c(self): assert True ``` ```python import pytest @pytest.mark.order(after="test_module_a.py::TestA::test_a") def test_a(): assert True @pytest.mark.order(before="test_module_c/test_submodule.py::test_2") def test_b(): assert True ``` -------------------------------- ### Order tests with directory scope level using pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst Illustrates how to use the @pytest.mark.order decorator to assign specific order numbers to tests. This example shows how --order-scope-level can be used to group tests by directory for ordered execution. ```python import pytest @pytest.mark.order(4) def test_four(): pass @pytest.mark.order(3) def test_three(): pass @pytest.mark.order(2) def test_two(): pass @pytest.mark.order(1) def test_one(): pass ``` -------------------------------- ### Basic Ordinal Test Ordering in Pytest Source: https://context7.com/pytest-dev/pytest-order/llms.txt This snippet demonstrates how to use the @pytest.mark.order decorator with integer indices to control the execution order of tests. Positive integers define order from the start, and negative integers from the end. This is a fundamental way to sequence tests. ```python import pytest @pytest.mark.order(2) def test_foo(): assert True @pytest.mark.order(1) def test_bar(): assert True # Execution order: test_bar (1) -> test_foo (2) ``` -------------------------------- ### Pytest Dependency Ordering with --order-dependencies Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst This demonstrates how to use the --order-dependencies option to ensure tests are run according to their defined dependencies when using the pytest-dependency plugin. It shows how ordering can be ignored if it breaks a dependency by default, and how --order-dependencies enforces the specified order. ```python import pytest def test_a(): assert True @pytest.mark.dependency(depends=["test_a"]) @pytest.mark.order("first") def test_b(): assert True ``` ```python import pytest @pytest.pytest.mark.dependency(depends=["test_b"]) def test_a(): assert True @pytest.mark.dependency def test_b(): assert True ``` -------------------------------- ### Relative Test Ordering Using Before/After in Pytest Source: https://context7.com/pytest-dev/pytest-order/llms.txt This snippet illustrates relative test ordering using the 'before' and 'after' attributes within the @pytest.mark.order decorator. This allows specifying that a test should run before or after another specific test, creating dependency-like behavior. ```python import pytest @pytest.mark.order(after="test_second") def test_third(): assert True def test_second(): assert True @pytest.mark.order(before="test_second") def test_first(): assert True # Execution order: test_first -> test_second -> test_third ``` -------------------------------- ### Order Test Modules using pytestmark in pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst Explains how to order entire test modules by applying the pytest.mark.order marker to the module level using `pytestmark`. Tests within a module maintain their relative order. ```python import pytest pytestmark = pytest.mark.order(1) def test_1(): ... ``` -------------------------------- ### Use Parametrized Tests with Multiple Order Markers in pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst Illustrates how to combine pytest's parametrization with multiple order markers from pytest-order. Each combination of marker and parameter creates a distinct test instance. ```python import pytest @pytest.pytest.mark.order(1) @pytest.pytest.mark.order(3) @pytest.pytest.mark.parametrize("foo", ["aaa", "bbb"]) def test_one_and_three(foo): pass @pytest.pytest.mark.order(4) @pytest.pytest.mark.parametrize("bar", ["bbb", "ccc"]) @pytest.pytest.mark.order(2) def test_two_and_four(bar): pass ``` -------------------------------- ### pytest test with absolute order using pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/intro.rst Demonstrates how to use the '@pytest.mark.order(n)' marker to control test execution sequence. 'test_bar' is marked with order 1 and 'test_foo' with order 2, ensuring 'test_bar' runs before 'test_foo'. ```python import pytest @pytest.mark.order(2) def test_foo(): assert True @pytest.mark.order(1) def test_bar(): assert True ``` -------------------------------- ### Sparse Ordering with pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst Demonstrates how pytest-order handles test execution with and without the --sparse-ordering option. It shows how missing ordinal numbers are filled by unordered tests when --sparse-ordering is enabled, affecting the final test execution sequence. ```python import pytest @pytest.mark.order(3) def test_two(): assert True def test_three(): assert True def test_four(): assert True @pytest.mark.order(1) def test_one(): assert True ``` ```python import pytest @pytest.mark.order(1) def test_two(): assert True def test_three(): assert True def test_four(): assert True @pytest.mark.order(0) def test_one(): assert True ``` -------------------------------- ### Basic pytest test without ordering Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/intro.rst Standard pytest functions that execute in the order they appear within a module. This serves as a baseline to demonstrate the effect of test ordering plugins. ```python def test_foo(): assert True def test_bar(): assert True ``` -------------------------------- ### Order Scope Configuration in Pytest Source: https://context7.com/pytest-dev/pytest-order/llms.txt This snippet illustrates how to configure the scope of test ordering using the '--order-scope' command-line option. It shows the difference between 'session' scope (global ordering) and 'module' scope (ordering within each file). ```python # test_module1.py import pytest @pytest.mark.order(2) def test2(): pass @pytest.mark.order(1) def test1(): pass # test_module2.py import pytest @pytest.mark.order(2) def test2(): pass @pytest.mark.order(1) def test1(): pass # Run with session scope (default): ordering applies across all modules # pytest --order-scope=session # Execution: module1::test1 -> module1::test2 -> module2::test1 -> module2::test2 # Run with module scope: ordering applies within each module only # pytest --order-scope=module # Execution: module1::test1 -> module1::test2 -> module2::test1 -> module2::test2 (same result but ordering is independent per module) ``` -------------------------------- ### Combine Absolute and Relative Ordering Markers in pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst Shows how to apply multiple order markers to a single test in pytest-order, including a mix of absolute and relative ordering. Each marker results in a separate test execution. ```python import pytest @pytest.pytest.mark.order(1) @pytest.pytest.mark.order(-1) def test_one_and_seven(): pass @pytest.pytest.mark.order(2) @pytest.pytest.mark.order(-2) def test_two_and_six(): pass def test_four(): pass @pytest.pytest.mark.order(before="test_four") @pytest.pytest.mark.order(after="test_four") def test_three_and_five(): pass ``` -------------------------------- ### Order Test Relative to Multiple Other Tests with pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst This code demonstrates how to specify multiple dependencies for a single test using a list or tuple with the 'after' marker. The test will only execute after all specified tests (e.g., 'test_second' and 'other_module.py::test_other') have completed. ```python import pytest @pytest.mark.order(after=["test_second", "other_module.py::test_other"]) def test_first(): assert True def test_second(): assert True ``` -------------------------------- ### Class-Level Markers with pytest-order for Test Ordering Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst This snippet illustrates how markers applied at the class level in pytest-order are inherited by all tests within that class. It also demonstrates referencing entire classes using 'before' or 'after' attributes, which orders all tests in the marked class relative to the referenced class. ```python import pytest @pytest.mark.order(after="Test2") class Test1: def test_1(self): assert True def test_2(self): assert True class Test2: def test_1(self): assert True def test_2(self): assert True ``` -------------------------------- ### Relative Test Ordering with Pytest Markers Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst Illustrates using relative markers like `after` and `before` to specify test execution order. This approach allows tests to depend on the completion of other specific tests, even across different modules. ```python import pytest @pytest.mark.order(after="test_module2.py::test1") def test1(): pass def test2(): pass ``` ```python import pytest def test1(): pass @pytest.mark.order(before="test1") def test2(): pass ``` -------------------------------- ### Configure pytest-order to always order tests with dependency markers Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst This configuration option in pytest.ini ensures that tests are always ordered based on their dependency markers. It's useful for maintaining a predictable test execution flow. ```ini [pytest] ; always order tests with dependency markers addopts = --order-dependencies ``` -------------------------------- ### Define Test Order Relative to Other Tests with pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst This snippet demonstrates how to use the 'after' and 'before' markers from pytest-order to define the execution order of tests relative to each other. Tests are referenced by their names. This is useful for ensuring that certain tests run only after specific dependencies have passed. ```python import pytest @pytest.mark.order(after="test_second") def test_third(): assert True def test_second(): assert True @pytest.mark.order(before="test_second") def test_first(): assert True ``` -------------------------------- ### Dependency Ordering with pytest-dependency Source: https://context7.com/pytest-dev/pytest-order/llms.txt Integrates with pytest-dependency to automatically order tests based on their declared dependencies. The `--order-dependencies` flag ensures that dependencies are respected even when explicit order markers are present. This ensures that a test runs only after its dependencies have successfully executed. ```python import pytest @pytest.mark.dependency() def test_base(): assert True @pytest.mark.dependency(depends=["test_base"]) @pytest.mark.order(1) def test_dependent(): assert True # With --order-dependencies: # pytest --order-dependencies # Execution: test_base -> test_dependent (dependency respected despite order marker) # Without --order-dependencies: # Execution: test_dependent (1) -> test_base (dependencies ignored) ``` -------------------------------- ### Reference Parametrized Tests by Name in pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst Demonstrates how to reference parametrized tests by their base name in pytest-order. Note that fully qualified names including parameters are not supported. ```python import pytest @pytest.mark.order(after=["test_second"]) def test_first(): assert True @pytest.pytest.mark.parametrize("param", [1, 2, 3]) def test_second(param): assert True ``` -------------------------------- ### Order tests per module using pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst Demonstrates how to mark tests with the @pytest.mark.order decorator to control their execution order within a module. When used with --order-scope=module, tests are ordered independently in each module. ```python import pytest @pytest.mark.order(2) def test2(): pass @pytest.mark.order(1) def test1(): pass ``` -------------------------------- ### Order Tests by Index using pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/README.md This snippet demonstrates how to use the 'order' marker provided by pytest-order to specify the execution sequence of tests using ordinal numbers. Tests marked with lower numbers will execute before tests marked with higher numbers. This feature is useful for defining a precise test run order. ```python import pytest @pytest.mark.order(2) def test_foo(): assert True @pytest.mark.order(1) def test_bar(): assert True ``` -------------------------------- ### Order Tests by Absolute Index Number (Python) Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst Demonstrates how to use the `@pytest.mark.order` decorator with integer indices to define the execution order of tests. Supports positive, negative, and the `index=` keyword. ```python import pytest @pytest.mark.order(-2) def test_three(): assert True @pytest.mark.order(index=-1) def test_four(): assert True @pytest.mark.order(index=2) def test_two(): assert True @pytest.mark.order(1) def test_one(): assert True ``` -------------------------------- ### Order Tests by Ordinal Strings (Python) Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst Illustrates using the `@pytest.mark.order` decorator with string aliases like 'first', 'second', 'last', and 'second_to_last' for a more readable test ordering. These map to specific integer indices. ```python import pytest @pytest.mark.order("second_to_last") def test_three(): assert True @pytest.mark.order("last") def test_four(): assert True @pytest.mark.order("second") def test_two(): assert True @pytest.mark.order("first") def test_one(): assert True ``` -------------------------------- ### Custom Marker-Based Test Group Ordering with --order-marker-prefix Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst This code illustrates how to group tests using custom markers (e.g., 'm1', 'm2', 'm3') and then enforce a specific execution order for these groups using the --order-marker-prefix option. This avoids redundant order markers by allowing the prefix of the marker itself to define the order. ```python import pytest @pytest.mark.m3 def test_a(): assert True @pytest.mark.m1 def test_b(): assert True @pytest.mark.m2 def test_c(): assert True ``` ```python import pytest @pytest.mark.order(3) @pytest.mark.m3 def test_a(): assert True @pytest.mark.order(1) @pytest.mark.m1 def test_b(): assert True ``` -------------------------------- ### Custom Marker Prefix for Test Ordering Source: https://context7.com/pytest-dev/pytest-order/llms.txt Allows using custom marker names for test ordering instead of the default 'order' marker. The `--order-marker-prefix` option specifies a prefix, enabling tests to be ordered based on markers like `run1`, `run2`, etc. This provides flexibility in naming and organizing tests by execution priority. ```python import pytest @pytest.mark.run1 def test_first(): assert True @pytest.mark.run2 def test_second(): assert True @pytest.mark.run3 def test_third(): assert True # Run with custom marker prefix: # pytest --order-marker-prefix=run # Execution: test_first (run1=1) -> test_second (run2=2) -> test_third (run3=3) ``` -------------------------------- ### Error on Failed Ordering Enforcement Source: https://context7.com/pytest-dev/pytest-order/llms.txt Converts ordering warnings into test failures with the `--error-on-failed-ordering` option. If a test cannot be ordered due to missing dependencies or invalid markers, the test suite will fail instead of just issuing a warning. This enforces strict adherence to ordering rules. ```python import pytest @pytest.mark.order(before="nonexistent_test") def test_example(): assert True # Without --error-on-failed-ordering: # pytest test_file.py # WARNING: cannot execute 'test_example' relative to others: 'nonexistent_test' - ignoring the marker. # Test runs normally # With --error-on-failed-ordering: # pytest --error-on-failed-ordering test_file.py # Test fails with: "The test could not be ordered" ``` -------------------------------- ### Test Ordering with Markers in Pytest Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst Demonstrates how to use pytest markers to define the order of test execution within modules. The `@pytest.mark.order` decorator assigns a specific order number to a test function. ```python import pytest @pytest.mark.order(2) def test1(): pass def test2(): pass ``` ```python import pytest @pytest.mark.order(1) def test1(): pass def test2(): pass ``` -------------------------------- ### Order Tests Using Class-Level Markers (Python) Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/usage.rst Shows how to apply the `@pytest.mark.order` decorator to a test class, which then orders all tests within that class relative to other classes. Individual test orders within the class remain unaffected. ```python import pytest @pytest.mark.order(1) class Test1: def test_1(self): assert True def test_2(self): assert True @pytest.mark.order(0) class Test2: def test_1(self): assert True def test_2(self): assert True ``` -------------------------------- ### Indulgent Ordering for Plugin Priority Source: https://context7.com/pytest-dev/pytest-order/llms.txt Applies pytest-order sorting before other plugins using the `--indulgent-ordering` option. This allows other test ordering plugins to modify the test order after pytest-order has applied its initial sorting, effectively giving priority to other plugins. This is useful for complex integration scenarios with multiple ordering mechanisms. ```bash # Run with indulgent ordering: pytest --indulgent-ordering # This makes pytest-order apply its sorting first (tryfirst hook) # Other sorting plugins can then modify the order, giving them priority # Useful when integrating with other test ordering plugins ``` -------------------------------- ### Named Ordinal Test Ordering in Pytest Source: https://context7.com/pytest-dev/pytest-order/llms.txt This code showcases the use of named ordinals (e.g., 'first', 'last') with the @pytest.mark.order decorator for more readable test sequencing. It allows defining order using descriptive terms instead of just numbers. ```python import pytest @pytest.mark.order("second_to_last") def test_three(): assert True @pytest.mark.order("last") def test_four(): assert True @pytest.mark.order("second") def test_two(): assert True @pytest.mark.order("first") def test_one(): assert True # Execution order: test_one (first) -> test_two (second) -> test_three (second_to_last) -> test_four (last) ``` -------------------------------- ### Class-Level Test Ordering in Pytest Source: https://context7.com/pytest-dev/pytest-order/llms.txt This snippet shows how to apply the @pytest.mark.order decorator at the class level to control the execution order of entire test classes. Tests within classes marked with lower numbers will execute before those in classes with higher numbers. ```python import pytest @pytest.mark.order(1) class Test1: def test_1(self): assert True def test_2(self): assert True @pytest.mark.order(0) class Test2: def test_1(self): assert True def test_2(self): assert True # Execution order: Test2 tests run before Test1 tests # Test2::test_1 -> Test2::test_2 -> Test1::test_1 -> Test1::test_2 ``` -------------------------------- ### Directory-Level Test Order Scope Source: https://context7.com/pytest-dev/pytest-order/llms.txt Defines hierarchical test ordering scope using the `--order-scope-level` option. This allows for organizing tests within different directories or modules and controlling the scope of ordering. Setting `--order-scope-level=1` applies ordering within the first level of directories, enabling independent ordering of tests across different feature sets. ```python # feature1/test_a.py import pytest @pytest.mark.order(2) def test_a2(): pass @pytest.mark.order(1) def test_a1(): pass # feature2/test_b.py import pytest @pytest.mark.order(2) def test_b2(): pass @pytest.mark.order(1) def test_b1(): pass # Run with directory scope level 1 (first level directories): # pytest --order-scope-level=1 # Execution: ordering is applied within each feature directory independently # feature1/test_a1 -> feature1/test_a2 -> feature2/test_b1 -> feature2/test_b2 ``` -------------------------------- ### Error Handling for Failed Test Ordering in pytest-order Source: https://github.com/pytest-dev/pytest-order/blob/main/docs/source/configuration.rst Illustrates the behavior of the --error-on-failed-ordering option. When a test depends on another test that doesn't exist or has a cyclic dependency, using this option causes the dependent test to error instead of just issuing a warning. ```python import pytest def test_one(): assert True @pytest.mark.order(before="test_three") def test_two(): assert True ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.