Skip to content

Commit a218a74

Browse files
- added get_function, to parse out the test function name out of a test node id;
- added missing tests;
1 parent 51691ab commit a218a74

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/pytest_cleanslate/reduce.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,15 @@ def get_module(testid: str) -> str:
138138
return testid.split('::')[0]
139139

140140

141-
def is_module(testid: str) -> bool:
141+
def _is_module(testid: str) -> bool:
142142
return '::' not in testid
143143

144144

145+
def get_function(testid: str) -> str:
146+
if '::' in testid:
147+
return testid.split('::')[1].split('[')[0]
148+
149+
145150
def run_pytest(tests_path: Path, pytest_args=(), *,
146151
modules: T.List[Path] = None, tests: T.List[str] = None, trace: bool = False) -> dict:
147152
import tempfile
@@ -261,7 +266,7 @@ def reduce(*, tests_path: Path, results: Results = None, pytest_args: T.List[str
261266
'error': 'No tests failed',
262267
}
263268

264-
failed_is_module = is_module(failed_id)
269+
failed_is_module = _is_module(failed_id)
265270
if failed_is_module:
266271
if trace: print()
267272
print(f"Module \"{failed_id}\"'s collection failed; trying it by itself...", flush=True)

tests/test_reduce.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99

1010
import pytest_cleanslate.reduce as reduce
1111

12-
from pytest_cleanslate.reduce import MODULE_LIST_ARG, TEST_LIST_ARG, RESULTS_ARG, Results
12+
from pytest_cleanslate.reduce import MODULE_LIST_ARG, TEST_LIST_ARG, RESULTS_ARG, Results, \
13+
get_module, get_function
1314

1415

15-
def get_test_module(testid):
16-
return testid.split('::')[0]
16+
def test_get_module():
17+
assert 'test.py' == get_module('test.py')
18+
assert 'test.py' == get_module('test.py::test_foo')
19+
assert 'test.py' == get_module('test.py::test_foo[1]')
1720

21+
def test_get_function():
22+
assert None == get_function('test.py')
23+
assert 'test_foo' == get_function('test.py::test_foo')
24+
assert 'test_foo' == get_function('test.py::test_foo[1]')
1825

1926
def test_run_pytest_collect_failure(tests_dir):
2027
test1 = seq2p(tests_dir, 1)
@@ -148,7 +155,7 @@ def test_reduce(tests_dir, pollute_in_collect, fail_collect, r):
148155
reduction = r(tests_path=tests_dir, trace=True)
149156

150157
assert reduction['failed'] == failing
151-
assert reduction['modules'] == [get_test_module(polluter)]
158+
assert reduction['modules'] == [get_module(polluter)]
152159
assert reduction['tests'] == [] if pollute_in_collect else [polluter]
153160

154161

@@ -180,7 +187,7 @@ def test_nothing():
180187
reduction = r(tests_path=tests_dir, trace=True)
181188

182189
assert reduction['failed'] == failing
183-
assert reduction['modules'] == [get_test_module(polluter)]
190+
assert reduction['modules'] == [get_module(polluter)]
184191
assert reduction['tests'] == []
185192

186193

@@ -196,7 +203,7 @@ def test_reduce_pytest_args(tests_dir, pollute_in_collect, fail_collect, r):
196203
reduction = r(tests_path=tests_dir, trace=True, pytest_args=['--noconftest'])
197204

198205
assert reduction['failed'] == failing
199-
assert reduction['modules'] == [get_test_module(polluter)]
206+
assert reduction['modules'] == [get_module(polluter)]
200207
assert reduction['tests'] == [] if pollute_in_collect else [polluter]
201208

202209

0 commit comments

Comments
 (0)