Skip to content

Commit 21a1778

Browse files
committed
Move plugin code from __init__.py to plugin.py
- move version into __init__.py - adapt tests, use standard testdir fixture
1 parent 76de97d commit 21a1778

23 files changed

+965
-1363
lines changed

.github/workflows/performance.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
python -m pip install wheel
3939
python -m pip install pytest
4040
python -m pip install pytest-dependency
41+
python -m pip install -e .
4142
- name: Run tests
4243
run: |
4344
python -m pytest -s perf_tests

.github/workflows/pythontests.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,10 @@ jobs:
9494
uses: actions/setup-python@v2
9595
with:
9696
python-version: "3.8"
97-
- name: Install wheel and pytest
97+
- name: Install wheel
9898
run: |
9999
python -m pip install --upgrade pip
100100
pip install wheel
101-
pip install pytest
102101
- name: Build package
103102
run: |
104103
cd main

perf_tests/test_dependencies.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import os
2-
import shutil
31
from unittest import mock
42

53
import pytest
6-
7-
import pytest_order
84
from perf_tests.util import TimedSorter
9-
from tests.utils import write_test
5+
6+
pytest_plugins = ["pytester"]
107

118

129
@pytest.fixture
13-
def fixture_path_relative(tmpdir_factory):
14-
fixture_path = str(tmpdir_factory.mktemp("dep_perf_sparse"))
15-
for module_index in range(10):
16-
testname = os.path.join(
17-
fixture_path, "test_dep_perf{}.py".format(module_index))
10+
def fixture_path_relative(testdir):
11+
for i_mod in range(10):
12+
test_name = testdir.tmpdir.join("test_dep_perf{}.py".format(i_mod))
1813
test_contents = """
1914
import pytest
2015
"""
@@ -30,16 +25,14 @@ def test_{}():
3025
def test_{}():
3126
assert True
3227
""".format(i + 40)
33-
write_test(testname, test_contents)
34-
yield fixture_path
35-
shutil.rmtree(fixture_path, ignore_errors=True)
28+
test_name.write(test_contents)
29+
yield testdir
3630

3731

38-
@mock.patch("pytest_order.Sorter", TimedSorter)
32+
@mock.patch("pytest_order.plugin.Sorter", TimedSorter)
3933
def test_performance_dependency(fixture_path_relative):
4034
"""Test performance of dependency markers that point to tests without
4135
an order mark (same as for test_relative does for after markers)."""
42-
args = ["--order-dependencies", fixture_path_relative]
4336
TimedSorter.nr_marks = 400
44-
pytest.main(args, [pytest_order])
45-
assert TimedSorter.elapsed < 0.2
37+
fixture_path_relative.runpytest("--quiet", "--order-dependencies")
38+
assert TimedSorter.elapsed < 0.15

perf_tests/test_ordinal.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import os
2-
import shutil
31
from unittest import mock
42

53
import pytest
6-
import pytest_order
74
from perf_tests.util import TimedSorter
8-
from tests.utils import write_test
5+
6+
pytest_plugins = ["pytester"]
97

108

119
@pytest.fixture
12-
def fixture_path_ordinal(tmpdir_factory):
13-
fixture_path = str(tmpdir_factory.mktemp("ordinal_perf"))
14-
for module_index in range(10):
15-
testname = os.path.join(
16-
fixture_path, "test_performance{}.py".format(module_index))
10+
def fixture_path_ordinal(testdir):
11+
for i_mod in range(10):
12+
test_name = testdir.tmpdir.join("test_performance{}.py".format(i_mod))
1713
test_contents = """
1814
import pytest
1915
"""
@@ -23,14 +19,12 @@ def fixture_path_ordinal(tmpdir_factory):
2319
def test_{}():
2420
assert True
2521
""".format(50 - i, i)
26-
write_test(testname, test_contents)
27-
yield fixture_path
28-
shutil.rmtree(fixture_path, ignore_errors=True)
22+
test_name.write(test_contents)
23+
yield testdir
2924

