Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/keep already run testcases #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add feature keep existing test runtime
haicheviet committed Oct 11, 2023
commit 1011b7c2a5dc9488418fee9e9876124ce3b6ebd4
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -44,4 +44,3 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
[0.6.0]: https://github.com/jerry-git/pytest-split/compare/0.5.0...0.6.0
[0.5.0]: https://github.com/jerry-git/pytest-split/compare/0.4.0...0.5.0
[0.4.0]: https://github.com/jerry-git/pytest-split/tree/0.4.0

11 changes: 9 additions & 2 deletions src/pytest_split/plugin.py
Original file line number Diff line number Diff line change
@@ -33,8 +33,11 @@ def pytest_addoption(parser: "Parser") -> None:
group.addoption(
"--store-durations",
dest="store_durations",
action="store_true",
help="Store durations into '--durations-path'.",
type=str,
choices=["keep", "replace"],
const="replace",
nargs="?",
)
group.addoption(
"--durations-path",
@@ -187,7 +190,7 @@ class PytestSplitCachePlugin(Base):
The cache plugin writes durations to our durations file.
"""

def pytest_sessionfinish(self) -> None:
def pytest_sessionfinish(self) -> None: # noqa: C901
"""
Method is called by Pytest after the test-suite has run.
https://github.com/pytest-dev/pytest/blob/main/src/_pytest/main.py#L308
@@ -216,6 +219,10 @@ def pytest_sessionfinish(self) -> None:

if self.config.option.clean_durations:
self.cached_durations = dict(test_durations)
elif self.config.option.store_durations == "keep":
for k, v in test_durations.items():
if k not in self.cached_durations:
self.cached_durations[k] = v
else:
for k, v in test_durations.items():
self.cached_durations[k] = v
60 changes: 49 additions & 11 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -27,28 +27,66 @@ def durations_path(tmpdir):


class TestStoreDurations:
def test_it_stores(self, example_suite, durations_path):
def test_it_stores_replace(self, example_suite, durations_path):
example_suite.runpytest("--store-durations", "--durations-path", durations_path)

with open(durations_path) as f:
durations = json.load(f)

assert list(durations.keys()) == [
"test_it_stores.py::test_1",
"test_it_stores.py::test_10",
"test_it_stores.py::test_2",
"test_it_stores.py::test_3",
"test_it_stores.py::test_4",
"test_it_stores.py::test_5",
"test_it_stores.py::test_6",
"test_it_stores.py::test_7",
"test_it_stores.py::test_8",
"test_it_stores.py::test_9",
"test_it_stores_replace.py::test_1",
"test_it_stores_replace.py::test_10",
"test_it_stores_replace.py::test_2",
"test_it_stores_replace.py::test_3",
"test_it_stores_replace.py::test_4",
"test_it_stores_replace.py::test_5",
"test_it_stores_replace.py::test_6",
"test_it_stores_replace.py::test_7",
"test_it_stores_replace.py::test_8",
"test_it_stores_replace.py::test_9",
]

for duration in durations.values():
assert isinstance(duration, float)

def test_it_stores_keep(self, example_suite, durations_path):
example_suite.runpytest("--store-durations", "--durations-path", durations_path)

with open(durations_path) as f:
durations = json.load(f)

default_keys = [
"test_it_stores_keep.py::test_1",
"test_it_stores_keep.py::test_10",
"test_it_stores_keep.py::test_2",
"test_it_stores_keep.py::test_3",
"test_it_stores_keep.py::test_4",
"test_it_stores_keep.py::test_5",
"test_it_stores_keep.py::test_6",
"test_it_stores_keep.py::test_7",
"test_it_stores_keep.py::test_8",
"test_it_stores_keep.py::test_9",
]

assert list(durations.keys()) == default_keys

for duration in durations.values():
assert isinstance(duration, float)

example_suite.makepyfile("def test_11(): pass")
example_suite.runpytest(
"--store-durations", "keep", "--durations-path", durations_path
)

with open(durations_path) as f:
durations_keep = json.load(f)
assert list(durations_keep.keys()) == [
*default_keys,
"test_it_stores_keep0/test_it_stores_keep.py::test_11",
]
for k in default_keys:
assert durations_keep[k] == durations[k]

def test_it_overrides_existing_durations(self, example_suite, durations_path):
existing_duration_test_name = "test_it_overrides_existing_durations0/test_it_overrides_existing_durations.py::test_1"
old_value = 99