﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32540	"Only do ""top level"" detection in DiscoverRunner.build_suite() when needed"	Chris Jerdonek	Chris Jerdonek	"Currently, `DiscoverRunner.build_suite()` is a bit confusing to read because it does [https://github.com/django/django/blob/551b0f94bf62c81d9ff9d5f7e261bcd2a594a4d1/django/test/runner.py#L576-L599 ""top level"" detection] for test discovery even when discovery won't be taking place.

My suggestion is to move the top-level detection logic (and large code comment) into a `find_top_level(top_level)` function that is placed immediately before where `is_discoverable()` is defined. Then, only call `find_top_level()` when needed, namely right before where [https://github.com/django/django/blob/551b0f94bf62c81d9ff9d5f7e261bcd2a594a4d1/django/test/runner.py#L603 self.test_loader.discover()] is called.

Similarly, the `kwargs = discover_kwargs.copy()` line can also go into that if block right before that line, because that's the only code path where `kwargs` is actually used. Without this change, it's hard for one to notice that the `kwargs` are in fact only used for that one line.

Together, this will cut down on the size of `build_suite()` quite a bit and make it easier to understand.
"	Cleanup/optimization	closed	Testing framework	dev	Normal	fixed	DiscoverRunner,build_suite,discovery		Ready for checkin	1	0	0	0	0	0
