Don't run _pre_setup() twice for transactional tests - #1293
Open
vokracko wants to merge 2 commits into
Open
Conversation
Records who calls _pre_setup(). A plain django_db test is set up once, by us. A django_db(transaction=True) test is set up twice: TransactionTestCase.setUpClass() runs _pre_setup() eagerly and we then run it again.
TransactionTestCase.setUpClass() runs _pre_setup() eagerly and flags it in _pre_setup_ran_eagerly, which unittest.TestCase.run() clears instead of setting the test up again. We drive the test case ourselves, so do the same. Repeating _pre_setup() redid the whole fixture setup - serialized rollback restore, fixtures, sequence reset - on every transactional test. With available_apps it also sent setting_changed(enter=True) and post_migrate twice against the single exit in _post_teardown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since Django 5.2,
TransactionTestCase.setUpClass()callscls._pre_setup()eagerly and sets_pre_setup_ran_eagerlyso that whoever drives the test case can skip its own call, the wayunittest.TestCase.run()does._django_db_helper()callssetUpClass()and thentest_case._pre_setup()unconditionally, so everydjango_db(transaction=True)test has its fixtures set up twice: theserialized_rollbackrestore, thefixturesload and the sequence reset all run again. On a suite of 121 transactional tests that was 567s against 445s, roughly 1s per test.The first commit adds a test recording which frames call
_pre_setup(). It passes on currentmainand asserts the duplicate: a plaindjango_dbtest records one call, a transactional one recordssetUpClassand then a second call from_django_db_helper().The second commit consumes
_pre_setup_ran_eagerlyin_django_db_helper()and flips that assertion to the single call. Verified against Django 5.2; the eager call is unchanged on 6.0 and main.