3025

31-
@mock.patch("pytest_order.Sorter", TimedSorter)
26+
@mock.patch("pytest_order.plugin.Sorter", TimedSorter)
3227
def test_performance_ordinal(fixture_path_ordinal):
33-
args = [fixture_path_ordinal]
3428
TimedSorter.nr_marks = 1000
35-
pytest.main(args, [pytest_order])
29+
fixture_path_ordinal.runpytest("--quiet")
3630
assert TimedSorter.elapsed < 0.02

perf_tests/test_relative.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import os
2-
import shutil
31
from unittest import mock
42

53
import pytest
6-
7-
import pytest_order
84
from perf_tests.util import TimedSorter
9-
from tests.utils import write_test
5+
6+
pytest_plugins = ["pytester"]
107

118

129
@pytest.fixture
13-
def fixture_path_relative(tmpdir_factory):
14-
fixture_path = str(tmpdir_factory.mktemp("relative_perf"))
15-
for module_index in range(10):
16-
testname = os.path.join(
17-
fixture_path, "test_relative_perf{}.py".format(module_index))
10+
def fixture_path_relative(testdir):
11+
for i_mod in range(10):
12+
test_name = testdir.tmpdir.join(
13+
"test_relative_perf{}.py".format(i_mod))
1814
test_contents = """
1915
import pytest
2016
"""
@@ -29,16 +25,14 @@ def test_{}():
2925
def test_{}():
3026
assert True
3127
""".format(i + 40)
32-
write_test(testname, test_contents)
33-
yield fixture_path
34-
shutil.rmtree(fixture_path, ignore_errors=True)
28+
test_name.write(test_contents)
29+
yield testdir
3530

3631

37-
@mock.patch("pytest_order.Sorter", TimedSorter)
32+
@mock.patch("pytest_order.plugin.Sorter", TimedSorter)
3833
def test_performance_relative(fixture_path_relative):
3934
"""Test performance of after markers that point to tests without
4035
an order mark (the usual case)."""
41-
args = [fixture_path_relative]
4236
TimedSorter.nr_marks = 400
43-
pytest.main(args, [pytest_order])
37+
fixture_path_relative.runpytest("--quiet")
4438
assert TimedSorter.elapsed < 0.15

perf_tests/test_relative_dense.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import os
2-
import shutil
31
from unittest import mock
42

53
import pytest
6-
7-
import pytest_order
84
from perf_tests.util import TimedSorter
9-
from tests.utils import write_test
5+
6+
pytest_plugins = ["pytester"]
107

118

129
@pytest.fixture
13-
def fixture_path_relative_dense(tmpdir_factory):
14-
fixture_path = str(tmpdir_factory.mktemp("relative_dense_perf"))
15-
for module_index in range(10):
16-
testname = os.path.join(
17-
fixture_path, "test_relative_dense_perf{}.py".format(module_index))
10+
def fixture_path_relative_dense(testdir):
11+
for i_mod in range(10):
12+
test_name = testdir.tmpdir.join(
13+
"test_relative_dense_perf{}.py".format(i_mod))
1814
test_contents = """
1915
import pytest
2016
"""
@@ -29,16 +25,14 @@ def test_{}():
2925
def test_{}():
3026
assert True
3127
""".format(i + 90)
32-
write_test(testname, test_contents)
33-
yield fixture_path
34-
shutil.rmtree(fixture_path, ignore_errors=True)
28+
test_name.write(test_contents)
29+
yield testdir
3530

3631

37-
@mock.patch("pytest_order.Sorter", TimedSorter)
32+
@mock.patch("pytest_order.plugin.Sorter", TimedSorter)
3833
def test_performance_relative(fixture_path_relative_dense):
3934
"""Test performance of after markers that mostly point to tests with
4035
another order mark, so items are evaluated multiple times."""
41-
args = [fixture_path_relative_dense]
4236
TimedSorter.nr_marks = 900
43-
pytest.main(args, [pytest_order])
37+
fixture_path_relative_dense.runpytest("--quiet")
4438
assert TimedSorter.elapsed < 0.15

