Skip to content

Commit

Permalink
Add support of 'auto' scope (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
yugokato authored Dec 13, 2024
1 parent f1e0e9b commit b06f2f4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Smoke testing:
The plugin provides the following predefined scopes:
- function: Applies to each test function (default)
- class: Applies to each test class
- auto: Applies function scope for test functions, class scope for test methods
- file: Applies to each test file
- all: Applies to the entire test suite
NOTE: You can also implement your own custom scopes using a hook
Expand Down
5 changes: 4 additions & 1 deletion src/pytest_smoke/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class SmokeScope(StrEnum):
FUNCTION = auto()
CLASS = auto()
AUTO = auto()
FILE = auto()
ALL = auto()

Expand Down Expand Up @@ -77,6 +78,8 @@ def pytest_addoption(parser: Parser):
"The plugin provides the following predefined scopes:\n"
f"- {SmokeScope.FUNCTION}: Applies to each test function (default)\n"
f"- {SmokeScope.CLASS}: Applies to each test class\n"
f"- {SmokeScope.AUTO}: Applies {SmokeScope.FUNCTION} scope for test functions, "
f"{SmokeScope.CLASS} scope for test methods\n"
f"- {SmokeScope.FILE}: Applies to each test file\n"
f"- {SmokeScope.ALL}: Applies to the entire test suite\n"
"NOTE: You can also implement your own custom scopes using a hook"
Expand Down Expand Up @@ -230,7 +233,7 @@ def _generate_group_id(item: Item, scope: str) -> Optional[str]:

if cls:
group_id += f"::{cls.__name__}"
if scope == SmokeScope.CLASS:
if scope in [SmokeScope.CLASS, SmokeScope.AUTO]:
return group_id

# The default scope
Expand Down
33 changes: 28 additions & 5 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,41 @@ def get_num_tests_to_be_selected(test_file_specs: list[TestFileSpec], n: str | N
scale = float(n[:-1])
if scope == SmokeScope.ALL:
num_expected_tests = scale_down(get_num_tests(*test_file_specs), scale)
elif scope == SmokeScope.CLASS:
num_expected_tests = sum([int(scale_down(get_num_tests(x), scale)) for x in test_class_specs])
elif scope == SmokeScope.FILE:
num_expected_tests = sum([int(scale_down(get_num_tests(*x.test_specs), scale)) for x in test_file_specs])
elif scope == SmokeScope.AUTO:
num_expected_tests = sum(
[
*(int(scale_down(get_num_tests(x), scale)) for x in test_class_specs),
*(
int(scale_down(get_num_tests(x), scale))
for x in get_test_func_specs(test_file_specs, exclude_class=True)
),
]
)
elif scope == SmokeScope.CLASS:
num_expected_tests = sum([int(scale_down(get_num_tests(x), scale)) for x in test_class_specs])
else:
num_expected_tests = sum(
[int(scale_down(get_num_tests(x), scale)) for x in get_test_func_specs(test_file_specs)]
)
else:
if scope == SmokeScope.ALL:
num_expected_tests = min([int(n), get_num_tests(*test_file_specs)])
elif scope == SmokeScope.CLASS:
num_expected_tests = sum([min([int(n), get_num_tests(x)]) for x in test_class_specs])
elif scope == SmokeScope.FILE:
num_expected_tests = sum([min([int(n), get_num_tests(*x.test_specs)]) for x in test_file_specs])
elif scope == SmokeScope.AUTO:
num_expected_tests = sum(
[
*(min([int(n), get_num_tests(x)]) for x in test_class_specs),
*(
min([int(n), get_num_tests(x)])
for x in get_test_func_specs(test_file_specs, exclude_class=True)
),
]
)
elif scope == SmokeScope.CLASS:
num_expected_tests = sum([min([int(n), get_num_tests(x)]) for x in test_class_specs])
else:
num_expected_tests = sum([min([int(n), get_num_tests(x)]) for x in get_test_func_specs(test_file_specs)])

Expand All @@ -184,10 +204,13 @@ def get_test_class_specs(test_file_specs: list[TestFileSpec]) -> list[TestClassS
return [x for x in test_specs if isinstance(x, TestClassSpec)]


def get_test_func_specs(test_file_specs: list[TestFileSpec]) -> list[TestFuncSpec]:
def get_test_func_specs(test_file_specs: list[TestFileSpec], exclude_class: bool = False) -> list[TestFuncSpec]:
"""Returns all TestFuncSpec objects from test file specs
:param test_file_specs: Test file specs
:param exclude_class: Exclude test func specs defined under a test class spec
"""
test_specs = list(chain(*[x.test_specs for x in test_file_specs]))
if exclude_class:
test_specs = [x for x in test_specs if not isinstance(x, TestClassSpec)]
return list(chain(*[(x.test_func_specs if isinstance(x, TestClassSpec) else [x]) for x in test_specs]))

0 comments on commit b06f2f4

Please sign in to comment.