Skip to content

Commit

Permalink
added option to suppress warning about no performed assertions in a t…
Browse files Browse the repository at this point in the history
…est method/test case

this closes #54
  • Loading branch information
konecnyjakub committed Dec 28, 2024
1 parent 33107d9 commit 40b462c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
declare(strict_types=1);

use MyTester\Attributes\NoAssertions;

final class Tests extends MyTester\TestCase
{
#[NoAssertions]
public function testNoAssertions(): void
{
}
}
```

#### Unexpected errors/exceptions

Expand Down
2 changes: 1 addition & 1 deletion RELEASE_NOTES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A new version of My Tester is out. It is only for Composer 2.2 or later. It removes previously deprecated stuff. Data providers can now also return iterable objects not just arrays.
A new version of My Tester is out. It is only for Composer 2.2 or later. It removes previously deprecated stuff. Data providers can now also return iterable objects not just arrays. It is possible to suppress warnings about no performed assertion on a test method/test case with attribute NoAssertions.

Interface ITesterExtension was changed - it has new method getName and was changed into an event subscriber.

Expand Down
18 changes: 18 additions & 0 deletions src/Attributes/NoAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace MyTester\Attributes;

use Attribute;

/**
* No assertions attribute
*
* Suppresses a warning about no performed assertions in a test
*
* @author Jakub Konečný
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
final class NoAssertions
{
}
18 changes: 13 additions & 5 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ abstract class TestCase
public const string ANNOTATION_TEST_SUITE = "testSuite";
/** @internal */
public const string ANNOTATION_IGNORE_DEPRECATIONS = "ignoreDeprecations";
/** @internal */
public const string ANNOTATION_NO_ASSERTIONS = "noAssertions";

protected ISkipChecker $skipChecker;
protected IDataProvider $dataProvider;
Expand Down Expand Up @@ -68,16 +70,22 @@ function (ReflectionMethod $rm) {

/**
* Get list of callbacks that should be called after a job finishes
* Has to be the same for every job
*
* @return callable[]
*/
protected function getJobAfterExecuteCallbacks(): array
protected function getJobAfterExecuteCallbacks(string $methodName): array
{
return [
function (Job $job): void {
function (Job $job) use ($methodName): void {
$job->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";
}
},
Expand Down Expand Up @@ -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),
];
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,6 +162,7 @@ public function testSkipOsFamily(): void
}

#[Test("No assertions")]
#[NoAssertions]
public function testNoAssertions(): void
{
}
Expand Down

0 comments on commit 40b462c

Please sign in to comment.