Skip to content

Commit fae144a

Browse files
committed
Refactor ordering scope test
- remove fixtures, make it more readable - add test for separate files on command line - add release dates to changelog - fix coverage badge link
1 parent 55a30c2 commit fae144a

File tree

11 files changed

+174
-139
lines changed

11 files changed

+174
-139
lines changed

CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
## Unreleased
44

5-
## [Version 0.8.1](https://pypi.org/project/pytest-order/0.8.1/)
5+
## [Version 0.8.1](https://pypi.org/project/pytest-order/0.8.1/) (2020-11-02)
66

77
### Added
8-
- add configuration option for sorting scope,
8+
- added configuration option for sorting scope,
99
see [#2](https://github.com/mrbean-bremen/pytest-order/issues/2)
1010

11-
## [Version 0.8.0](https://pypi.org/project/pytest-order/0.8.0/)
11+
## [Version 0.8.0](https://pypi.org/project/pytest-order/0.8.0/) (2020-10-30)
1212
This release is mostly related to the consolidation of infrastructure
1313
(documentation build and tests in CI build) and documentation.
1414

@@ -22,7 +22,7 @@ This release is mostly related to the consolidation of infrastructure
2222
- added regression test for ``pytest-xdist``
2323
(imported from [PR #52](https://github.com/ftobia/pytest-ordering/pull/52))
2424

25-
## [Version 0.7.1](https://pypi.org/project/pytest-order/0.7.1/)
25+
## [Version 0.7.1](https://pypi.org/project/pytest-order/0.7.1/) (2020-10-24)
2626
Update after renaming the repository and the package.
2727

2828
### Changes
@@ -33,7 +33,7 @@ Update after renaming the repository and the package.
3333
### Documentation
3434
- use separate documentation pages for release and development versions
3535

36-
## [Version 0.7.0](https://pypi.org/project/pytest-ordering2/0.7.0/)
36+
## [Version 0.7.0](https://pypi.org/project/pytest-ordering2/0.7.0/) (2020-10-22)
3737
Imported version from [pytest-ordering](https://github.com/ftobia/pytest-ordering),
3838
including some PRs (manually merged).
3939

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
_pytest-order_ - a pytest plugin to order test execution
22
========================================================
33

4-
[![PyPI version](https://badge.fury.io/py/pytest-order.svg)](https://badge.fury.io/py/pytest-order) [![Build Status](https://travis-ci.org/mrbean-bremen/pytest-order.svg?branch=master)](https://travis-ci.org/mrbean-bremen/pytest-order) [![Coverage Status](https://img.shields.io/coveralls/github/mrbean-bremen/pytest-order)](https://coveralls.io/github/badges/shields?branch=master) [![Python version](https://img.shields.io/pypi/pyversions/pytest-order.svg)](https://img.shields.io/pypi/pyversions/pytest-order.svg)
4+
[![PyPI version](https://badge.fury.io/py/pytest-order.svg)](https://badge.fury.io/py/pytest-order) [![Build Status](https://travis-ci.org/mrbean-bremen/pytest-order.svg?branch=master)](https://travis-ci.org/mrbean-bremen/pytest-order) [![Coverage Status](https://img.shields.io/coveralls/github/mrbean-bremen/pytest-order)](https://coveralls.io/github/mrbean-bremen/pytest-order) [![Python version](https://img.shields.io/pypi/pyversions/pytest-order.svg)](https://img.shields.io/pypi/pyversions/pytest-order.svg)
55

66
`pytest-order` is a pytest plugin that allows you to customize the order in which
77
your tests are run. It uses the marker `order` that defines when a specific

fixture/test_classes.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

fixture/test_functions1.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

fixture/test_functions2.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

pytest_order/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def pytest_addoption(parser):
7171
"allowing the other sorting to have priority")
7272
group.addoption("--order-scope", action="store",
7373
dest="order-scope",
74-
help="Defines the scope used for ordeing. Possible values"
74+
help="Defines the scope used for ordering. Possible values"
7575
"are 'session' (default), 'module', and 'class'")
7676

7777

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
pytest_plugins = ["pytester"]
4+
5+
6+
@pytest.fixture
7+
def item_names_for(testdir):
8+
def _item_names_for(tests_content):
9+
# some strange code to extract sorted items
10+
items = testdir.getitems(tests_content)
11+
hook = testdir.config.hook
12+
hook.pytest_collection_modifyitems(session=items[0].session,
13+
config=testdir.config, items=items)
14+
return [item.name for item in items]
15+
16+
return _item_names_for

tests/test_indulgent_ordering.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import pytest_order
66

7-
pytest_plugins = ["pytester"]
8-
97

108
def test_run_marker_registered(capsys, tmpdir):
119
testname = str(tmpdir.join("failing.py"))

tests/test_order_scope.py

Lines changed: 151 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,173 @@
11
# -*- coding: utf-8 -*-
22
import os
3+
import re
4+
import shutil
35

46
import pytest
57

68
import pytest_order
79

810

9-
def fixture_path():
10-
base_path = os.path.split(os.path.dirname(__file__))[0]
11-
return os.path.join(base_path, "fixture")
11+
@pytest.fixture(scope="module")
12+
def fixture_path(tmpdir_factory):
13+
fixture_path = str(tmpdir_factory.mktemp("fixtures"))
14+
testname = os.path.join(fixture_path, "test_classes.py")
15+
test_class_contents = """
16+
import pytest
17+
18+
class Test1:
19+
@pytest.mark.order("last")
20+
def test_two(self):
21+
assert True
22+
23+
@pytest.mark.order("first")
24+
def test_one(self):
25+
assert True
26+
27+
class Test2:
28+
@pytest.mark.order("last")
29+
def test_two(self):
30+
assert True
31+
32+
@pytest.mark.order("first")
33+
def test_one(self):
34+
assert True
35+
36+
@pytest.mark.order("last")
37+
def test_two():
38+
assert True
39+
40+
@pytest.mark.order("first")
41+
def test_one():
42+
assert True
43+
"""
44+
with open(testname, "w") as fi:
45+
fi.write(test_class_contents)
46+
47+
test_function_contents = """
48+
import pytest
49+
50+
@pytest.mark.order("last")
51+
def test1_two():
52+
assert True
53+
54+
@pytest.mark.order("first")
55+
def test1_one():
56+
assert True
57+
"""
58+
testname = os.path.join(fixture_path, "test_functions1.py")
59+
with open(testname, "w") as fi:
60+
fi.write(test_function_contents)
61+
test_function_contents = """
62+
import pytest
1263
64+
@pytest.mark.order("last")
65+
def test2_two():
66+
assert True
1367
14-
def test_session_scope(capsys):
15-
args = ["-v", fixture_path()]
68+
@pytest.mark.order("first")
69+
def test2_one():
70+
assert True
71+
"""
72+
testname = os.path.join(fixture_path, "test_functions2.py")
73+
with open(testname, "w") as fi:
74+
fi.write(test_function_contents)
75+
yield fixture_path
76+
shutil.rmtree(fixture_path, ignore_errors=True)
77+
78+
79+
@pytest.fixture(scope="module")
80+
def fixture_file_paths(fixture_path):
81+
yield [
82+
os.path.join(fixture_path, "test_classes.py"),
83+
os.path.join(fixture_path, "test_functions1.py"),
84+
os.path.join(fixture_path, "test_functions2.py")
85+
]
86+
87+
88+
def test_session_scope(fixture_path, capsys):
89+
args = ["-v", fixture_path]
90+
pytest.main(args, [pytest_order])
91+
out, err = capsys.readouterr()
92+
expected = (
93+
".*test_classes.py::Test1::test_one"
94+
".*test_classes.py::Test2::test_one"
95+
".*test_classes.py::test_one"
96+
".*test_functions1.py::test1_one"
97+
".*test_functions2.py::test2_one"
98+
".*test_classes.py::Test1::test_two"
99+
".*test_classes.py::Test2::test_two"
100+
".*test_classes.py::test_two"
101+
".*test_functions1.py::test1_two"
102+
".*test_functions2.py::test2_two.*"
103+
)
104+
assert re.match(expected, out.replace("\n", "")), out
105+
106+
107+
def test_session_scope_multiple_files(fixture_file_paths, capsys):
108+
"""Test that session scope works with multiple files on command line."""
109+
args = ["-v"] + fixture_file_paths
16110
pytest.main(args, [pytest_order])
17111
out, err = capsys.readouterr()
18-
i_class1_one = out.index("test_classes.py::Test1::test_one")
19-
i_class2_one = out.index("test_classes.py::Test2::test_one")
20-
i_noclass_one = out.index("test_classes.py::test_one")
21-
i_func1_one = out.index("test_functions1.py::test_one")
22-
i_func2_one = out.index("test_functions2.py::test_one")
23-
i_class1_two = out.index("test_classes.py::Test1::test_two")
24-
i_class2_two = out.index("test_classes.py::Test2::test_two")
25-
i_noclass_two = out.index("test_classes.py::test_two")
26-
i_func1_two = out.index("test_functions1.py::test_two")
27-
i_func2_two = out.index("test_functions2.py::test_two")
28-
assert (i_class1_one < i_class2_one < i_noclass_one
29-
< i_func1_one < i_func2_one
30-
< i_class1_two < i_class2_two < i_noclass_two
31-
< i_func1_two < i_func2_two)
32-
33-
34-
def test_module_scope(capsys):
35-
args = ["-v", "--order-scope=module", fixture_path()]
112+
# under Windows, the module name is not listed in this case,
113+
# so do not expect it
114+
expected = (
115+
".*::Test1::test_one"
116+
".*::Test2::test_one"
117+
".*::test_one"
118+
".*::test1_one"
119+
".*::test2_one"
120+
".*::Test1::test_two"
121+
".*::Test2::test_two"
122+
".*::test_two"
123+
".*::test1_two"
124+
".*::test2_two.*"
125+
)
126+
assert re.match(expected, out.replace("\n", "")), out
127+
128+
129+
def test_module_scope(fixture_path, capsys):
130+
args = ["-v", "--order-scope=module", fixture_path]
36131
pytest.main(args, [pytest_order])
37132
out, err = capsys.readouterr()
38-
print('module:', out)
39-
i_class1_one = out.index("test_classes.py::Test1::test_one")
40-
i_class2_one = out.index("test_classes.py::Test2::test_one")
41-
i_noclass_one = out.index("test_classes.py::test_one")
42-
i_func1_one = out.index("test_functions1.py::test_one")
43-
i_func2_one = out.index("test_functions2.py::test_one")
44-
i_class1_two = out.index("test_classes.py::Test1::test_two")
45-
i_class2_two = out.index("test_classes.py::Test2::test_two")
46-
i_noclass_two = out.index("test_classes.py::test_two")
47-
i_func1_two = out.index("test_functions1.py::test_two")
48-
i_func2_two = out.index("test_functions2.py::test_two")
49-
assert (i_class1_one < i_class2_one < i_noclass_one
50-
< i_class1_two < i_class2_two < i_noclass_two
51-
< i_func1_one < i_func1_two
52-
< i_func2_one < i_func2_two)
53-
54-
55-
def test_class_scope(capsys):
56-
args = ["-v", "--order-scope=class", fixture_path()]
133+
expected = (
134+
".*test_classes.py::Test1::test_one"
135+
".*test_classes.py::Test2::test_one"
136+
".*test_classes.py::test_one"
137+
".*test_classes.py::Test1::test_two"
138+
".*test_classes.py::Test2::test_two"
139+
".*test_classes.py::test_two"
140+
".*test_functions1.py::test1_one"
141+
".*test_functions1.py::test1_two"
142+
".*test_functions2.py::test2_one"
143+
".*test_functions2.py::test2_two.*"
144+
)
145+
assert re.match(expected, out.replace("\n", "")), out
146+
147+
148+
def test_class_scope(fixture_path, capsys):
149+
args = ["-v", "--order-scope=class", fixture_path]
57150
pytest.main(args, [pytest_order])
58151
out, err = capsys.readouterr()
59-
print('class:', out)
60-
i_class1_one = out.index("test_classes.py::Test1::test_one")
61-
i_class2_one = out.index("test_classes.py::Test2::test_one")
62-
i_noclass_one = out.index("test_classes.py::test_one")
63-
i_func1_one = out.index("test_functions1.py::test_one")
64-
i_func2_one = out.index("test_functions2.py::test_one")
65-
i_class1_two = out.index("test_classes.py::Test1::test_two")
66-
i_class2_two = out.index("test_classes.py::Test2::test_two")
67-
i_noclass_two = out.index("test_classes.py::test_two")
68-
i_func1_two = out.index("test_functions1.py::test_two")
69-
i_func2_two = out.index("test_functions2.py::test_two")
70-
assert (i_class1_one < i_class1_two < i_class2_one < i_class2_two
71-
< i_noclass_one < i_noclass_two
72-
< i_func1_one < i_func1_two
73-
< i_func2_one < i_func2_two)
152+
expected = (
153+
".*test_classes.py::Test1::test_one"
154+
".*test_classes.py::Test1::test_two"
155+
".*test_classes.py::Test2::test_one"
156+
".*test_classes.py::Test2::test_two"
157+
".*test_classes.py::test_one"
158+
".*test_classes.py::test_two"
159+
".*test_functions1.py::test1_one"
160+
".*test_functions1.py::test1_two"
161+
".*test_functions2.py::test2_one"
162+
".*test_functions2.py::test2_two.*"
163+
)
164+
assert re.match(expected, out.replace("\n", "")), out
74165

75166

76167
@pytest.mark.skipif(pytest.__version__.startswith(("3.6.", "3.7.")),
77-
reason="Warning does not appear in out in pytest < 3.8")
78-
def test_invalid_scope(capsys):
79-
args = ["-v", "--order-scope=function", fixture_path()]
168+
reason="Warning does not appear in output in pytest < 3.8")
169+
def test_invalid_scope(fixture_path, capsys):
170+
args = ["-v", "--order-scope=function", fixture_path]
80171
pytest.main(args, [pytest_order])
81172
out, err = capsys.readouterr()
82173
assert "UserWarning: Unknown order scope 'function', ignoring it." in out

tests/test_ordering.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@
22

33
import pytest
44

5-
pytest_plugins = ["pytester"]
6-
7-
8-
@pytest.fixture
9-
def item_names_for(testdir):
10-
def _item_names_for(tests_content):
11-
# some strange code to extract sorted items
12-
items = testdir.getitems(tests_content)
13-
hook = testdir.config.hook
14-
hook.pytest_collection_modifyitems(session=items[0].session,
15-
config=testdir.config, items=items)
16-
return [item.name for item in items]
17-
18-
return _item_names_for
19-
205

216
def test_no_marks(item_names_for):
227
tests_content = """

0 commit comments

Comments
 (0)