Skip to content

Commit

Permalink
Backport test.useless from 5.0
Browse files Browse the repository at this point in the history
Only unit tests are supported
Status U is printed in standard output
  • Loading branch information
Naktibalda committed May 23, 2022
1 parent c502768 commit 4490612
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Codeception/Events.php
Expand Up @@ -99,6 +99,14 @@ private function __construct()
*/
const TEST_WARNING = 'test.warning';

/**
* The <b>TEST_USELESS</b> event occurs whenever test does not execute any assertions
* or when it calls expectNotToPerformAssertions and then performs some assertion.
*
* The event listener method receives a {@link \Codeception\Event\FailEvent} instance.
*/
const TEST_USELESS = 'test.useless';


/**
* The event listener method receives a {@link Codeception\Event\TestEvent} instance.
Expand Down
6 changes: 6 additions & 0 deletions src/Codeception/Subscriber/Console.php
Expand Up @@ -42,6 +42,7 @@ class Console implements EventSubscriberInterface
Events::TEST_INCOMPLETE => 'testIncomplete',
Events::TEST_SKIPPED => 'testSkipped',
Events::TEST_WARNING => 'testWarning',
Events::TEST_USELESS => 'testUseless',
Events::TEST_FAIL_PRINT => 'printFail',
Events::RESULT_PRINT_AFTER => 'afterResult',
];
Expand Down Expand Up @@ -292,6 +293,11 @@ public function testIncomplete(FailEvent $e)
$this->writelnFinishedTest($e, $this->message('I')->style('pending'));
}

public function testUseless(FailEvent $event)
{
$this->writelnFinishedTest($event, $this->message('U')->style('pending'));
}

protected function isDetailed($test)
{
return $test instanceof ScenarioDriven && $this->steps;
Expand Down
136 changes: 136 additions & 0 deletions tests/cli/RunUselessTestsCest.php
@@ -0,0 +1,136 @@
<?php

class RunUselessTestsCest
{
public function checkOutput(CliGuy $I)
{
$I->amInPath('tests/data/useless');
$I->executeCommand('run');
// $I->seeInShellOutput('U UselessCept: Make no assertions');
// $I->seeInShellOutput('U UselessCest: Make no assertions');
$I->seeInShellOutput('U UselessTest: Make no assertions');
$I->seeInShellOutput('OK, but incomplete, skipped, or risky tests!');

if (version_compare(\PHPUnit\Runner\Version::id(), '7.2.0', '>=')) {
$I->seeInShellOutput('There were 2 risky tests:');
$I->seeInShellOutput('U UselessTest: Make unexpected assertion');
} else {
$I->seeInShellOutput('There was 1 risky test:');
$I->seeInShellOutput('S UselessTest: Make unexpected assertion');
}

if (DIRECTORY_SEPARATOR === '/') {
// $I->seeInShellOutput(
// '1) UselessCept: Make no assertions
// Test tests/unit/UselessCept.php
//This test did not perform any assertions'
// );
// $I->seeInShellOutput(
// '
//2) UselessCest: Make no assertions
// Test tests/unit/UselessCest.php:makeNoAssertions
//This test did not perform any assertions
//
//Scenario Steps:
//
// 1. // make no assertions'
// );
$I->seeInShellOutput(
'
1) UselessTest: Make no assertions
Test tests/unit/UselessTest.php:testMakeNoAssertions
This test did not perform any assertions'
);
if (version_compare(\PHPUnit\Runner\Version::id(), '7.2.0', '>=')) {
$I->seeInShellOutput(
'
2) UselessTest: Make unexpected assertion
Test tests/unit/UselessTest.php:testMakeUnexpectedAssertion
This test is annotated with "@doesNotPerformAssertions" but performed 1 assertions'
);
}

return;
}

// $I->seeInShellOutput(
// '1) UselessCept: Make no assertions
// Test tests\unit\UselessCept.php
//This test did not perform any assertions'
// );
// $I->seeInShellOutput(
// '
//2) UselessCest: Make no assertions
// Test tests\unit\UselessCest.php:makeNoAssertions
//This test did not perform any assertions
//
//Scenario Steps:
//
// 1. // make no assertions'
// );
$I->seeInShellOutput(
'
1) UselessTest: Make no assertions
Test tests\unit\UselessTest.php:testMakeNoAssertions
This test did not perform any assertions'
);
if (version_compare(\PHPUnit\Runner\Version::id(), '7.2.0', '>=')) {
$I->seeInShellOutput(
'
2) UselessTest: Make unexpected assertion
Test tests\unit\UselessTest.php:testMakeUnexpectedAssertion
This test is annotated with "@doesNotPerformAssertions" but performed 1 assertions'
);
}
}

public function checkReports(CliGuy $I)
{
$I->amInPath('tests/data/useless');
$I->executeCommand('run --report --xml --phpunit-xml --html');

if (version_compare(\PHPUnit\Runner\Version::id(), '7.2.0', '>=')) {
$useless = 2;
$skipped = 0;
$assertions = 1;
$I->seeInShellOutput('UselessTest: Make unexpected assertion.....................................Useless');
} else {
$useless = 1;
$skipped = 1;
$assertions = 0;
$I->seeInShellOutput('UselessTest: Make unexpected assertion.....................................Skipped');
}
$I->seeInShellOutput("Skipped: $skipped. Useless: $useless");

// $I->seeInShellOutput('UselessCept: Make no assertions............................................Useless');
// $I->seeInShellOutput('UselessCest: Make no assertions............................................Useless');
$I->seeInShellOutput('UselessTest: Make no assertions............................................Useless');

$I->seeFileFound('report.xml', 'tests/_output');
$I->seeInThisFile(
"<testsuite name=\"unit\" tests=\"4\" assertions=\"$assertions\" errors=\"$useless\" failures=\"0\" skipped=\"$skipped\" time=\""
);
// $I->seeInThisFile('<testcase name="Useless"');
// $I->seeInThisFile('<testcase name="makeNoAssertions" class="UselessCest"');
$I->seeInThisFile('<testcase name="testMakeNoAssertions" class="UselessTest" file="');
$I->seeInThisFile('<testcase name="testMakeUnexpectedAssertion" class="UselessTest" file="');
$I->seeInThisFile('Risky Test');

$I->seeFileFound('phpunit-report.xml', 'tests/_output');
$I->seeInThisFile(
"<testsuite name=\"unit\" tests=\"4\" assertions=\"$assertions\" errors=\"$useless\" failures=\"0\" skipped=\"$skipped\" time=\""
);
// $I->seeInThisFile('<testcase name="Useless"');
// $I->seeInThisFile('<testcase name="makeNoAssertions" class="UselessCest"');
$I->seeInThisFile('<testcase name="testMakeNoAssertions" class="UselessTest" file="');
$I->seeInThisFile('<testcase name="testMakeUnexpectedAssertion" class="UselessTest" file="');
$I->seeInThisFile('Risky Test');

$I->seeFileFound('report.html', 'tests/_output');
$I->seeInThisFile('<td class="scenarioUseless">Useless scenarios:</td>');
$I->seeInThisFile("<td class=\"scenarioUselessValue\"><strong>$useless</strong></td>");
$I->seeInThisFile('UselessCest');
$I->seeInThisFile('UselessTest');
$I->seeInThisFile('UselessCept');
}
}
11 changes: 11 additions & 0 deletions tests/data/useless/codeception.yml
@@ -0,0 +1,11 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
settings:
log_incomplete_skipped: true
report_useless_tests: true
colors: false
2 changes: 2 additions & 0 deletions tests/data/useless/tests/_output/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
26 changes: 26 additions & 0 deletions tests/data/useless/tests/_support/UnitTester.php
@@ -0,0 +1,26 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;

/**
* Define custom actions here
*/
}
2 changes: 2 additions & 0 deletions tests/data/useless/tests/_support/_generated/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
1 change: 1 addition & 0 deletions tests/data/useless/tests/unit.suite.yml
@@ -0,0 +1 @@
actor: UnitTester
3 changes: 3 additions & 0 deletions tests/data/useless/tests/unit/UselessCept.php
@@ -0,0 +1,3 @@
<?php
$I = new UnitTester($scenario);
$I->wantTo('make no assertions');
9 changes: 9 additions & 0 deletions tests/data/useless/tests/unit/UselessCest.php
@@ -0,0 +1,9 @@
<?php

class UselessCest
{
public function makeNoAssertions(UnitTester $I)
{
$I->comment('make no assertions');
}
}
20 changes: 20 additions & 0 deletions tests/data/useless/tests/unit/UselessTest.php
@@ -0,0 +1,20 @@
<?php

class UselessTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;

public function testMakeNoAssertions()
{

}

public function testMakeUnexpectedAssertion()
{
$this->expectNotToPerformAssertions();
$this->assertTrue(true);
}
}

0 comments on commit 4490612

Please sign in to comment.