From d69fdda10773aecde7694d4601a4a4f5b91fed31 Mon Sep 17 00:00:00 2001 From: ChristopheDEBOVE <34914918+ChristopheDEBOVE@users.noreply.github.com> Date: Fri, 9 Feb 2024 00:17:22 +0800 Subject: [PATCH] Can change function and module prefix in configuration (#116) --- pycrunch/discovery/simple.py | 7 ++++- pycrunch/session/configuration.py | 17 ++++++++++- .../dogfood/spec_discovery_prefix_demo.py | 2 ++ pycrunch_tests/tests_simple_discovery.py | 30 +++++++++++++------ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 pycrunch_tests/dogfood/spec_discovery_prefix_demo.py diff --git a/pycrunch/discovery/simple.py b/pycrunch/discovery/simple.py index afd99a5..8e5eac5 100644 --- a/pycrunch/discovery/simple.py +++ b/pycrunch/discovery/simple.py @@ -177,13 +177,18 @@ def is_subclass_of_unittest(self, function_or_variable_or_class): def is_module_with_tests(self, module_name): module_short_name = module_name.split('.')[-1] + return module_short_name.startswith(( 'test_', 'tests_', + *self.configuration.module_prefixes, )) or module_short_name.endswith(('_test', 'tests', '_tests')) def looks_like_test_name(self, v): - return v.startswith('test_') or v.endswith('_test') + return any([ + v.startswith(prefix) + for prefix in ['test_', *self.configuration.function_prefixes] + ]) or v.endswith('_test') def get_test_case_names_from_class(self, test_case_class): # implementation from standard unitest library diff --git a/pycrunch/session/configuration.py b/pycrunch/session/configuration.py index 3697569..38377eb 100644 --- a/pycrunch/session/configuration.py +++ b/pycrunch/session/configuration.py @@ -60,6 +60,8 @@ def __init__(self): self.enable_web_ui = False self.change_detection_root: str = self._get_working_directory() self.intellij_connector_version = 'unknown' + self.module_prefixes = [] + self.function_prefixes = [] def _get_working_directory(self): return str(Path('.').absolute()) @@ -165,7 +167,8 @@ def _load_runtime_configuration_engine(self, engine_config): if multiprocess_threshold: self.multiprocess_threshold_will_change(multiprocess_threshold) self.load_pytest_plugin_config(engine_config) - + self.load_module_prefixes_config(engine_config) + self.load_function_prefixes_config(engine_config) # this is in seconds execution_timeout = engine_config.get('timeout', None) if execution_timeout is not None: @@ -194,6 +197,18 @@ def load_pytest_plugin_config(self, engine_config): if isinstance(node, bool): self.load_pytest_plugins = node + def load_module_prefixes_config(self, engine_config): + node: Optional[str] = engine_config.get('module-prefixes', None) + if node is not None: + if isinstance(node, str): + self.module_prefixes = node.split(' ') + + def load_function_prefixes_config(self, engine_config): + node: Optional[str] = engine_config.get('function-prefixes', None) + if node is not None: + if isinstance(node, str): + self.function_prefixes = node.split(' ') + def deep_inheritance_will_change(self, engine_config): deep_inheritance: str = engine_config.get('deep-inheritance', None) if deep_inheritance is not None: diff --git a/pycrunch_tests/dogfood/spec_discovery_prefix_demo.py b/pycrunch_tests/dogfood/spec_discovery_prefix_demo.py new file mode 100644 index 0000000..b00afcd --- /dev/null +++ b/pycrunch_tests/dogfood/spec_discovery_prefix_demo.py @@ -0,0 +1,2 @@ +def should_regular_2(): + pass \ No newline at end of file diff --git a/pycrunch_tests/tests_simple_discovery.py b/pycrunch_tests/tests_simple_discovery.py index ab8036d..d314a86 100644 --- a/pycrunch_tests/tests_simple_discovery.py +++ b/pycrunch_tests/tests_simple_discovery.py @@ -1,9 +1,4 @@ -import io -import os from pathlib import Path -from pprint import pprint -from unittest import mock -from unittest.mock import mock_open, create_autospec from pycrunch.discovery.simple import SimpleTestDiscovery from pycrunch.session.configuration import Configuration @@ -21,6 +16,7 @@ def test_simple_discovery(): assert found_flag + def test_dir_walk(): for x in Path('/Users/gleb/code/PyCrunch/').glob('**/test*.py'): print(x.name) @@ -31,6 +27,7 @@ def test_module_with_tests_simple(): assert sut.is_module_with_tests('tests_simple') assert sut.is_module_with_tests('simple_tests') + def test_module_with_tests_nested(): sut = SimpleTestDiscovery('') assert sut.is_module_with_tests('nested.tests_simple') @@ -44,6 +41,7 @@ def test_only_methods_are_discovered_not_variables(): assert len(test_names) > 0 assert 'test_variable' not in test_names + def test_classes_with_unit_tests_are_discoverable(): actual = run_dogfood_discovery() test_names = list(map(lambda _: _.name, actual.tests)) @@ -55,11 +53,25 @@ def test_classes_with_unit_tests_are_discoverable(): assert 'TestForDummies::helper_method' not in test_names -def run_dogfood_discovery(): +def test_discovery_with_prefixes(): + conf = Configuration() + conf.function_prefixes.append("should_") + conf.module_prefixes.append("spec_") + actual = run_dogfood_discovery(conf) + + found_flag = False + for t in actual.tests: + if t.filename.endswith('spec_discovery_prefix_demo.py'): + if t.name == 'should_regular_2': + found_flag = True + break + + assert found_flag + + +def run_dogfood_discovery(configuration=None): root_folder = Path('.') current_folder = root_folder.joinpath('pycrunch_tests', 'dogfood').absolute() - sut = SimpleTestDiscovery(str(root_folder.absolute()), Configuration()) + sut = SimpleTestDiscovery(str(root_folder.absolute()), configuration or Configuration()) actual = sut.find_tests_in_folder(str(current_folder.absolute())) return actual - -