From 40b462c3b36d87c7adf6b79e6904aeb7d6102742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kone=C4=8Dn=C3=BD?= Date: Sat, 28 Dec 2024 21:52:48 +0100 Subject: [PATCH] added option to suppress warning about no performed assertions in a test method/test case this closes #54 --- CHANGELOG.md | 1 + README.md | 17 ++++++++++++++++- RELEASE_NOTES | 2 +- src/Attributes/NoAssertions.php | 18 ++++++++++++++++++ src/TestCase.php | 18 +++++++++++++----- tests/TestCaseTest.php | 2 ++ 6 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/Attributes/NoAssertions.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 20eae78..049f312 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Version 8.0.0-dev - InfoExtension (when added to automated tests runner) prints active extensions - BC break: added method getSubscribedEvents to interface IResultsFormatter, removed methods report* (it is now an event subscriber) - data providers can now also return iterable objects not just arrays +- added option to suppress warning about no performed assertions in a test method/test case Version 7.3.1 - allowed installation konecnyjakub/event-dispatcher 2 diff --git a/README.md b/README.md index 5035616..d957c4e 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,22 @@ final class Tests extends MyTester\TestCase ``` #### Tests without assertions -By default, if a test method performs no assertions, it is reported as passed with warnings. If you do not set a different results formatter (see below), it will print a warning `Method name passed with warning: No assertions were performed.`. +By default, if a test method performs no assertions, it is reported as passed with warnings. If you do not set a different results formatter (see below), it will print a warning `Method name passed with warning: No assertions were performed.`. It is possible to suppress that warning by adding attribute NoAssertions on a test method or the whole class, then it is reported as passed (assuming there are no other issues). Example: + +```php +totalAssertions = $this->getCounter(); - if ($job->totalAssertions === 0) { + $checkAssertions = + !$this->annotationsReader->hasAnnotation(static::ANNOTATION_NO_ASSERTIONS, static::class) && + !$this->annotationsReader->hasAnnotation( + static::ANNOTATION_NO_ASSERTIONS, + static::class, + $methodName + ); + if ($checkAssertions && $job->totalAssertions === 0) { echo "Warning: No assertions were performed.\n"; } }, @@ -115,7 +123,7 @@ protected function getJobs(): array "callback" => $callback, "params" => [], "skip" => $this->skipChecker->shouldSkip(static::class, $method), - "onAfterExecute" => $this->getJobAfterExecuteCallbacks(), + "onAfterExecute" => $this->getJobAfterExecuteCallbacks($method), "dataSetName" => "", "reportDeprecations" => $this->shouldReportDeprecations($method), ]; diff --git a/tests/TestCaseTest.php b/tests/TestCaseTest.php index 168f27a..53d2644 100644 --- a/tests/TestCaseTest.php +++ b/tests/TestCaseTest.php @@ -5,6 +5,7 @@ use MyTester\Attributes\DataProvider as DataProviderAttribute; use MyTester\Attributes\IgnoreDeprecations; +use MyTester\Attributes\NoAssertions; use MyTester\Attributes\RequiresOsFamily; use MyTester\Attributes\RequiresPhpExtension; use MyTester\Attributes\RequiresPhpVersion; @@ -161,6 +162,7 @@ public function testSkipOsFamily(): void } #[Test("No assertions")] + #[NoAssertions] public function testNoAssertions(): void { }