diff options
| author | David Smith <smithdc@gmail.com> | 2023-06-14 19:22:48 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-08-31 07:14:58 +0200 |
| commit | 74b5074174d1749ee44df2f7ed418010a7a4ac70 (patch) | |
| tree | a533ff3b0e3db36baa991081d012d4554acbef28 /tests/test_runner | |
| parent | 27b399d23531541d091886f683991e321c8fa314 (diff) | |
Fixed #34210 -- Added unittest's durations option to the test runner.
Diffstat (limited to 'tests/test_runner')
| -rw-r--r-- | tests/test_runner/test_discover_runner.py | 17 | ||||
| -rw-r--r-- | tests/test_runner/test_parallel.py | 14 | ||||
| -rw-r--r-- | tests/test_runner/tests.py | 27 |
3 files changed, 55 insertions, 3 deletions
diff --git a/tests/test_runner/test_discover_runner.py b/tests/test_runner/test_discover_runner.py index bca9037492..5fc35b7bf2 100644 --- a/tests/test_runner/test_discover_runner.py +++ b/tests/test_runner/test_discover_runner.py @@ -16,6 +16,7 @@ from django.test.utils import ( captured_stderr, captured_stdout, ) +from django.utils.version import PY312 @contextmanager @@ -765,6 +766,22 @@ class DiscoverRunnerTests(SimpleTestCase): failures = runner.suite_result(suite, result) self.assertEqual(failures, expected_failures) + @unittest.skipUnless(PY312, "unittest --durations option requires Python 3.12") + def test_durations(self): + with captured_stderr() as stderr, captured_stdout(): + runner = DiscoverRunner(durations=10) + suite = runner.build_suite(["test_runner_apps.simple.tests.SimpleCase1"]) + runner.run_suite(suite) + self.assertIn("Slowest test durations", stderr.getvalue()) + + @unittest.skipUnless(PY312, "unittest --durations option requires Python 3.12") + def test_durations_debug_sql(self): + with captured_stderr() as stderr, captured_stdout(): + runner = DiscoverRunner(durations=10, debug_sql=True) + suite = runner.build_suite(["test_runner_apps.simple.SimpleCase1"]) + runner.run_suite(suite) + self.assertIn("Slowest test durations", stderr.getvalue()) + class DiscoverRunnerGetDatabasesTests(SimpleTestCase): runner = DiscoverRunner(verbosity=2) diff --git a/tests/test_runner/test_parallel.py b/tests/test_runner/test_parallel.py index eea9e4de74..e83f53bf4e 100644 --- a/tests/test_runner/test_parallel.py +++ b/tests/test_runner/test_parallel.py @@ -4,7 +4,7 @@ import unittest from django.test import SimpleTestCase from django.test.runner import RemoteTestResult -from django.utils.version import PY311 +from django.utils.version import PY311, PY312 try: import tblib.pickling_support @@ -118,7 +118,11 @@ class RemoteTestResultTest(SimpleTestCase): subtest_test.run(result=result) events = result.events - self.assertEqual(len(events), 4) + # addDurations added in Python 3.12. + if PY312: + self.assertEqual(len(events), 5) + else: + self.assertEqual(len(events), 4) self.assertIs(result.wasSuccessful(), False) event = events[1] @@ -133,3 +137,9 @@ class RemoteTestResultTest(SimpleTestCase): event = events[2] self.assertEqual(repr(event[3][1]), "AssertionError('2 != 1')") + + @unittest.skipUnless(PY312, "unittest --durations option requires Python 3.12") + def test_add_duration(self): + result = RemoteTestResult() + result.addDuration(None, 2.3) + self.assertEqual(result.collectedDurations, [("None", 2.3)]) diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index 0643bf93a1..25b7f40e99 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -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) |
