﻿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
32668	Separate test-collection setup from runtests.py's setup() for use in get_app_test_labels()	Chris Jerdonek	nobody	"This is another clean-up follow-up to [https://github.com/django/django/pull/4106 PR #4106], similar to [https://github.com/django/django/pull/14276 PR #14276].

TLDR: Refactor out from `runtests.py`'s 125-line [https://github.com/django/django/blob/54e94640ace261b14cf8cdb1fae3dc6f068a5f87/tests/runtests.py#L135 setup()] and [https://github.com/django/django/blob/54e94640ace261b14cf8cdb1fae3dc6f068a5f87/tests/runtests.py#L263 teardown()] simpler `setup_test_collection()` and `teardown_test_collection()` functions for use in the new [https://github.com/django/django/blob/54e94640ace261b14cf8cdb1fae3dc6f068a5f87/tests/runtests.py#L337-L342 get_app_test_labels()] (used for `bisect_tests()` and `paired_tests()`). (The exact names aren't so important.)

Longer version:

Currently, `runtests.py`'s `setup()` function and its role in getting the list of default test labels for `bisect_tests()`, `paired_tests()`, and [https://github.com/django/django/blob/413c15ef2e3d3958fb641a023bc1e2d15fb2b228/tests/runtests.py#L332 test_runner.run_tests()] is a bit complicated and harder to understand than it needs to be.

There are a couple reasons for this. First, `setup()` is actually doing two kinds of setup: one for collecting the default test labels, and one for setting up the test run (needed only for [https://github.com/django/django/blob/54e94640ace261b14cf8cdb1fae3dc6f068a5f87/tests/runtests.py#L301 django_tests()] but never `bisect_tests()` and `paired_tests()`). Second, the way the list of default test labels is obtained is a bit roundabout. Namely, a bunch of apps are added to `INSTALLED_APPS`, and then `runtests.py`'s `get_installed()` is called to read from `INSTALLED_APPS`. However, for test-collection, `INSTALLED_APPS` doesn't actually need to be modified. You can see a side effect of this in the fact that `get_installed()` doesn't just return test labels. It also returns the following modules, which no longer contain any tests (this is the thing that [https://github.com/django/django/pull/4106 PR #4106] from six years ago fixed): 
`django.contrib.admin`, `django.contrib.auth`, `django.contrib.contenttypes`, `django.contrib.flatpages`, `django.contrib.messages`, `django.contrib.redirects`, `django.contrib.sessions`, `django.contrib.sites`, `django.contrib.staticfiles`.

By extracting out from `setup()` a `setup_test_collection()` function that just collects and returns the top-level, filtered test labels, I think things will become a lot easier to understand and work with -- not just for `bisect_tests()` and `paired_tests()`, but also for the main `django_tests()` case.
"	Cleanup/optimization	new	Testing framework	dev	Normal				Unreviewed	0	0	0	0	0	0
