Skip to content

Commit e6bd4a9

Browse files
committed
Greatly simplify tests using somewhat-hidden _pytester plugin.
Special thanks to Floris Bruynooghe for pointing me to it.
1 parent b7cd752 commit e6bd4a9

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from pytest_ordering import pytest_collection_modifyitems
2+
3+
pytest_plugins = "pytester",

tests/test_ordering.py

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,27 @@
1-
import operator
2-
import os
3-
import random
41
import re
52

63
import pytest
74

85
import pytest_ordering
6+
from . import numbers, words, words_backwards, grouping
97

10-
__here__ = os.path.dirname(os.path.abspath(__file__))
118

12-
13-
def get_module(module_name):
14-
return __import__(module_name, globals(), locals(), [], 1)
15-
16-
17-
def get_tests(module_name):
18-
module = get_module(module_name)
19-
return [getattr(module, t) for t in dir(module) if t.startswith('test_')]
20-
21-
22-
def _names_of_tests(output, test_file_name):
23-
regex = r'^{0}:(\d+): test_([a-z]) PASSED$'.format(
24-
test_file_name.replace('.', '\.'))
25-
for line in output.split('\n'):
26-
match = re.match(regex, line)
27-
if match:
28-
yield match.group(2)
29-
30-
31-
def get_order(output, test_file_name):
32-
return list(_names_of_tests(output, test_file_name))
33-
34-
35-
# Default sorting is whatever order the tests show up in the module.
36-
37-
@pytest.mark.parametrize('module_name', [
38-
'numbers', 'words', 'words_backwards', 'grouping',
9+
@pytest.mark.parametrize('module', [
10+
numbers, words, words_backwards, grouping,
3911
])
40-
def test_ordering(module_name, capsys):
41-
module = get_module(module_name)
42-
pytest.main('{0}/{1}.py -vv'.format(__here__, module_name))
43-
relative_filename = 'tests/{0}.py'.format(module_name)
44-
out, err = capsys.readouterr()
45-
assert list(module.ordering) == get_order(out, relative_filename)
12+
def test_ordered_tests(module, testdir):
13+
items = testdir.getitems(module)
14+
ordered_tests = list(pytest_ordering._order_tests(items))
15+
ordered_letters = [item.name[-1] for item in ordered_tests]
16+
assert ordered_letters == list(module.ordering)
4617

4718

4819
def test_run_marker_registered(capsys):
4920
pytest.main('--markers')
5021
out, err = capsys.readouterr()
5122
assert '@pytest.mark.run' in out
5223

24+
5325
def test_version():
5426
assert hasattr(pytest_ordering, '__version__')
5527
assert re.match(r'[0-9]+\.[0-9]+(\.[0-9]+)?$', pytest_ordering.__version__)

0 commit comments

Comments
 (0)