-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/reporter ext and splitter for failed tests (#66)
* Create php.yml * Added a FailedTestsReporter which catched all failed Tests and write it to the failedTests.txt * outsourced method which groups the files/tests * FailedTestSplitterTask.php created, Tests for the Task * Updated Readme.md, fixed Returntype in trait
- Loading branch information
Showing
9 changed files
with
383 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codeception\Task\Extension; | ||
|
||
use Codeception\Event\FailEvent; | ||
use Codeception\Event\TestEvent; | ||
use Codeception\Events; | ||
use Codeception\Extension; | ||
use Codeception\Test\Descriptor; | ||
|
||
/** | ||
* Class FailedTestsReporter - reports the failed tests to a reportfile | ||
* Modify the codeception.yml to enable this extension: | ||
* extensions: | ||
* enabled: | ||
* - Codeception\Task\Extension\FailedTestsReporter | ||
*/ | ||
class FailedTestsReporter extends Extension | ||
{ | ||
/** @var string */ | ||
public const REPORT_NAME = 'failedTests.txt'; | ||
|
||
/** @var string $reportFile */ | ||
private $reportFile = self::REPORT_NAME; | ||
|
||
/** @var array $failedTests */ | ||
private $failedTests = []; | ||
|
||
/** | ||
* @var string[] $events | ||
*/ | ||
public static $events = [ | ||
Events::TEST_FAIL => 'afterFail', | ||
Events::RESULT_PRINT_AFTER => 'endRun', | ||
]; | ||
|
||
/** | ||
* Event after each failed test - collect the failed test | ||
* @param FailEvent $event | ||
*/ | ||
public function afterFail(FailEvent $event): void | ||
{ | ||
$this->failedTests[] = $this->getTestname($event); | ||
} | ||
|
||
/** | ||
* Event after all Tests - write failed tests to reportfile | ||
*/ | ||
public function endRun(): void | ||
{ | ||
if (empty($this->failedTests)) { | ||
return; | ||
} | ||
|
||
$file = $this->getLogDir() . $this->reportFile; | ||
if (is_file($file)) { | ||
unlink($file); // remove old reportFile | ||
} | ||
|
||
file_put_contents($file, implode(PHP_EOL, $this->failedTests)); | ||
} | ||
|
||
/** | ||
* @param TestEvent $e | ||
* @return false|string | ||
*/ | ||
public function getTestname(TestEvent $e): string | ||
{ | ||
$name = Descriptor::getTestFullName($e->getTest()); | ||
|
||
return substr(str_replace($this->getRootDir(), '', $name), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codeception\Task\Splitter; | ||
|
||
use Codeception\Configuration; | ||
use Codeception\Task\Extension\FailedTestsReporter; | ||
use RuntimeException; | ||
|
||
class FailedTestSplitterTask extends TestsSplitter | ||
{ | ||
/** @var string */ | ||
private $reportPath = null; | ||
|
||
/** | ||
* @return string | ||
* @throws \Codeception\Exception\ConfigurationException | ||
*/ | ||
public function getReportPath(): string | ||
{ | ||
return $this->reportPath ?? (Configuration::logDir() . FailedTestsReporter::REPORT_NAME); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function run() | ||
{ | ||
$this->claimCodeceptionLoaded(); | ||
$reportPath = $this->getReportPath(); | ||
|
||
if (!@file_exists($reportPath)) { | ||
throw new RuntimeException( | ||
'The reportfile "failedTests.txt" did not exists.' | ||
); | ||
} | ||
|
||
$this->splitToGroupFiles( | ||
$this->filter( | ||
explode( | ||
PHP_EOL, | ||
file_get_contents($reportPath) | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param string $reportPath | ||
* @return FailedTestSplitterTask | ||
*/ | ||
public function setReportPath(string $reportPath): FailedTestSplitterTask | ||
{ | ||
if (empty($reportPath)) { | ||
throw new \InvalidArgumentException('The reportPath could not be empty!'); | ||
} | ||
|
||
$this->reportPath = $reportPath; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Tests\Codeception\Task\Extension; | ||
|
||
use Codeception\Event\FailEvent; | ||
use Codeception\Event\TestEvent; | ||
use Codeception\Task\Extension\FailedTestsReporter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class FailedTestsReporterTest | ||
* @coversDefaultClass \Codeception\Task\Extension\FailedTestsReporter | ||
*/ | ||
class FailedTestsReporterTest extends TestCase | ||
{ | ||
private $failedTests = [ | ||
['testname' => 'tests/acceptance/bar/baz.php:testA',], | ||
['testname' => 'tests/acceptance/bar/baz.php:testB',], | ||
['testname' => 'tests/acceptance/bar/baz.php:testC',], | ||
['testname' => 'tests/acceptance/bar/baz.php:testD',], | ||
['testname' => 'tests/acceptance/bar/baz.php:testE',], | ||
['testname' => 'tests/acceptance/bar/baz.php:testF',], | ||
['testname' => 'tests/acceptance/bar/baz.php:testG',], | ||
['testname' => 'tests/acceptance/bar/baz.php:testH',], | ||
]; | ||
|
||
/** | ||
* @covers ::endRun | ||
*/ | ||
public function testEndRun(): void | ||
{ | ||
$reporter = $this->getMockBuilder(FailedTestsReporter::class) | ||
->disableOriginalConstructor() | ||
->onlyMethods(['getTestname', 'getLogDir']) | ||
->getMock(); | ||
|
||
$reporter->method('getLogDir')->willReturn(TEST_PATH . '/result/'); | ||
|
||
// prepare Mocks for Test | ||
$testEvents = []; | ||
foreach ($this->failedTests as $test) { | ||
$eventMock = $this->getMockBuilder(FailEvent::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$testEvents[] = [ | ||
'mock' => $eventMock, | ||
'testname' => $test['testname'] | ||
]; | ||
} | ||
|
||
// get Testname by the TestEventMock | ||
$reporter | ||
->method('getTestname') | ||
->withConsecutive( | ||
...array_map( | ||
static function (FailEvent $event): array { | ||
return [$event]; | ||
}, | ||
array_column($testEvents, 'mock') | ||
) | ||
) | ||
->willReturnOnConsecutiveCalls(...array_column($testEvents, 'testname')); | ||
|
||
foreach ($testEvents as $event) { | ||
$reporter->afterFail($event['mock']); | ||
} | ||
|
||
$reporter->endRun(); | ||
$file = TEST_PATH . '/result/failedTests.txt'; | ||
$this->assertFileExists($file); | ||
$content = explode(PHP_EOL, file_get_contents($file)); | ||
$this->assertCount(8, $content); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); // TODO: Change the autogenerated stub | ||
unlink(TEST_PATH . '/result/failedTests.txt'); | ||
} | ||
} |
Oops, something went wrong.