1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #34210 -- Added unittest's durations option to the test runner.

This commit is contained in:
David Smith
2023-06-14 19:22:48 +01:00
committed by Mariusz Felisiak
parent 27b399d235
commit 74b5074174
8 changed files with 110 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ from django import db
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django.core.management.base import SystemCheckError
from django.core.management.base import CommandError, SystemCheckError
from django.test import SimpleTestCase, TransactionTestCase, skipUnlessDBFeature
from django.test.runner import (
DiscoverRunner,
@@ -31,6 +31,7 @@ from django.test.utils import (
get_unique_databases_and_mirrors,
iter_test_cases,
)
from django.utils.version import PY312
from .models import B, Person, Through
@@ -451,6 +452,8 @@ class MockTestRunner:
def __init__(self, *args, **kwargs):
if parallel := kwargs.get("parallel"):
sys.stderr.write(f"parallel={parallel}")
if durations := kwargs.get("durations"):
sys.stderr.write(f"durations={durations}")
MockTestRunner.run_tests = mock.Mock(return_value=[])
@@ -475,6 +478,28 @@ class ManageCommandTests(unittest.TestCase):
)
self.assertIn("Total run took", stderr.getvalue())
@unittest.skipUnless(PY312, "unittest --durations option requires Python 3.12")
def test_durations(self):
with captured_stderr() as stderr:
call_command(
"test",
"--durations=10",
"sites",
testrunner="test_runner.tests.MockTestRunner",
)
self.assertIn("durations=10", stderr.getvalue())
@unittest.skipIf(PY312, "unittest --durations option requires Python 3.12")
def test_durations_lt_py312(self):
msg = "Error: unrecognized arguments: --durations=10"
with self.assertRaises(CommandError, msg=msg):
call_command(
"test",
"--durations=10",
"sites",
testrunner="test_runner.tests.MockTestRunner",
)
# Isolate from the real environment.
@mock.patch.dict(os.environ, {}, clear=True)