Skip to content

Commit

Permalink
Add --store-durations back
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg committed May 30, 2021
1 parent 7161e05 commit 0cbb911
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/pytest_split/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def pytest_addoption(parser: "Parser") -> None:
"Run first the whole suite with --store-durations to save information "
"about test execution times"
)
group.addoption(
"--store-durations",
dest="store_durations",
action="store_true",
help="Store durations into '--durations-path'",
)
group.addoption(
"--splits",
dest="splits",
Expand Down Expand Up @@ -67,9 +73,12 @@ def pytest_configure(config: "Config") -> None:
elif config.option.splits and config.option.group:
# Register plugin to run only if we received a splits and group arg
config.pluginmanager.register(PytestSplitPlugin(config), "pytestsplitplugin")
config.pluginmanager.register(PytestSplitCachePlugin(config), "pytestsplitcacheplugin")
elif config.option.store_durations:
config.pluginmanager.register(PytestSplitCachePlugin(config), "pytestsplitcacheplugin")


class PytestSplitPlugin:
class PytestSplitBasePlugin:
cache_file = "cache/pytest-split"

def __init__(self, config: "Config") -> None:
Expand All @@ -93,6 +102,9 @@ def __init__(self, config: "Config") -> None:
"when test timings have been documented."
)


class PytestSplitPlugin(PytestSplitBasePlugin):

@hookimpl(hookwrapper=True, tryfirst=True)
def pytest_collection_modifyitems(self, config: "Config", items: "List[nodes.Item]") -> Generator[None, None, None]:
"""
Expand Down Expand Up @@ -122,10 +134,10 @@ def pytest_collection_modifyitems(self, config: "Config", items: "List[nodes.Ite

@staticmethod
def _split_tests(
splits: int,
group: int,
items: "List[nodes.Item]",
stored_durations: OrderedDict,
splits: int,
group: int,
items: "List[nodes.Item]",
stored_durations: OrderedDict,
) -> Tuple[int, int]:
"""
Split tests by runtime.
Expand Down Expand Up @@ -201,6 +213,9 @@ def _split_tests(

return selected, deselected


class PytestSplitCachePlugin(PytestSplitBasePlugin):

def pytest_sessionfinish(self) -> None:
"""
Write test runtimes to cache.
Expand All @@ -219,8 +234,8 @@ def pytest_sessionfinish(self) -> None:
if test_report.duration < 0:
continue
if (
getattr(test_report, "when", "") in ("teardown", "setup")
and test_report.duration > STORE_DURATIONS_SETUP_AND_TEARDOWN_THRESHOLD
getattr(test_report, "when", "") in ("teardown", "setup")
and test_report.duration > STORE_DURATIONS_SETUP_AND_TEARDOWN_THRESHOLD
):
# Ignore not legit teardown durations
continue
Expand Down

0 comments on commit 0cbb911

Please sign in to comment.