From 4d4fc36a6f7dac63057c1de137f6e80585f0c24e Mon Sep 17 00:00:00 2001 From: Christian Kuhn Date: Sat, 30 Nov 2024 16:52:50 +0100 Subject: [PATCH] [TASK] Do not assertX but fail() in tearDown() (#660) phpunit has a best practice that tests should usually have at least one assertion to be sure they actually do something. All assertions thus raise a counter that is checked after test execution. If zero, phpunit marks the test risky with "test has no assertion". There are two ways to suppress this: * Setting beStrictAboutTestsThatDoNotTestAnything="false" via phpunit config * Adding #[DoesNotPerformAssertions] attribute to single tests to actively mark tests that do not assert something as legit Our abstract UnitTestCase spoils this by always doing assertions in tearDown(). The patch turns these assertions into check+fail() code instead. Unit tests that don't have assertions for whatever reason are now properly marked as risky as intended by phpunit. Releases: main --- Classes/Core/Unit/UnitTestCase.php | 34 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Classes/Core/Unit/UnitTestCase.php b/Classes/Core/Unit/UnitTestCase.php index b5804ee4..dcbaf24d 100644 --- a/Classes/Core/Unit/UnitTestCase.php +++ b/Classes/Core/Unit/UnitTestCase.php @@ -127,12 +127,13 @@ protected function tearDown(): void $singletonInstances = GeneralUtility::getSingletonInstances(); // Reset singletons anyway to not let all further tests fail GeneralUtility::resetSingletonInstances([]); - self::assertEmpty( - $singletonInstances, - 'tearDown() integrity check found left over singleton instances in GeneralUtility::makeInstance()' - . ' instance list. The test should probably set \'$this->resetSingletonInstances = true;\' to' - . ' reset this framework state change. Found singletons: ' . implode(', ', array_keys($singletonInstances)) - ); + if (!empty($singletonInstances)) { + self::fail( + 'tearDown() integrity check found left over singleton instances in GeneralUtility::makeInstance()' + . ' instance list. The test should probably set \'$this->resetSingletonInstances = true;\' to' + . ' reset this framework state change. Found singletons: ' . implode(', ', array_keys($singletonInstances)) + ); + } } // Delete registered test files and directories @@ -175,18 +176,19 @@ protected function tearDown(): void if (!empty($notCleanInstances)) { // Reset instance list (including singletons & container) to not let all further tests fail GeneralUtility::purgeInstances(); + self::fail( + 'tearDown() integrity check found left over instances in GeneralUtility::makeInstance() instance list.' + . ' Always consume instances added via GeneralUtility::addInstance() in your test by the test subject.' + . ' Found instances of these classes: ' . implode(', ', array_keys($notCleanInstances)) + ); } - // Let the test fail if there were instances left and give some message on why it fails - self::assertEquals( - [], - $notCleanInstances, - 'tearDown() integrity check found left over instances in GeneralUtility::makeInstance() instance list.' - . ' Always consume instances added via GeneralUtility::addInstance() in your test by the test subject.' - ); - self::assertTrue($this->setUpMethodCallChainValid, 'tearDown() integrity check detected that setUp has a ' - . 'broken parent call chain. Please check that setUp() methods properly calls parent::setUp(), starting from "' - . static::class . '"'); + if ($this->setUpMethodCallChainValid === false) { + self::fail( + 'tearDown() integrity check detected that setUp() has a broken parent call chain.' + . ' Please check that setUp() methods properly calls parent::setUp(), starting from "' . static::class . '"' + ); + } parent::tearDown(); }