-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from jerry-git/cli-command-for-listing-slowest…
…-durations Introduce slowest-tests cli command
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import argparse | ||
import json | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Dict | ||
|
||
|
||
def list_slowest_tests(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--durations-path", | ||
help=( | ||
"Path to the file in which durations are stored, " | ||
"default is .test_durations in the current working directory" | ||
), | ||
default=".test_durations", | ||
type=argparse.FileType(), | ||
) | ||
parser.add_argument( | ||
"-c", | ||
"--count", | ||
help="How many slowest to list", | ||
default=10, | ||
type=int, | ||
) | ||
args = parser.parse_args() | ||
return _list_slowest_tests(json.load(args.durations_path), args.count) | ||
|
||
|
||
def _list_slowest_tests(durations: "Dict[str, float]", count: int) -> None: | ||
slowest_tests = tuple(sorted(durations.items(), key=lambda item: item[1], reverse=True))[:count] | ||
for test, duration in slowest_tests: | ||
print(f"{duration:.2f} {test}") # noqa: T001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import argparse | ||
import json | ||
import sys | ||
from io import StringIO | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from pytest_split import cli | ||
|
||
|
||
@pytest.fixture | ||
def durations_file(tmpdir): | ||
durations_path = str(tmpdir.join(".durations")) | ||
durations = {f"test_{i}": float(i) for i in range(1, 11)} | ||
with open(durations_path, "w") as f: | ||
json.dump(durations, f) | ||
with open(durations_path, "r") as f: | ||
yield f | ||
|
||
|
||
def test_slowest_tests(durations_file): | ||
with patch("pytest_split.cli.argparse.ArgumentParser", autospec=True) as arg_parser, patch( | ||
"sys.stdout", new_callable=StringIO | ||
): | ||
arg_parser().parse_args.return_value = argparse.Namespace(durations_path=durations_file, count=3) | ||
cli.list_slowest_tests() | ||
|
||
output = sys.stdout.getvalue() | ||
assert output == "10.00 test_10\n" "9.00 test_9\n" "8.00 test_8\n" |