Skip to content

Commit

Permalink
Can change function and module prefix in configuration (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopheDEBOVE authored Feb 8, 2024
1 parent 241b825 commit d69fdda
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
7 changes: 6 additions & 1 deletion pycrunch/discovery/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion pycrunch/session/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions pycrunch_tests/dogfood/spec_discovery_prefix_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def should_regular_2():
pass
30 changes: 21 additions & 9 deletions pycrunch_tests/tests_simple_discovery.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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')
Expand All @@ -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))
Expand All @@ -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


0 comments on commit d69fdda

Please sign in to comment.