In web requests, DeferredUpdates are executed at the end of the requests, while in maintenance scripts, they are executed immediately – this is implemented by checking $wgCommandLineMode (https://gerrit.wikimedia.org/g/mediawiki/core/+/6c07e2acf1e6e39831c2b4cab8ab62feb4096277/includes/deferred/DeferredUpdatesScopeMediaWikiStack.php#55).
$wgCommandLineMode is also true when running tests, and we have a number of test cases that override it to false to prevent DeferredUpdates from executing immediately, because they interfere with the test setup in some way.
Since I'd like to get rid of $wgCommandLineMode in T313841, they need to be updated to use something else. I'm hopeful that DeferredUpdates::preventOpportunisticUpdates() will work, it's a relatively new thing (84f0876b8302cd7d7ec727b2ed5daa49b6bddcc5) with a similar effect (not exactly the same, but hopefully close enough).