perf_tests/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import time
22

3-
from pytest_order import Sorter
3+
from pytest_order.sorter import Sorter
44

55

66
class TimedSorter(Sorter):
@@ -13,5 +13,5 @@ def sort_items(self):
1313
items = super().sort_items()
1414
self.__class__.elapsed = ((time.time() - start_time)
1515
/ self.nr_marks * 1000)
16-
print("Time per test: {:.3f} ms".format(self.__class__.elapsed))
16+
print("\nTime per test: {:.3f} ms".format(self.__class__.elapsed))
1717
return items

pytest_order/__init__.py

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1 @@
1-
# -*- coding: utf-8 -*-
2-
3-
import pytest
4-
5-
from pytest_order.sorter import Sorter
6-
from ._version import __version__ # noqa: F401
7-
8-
9-
def pytest_configure(config):
10-
"""Register the "order" marker and configure the plugin depending
11-
on the CLI options"""
12-
13-
provided_by_pytest_order = (
14-
"Provided by pytest-order. "
15-
"See also: https://pytest-dev.github.io/pytest-order/"
16-
)
17-
18-
config_line = (
19-
"order: specify ordering information for when tests should run "
20-
"in relation to one another. " + provided_by_pytest_order
21-
)
22-
config.addinivalue_line("markers", config_line)
23-
24-
if config.getoption("indulgent_ordering"):
25-
# We need to dynamically add this `tryfirst` decorator to the plugin:
26-
# only when the CLI option is present should the decorator be added.
27-
# Thus, we manually run the decorator on the class function and
28-
# manually replace it.
29-
# Python 2.7 didn't allow arbitrary attributes on methods, so we have
30-
# to keep the function as a function and then add it to the class as a
31-
# pseudo method. Since the class is purely for structuring and `self`
32-
# is never referenced, this seems reasonable.
33-
OrderingPlugin.pytest_collection_modifyitems = pytest.hookimpl(
34-
function=modify_items, tryfirst=True)
35-
else:
36-
OrderingPlugin.pytest_collection_modifyitems = pytest.hookimpl(
37-
function=modify_items, trylast=True)
38-
config.pluginmanager.register(OrderingPlugin(), "orderingplugin")
39-
40-
41-
def pytest_addoption(parser):
42-
"""Set up CLI option for pytest"""
43-
group = parser.getgroup("ordering")
44-
group.addoption("--indulgent-ordering", action="store_true",
45-
dest="indulgent_ordering",
46-
help="Request that the sort order provided by "
47-
"pytest-order be applied before other sorting, "
48-
"allowing the other sorting to have priority")
49-
group.addoption("--order-scope", action="store",
50-
dest="order_scope",
51-
help="Defines the scope used for ordering. Possible values"
52-
"are 'session' (default), 'module', and 'class'."
53-
"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.")
61-
group.addoption("--order-group-scope", action="store",
62-
dest="order_group_scope",
63-
help="Defines the scope used for order groups. Possible "
64-
"values are 'session' (default), 'module', "
65-
"and 'class'. Ordering is first done inside a group, "
66-
"then between groups.")
67-
group.addoption("--sparse-ordering", action="store_true",
68-
dest="sparse_ordering",
69-
help="If there are gaps between ordinals they are filled "
70-
"with unordered tests.")
71-
group.addoption("--order-dependencies", action="store_true",
72-
dest="order_dependencies",
73-
help="If set, dependencies added by pytest-dependency will"
74-
"be ordered if needed.")
75-
76-
77-
class OrderingPlugin(object):
78-
"""
79-
Plugin implementation
80-
81-
By putting this in a class, we are able to dynamically register it after
82-
the CLI is parsed.
83-
"""
84-
85-
86-
def modify_items(session, config, items):
87-
sorter = Sorter(config, items)
88-
items[:] = sorter.sort_items()
1+
__version__ = "1.0.dev0"

pytest_order/_version.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)