From a218a748be9d1587994dce3019fc95300934f033 Mon Sep 17 00:00:00 2001 From: Juan Altmayer Pizzorno Date: Sat, 15 Jun 2024 14:45:38 -0400 Subject: [PATCH] - added get_function, to parse out the test function name out of a test node id; - added missing tests; --- src/pytest_cleanslate/reduce.py | 9 +++++++-- tests/test_reduce.py | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/pytest_cleanslate/reduce.py b/src/pytest_cleanslate/reduce.py index d47e291..a62988e 100644 --- a/src/pytest_cleanslate/reduce.py +++ b/src/pytest_cleanslate/reduce.py @@ -138,10 +138,15 @@ def get_module(testid: str) -> str: return testid.split('::')[0] -def is_module(testid: str) -> bool: +def _is_module(testid: str) -> bool: return '::' not in testid +def get_function(testid: str) -> str: + if '::' in testid: + return testid.split('::')[1].split('[')[0] + + def run_pytest(tests_path: Path, pytest_args=(), *, modules: T.List[Path] = None, tests: T.List[str] = None, trace: bool = False) -> dict: import tempfile @@ -261,7 +266,7 @@ def reduce(*, tests_path: Path, results: Results = None, pytest_args: T.List[str 'error': 'No tests failed', } - failed_is_module = is_module(failed_id) + failed_is_module = _is_module(failed_id) if failed_is_module: if trace: print() print(f"Module \"{failed_id}\"'s collection failed; trying it by itself...", flush=True) diff --git a/tests/test_reduce.py b/tests/test_reduce.py index 90303bb..8c28477 100644 --- a/tests/test_reduce.py +++ b/tests/test_reduce.py @@ -9,12 +9,19 @@ import pytest_cleanslate.reduce as reduce -from pytest_cleanslate.reduce import MODULE_LIST_ARG, TEST_LIST_ARG, RESULTS_ARG, Results +from pytest_cleanslate.reduce import MODULE_LIST_ARG, TEST_LIST_ARG, RESULTS_ARG, Results, \ + get_module, get_function -def get_test_module(testid): - return testid.split('::')[0] +def test_get_module(): + assert 'test.py' == get_module('test.py') + assert 'test.py' == get_module('test.py::test_foo') + assert 'test.py' == get_module('test.py::test_foo[1]') +def test_get_function(): + assert None == get_function('test.py') + assert 'test_foo' == get_function('test.py::test_foo') + assert 'test_foo' == get_function('test.py::test_foo[1]') def test_run_pytest_collect_failure(tests_dir): test1 = seq2p(tests_dir, 1) @@ -148,7 +155,7 @@ def test_reduce(tests_dir, pollute_in_collect, fail_collect, r): reduction = r(tests_path=tests_dir, trace=True) assert reduction['failed'] == failing - assert reduction['modules'] == [get_test_module(polluter)] + assert reduction['modules'] == [get_module(polluter)] assert reduction['tests'] == [] if pollute_in_collect else [polluter] @@ -180,7 +187,7 @@ def test_nothing(): reduction = r(tests_path=tests_dir, trace=True) assert reduction['failed'] == failing - assert reduction['modules'] == [get_test_module(polluter)] + assert reduction['modules'] == [get_module(polluter)] assert reduction['tests'] == [] @@ -196,7 +203,7 @@ def test_reduce_pytest_args(tests_dir, pollute_in_collect, fail_collect, r): reduction = r(tests_path=tests_dir, trace=True, pytest_args=['--noconftest']) assert reduction['failed'] == failing - assert reduction['modules'] == [get_test_module(polluter)] + assert reduction['modules'] == [get_module(polluter)] assert reduction['tests'] == [] if pollute_in_collect else [polluter]