From 7e38a8d66f9a4fbbf373de30f1cc6be906502559 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 3 Aug 2021 09:52:32 +0200 Subject: [PATCH] Refs #31621 -- Added more tests for test --parallel option. --- tests/test_runner/test_discover_runner.py | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_runner/test_discover_runner.py b/tests/test_runner/test_discover_runner.py index 2fc4df3133..327a6625ae 100644 --- a/tests/test_runner/test_discover_runner.py +++ b/tests/test_runner/test_discover_runner.py @@ -1,4 +1,5 @@ import logging +import multiprocessing import os import unittest.loader from argparse import ArgumentParser @@ -36,6 +37,40 @@ def change_loader_patterns(patterns): DiscoverRunner.test_loader.testNamePatterns = original_patterns +# Isolate from the real environment. +@mock.patch.dict(os.environ, {}, clear=True) +@mock.patch.object(multiprocessing, 'cpu_count', return_value=12) +# Python 3.8 on macOS defaults to 'spawn' mode. +@mock.patch.object(multiprocessing, 'get_start_method', return_value='fork') +class DiscoverRunnerParallelArgumentTests(SimpleTestCase): + def get_parser(self): + parser = ArgumentParser() + DiscoverRunner.add_arguments(parser) + return parser + + def test_parallel_default(self, *mocked_objects): + result = self.get_parser().parse_args([]) + self.assertEqual(result.parallel, 1) + + def test_parallel_flag(self, *mocked_objects): + result = self.get_parser().parse_args(['--parallel']) + self.assertEqual(result.parallel, 12) + + def test_parallel_count(self, *mocked_objects): + result = self.get_parser().parse_args(['--parallel', '17']) + self.assertEqual(result.parallel, 17) + + @mock.patch.dict(os.environ, {'DJANGO_TEST_PROCESSES': 'typo'}) + def test_parallel_env_var_non_int(self, *mocked_objects): + with self.assertRaises(ValueError): + self.get_parser().parse_args([]) + + def test_parallel_spawn(self, mocked_get_start_method, mocked_cpu_count): + mocked_get_start_method.return_value = 'spawn' + result = self.get_parser().parse_args(['--parallel']) + self.assertEqual(result.parallel, 1) + + class DiscoverRunnerTests(SimpleTestCase): @staticmethod