RunFailed.php
1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Codeception\Extension;
use Codeception\Event\PrintResultEvent;
use Codeception\Events;
use Codeception\Extension;
use Codeception\Test\Descriptor;
/**
* Saves failed tests into tests/log/failed in order to rerun failed tests.
*
* To rerun failed tests just run the `failed` group:
*
* ```
* php codecept run -g failed
* ```
*
* Starting from Codeception 2.1 **this extension is enabled by default**.
*
* ``` yaml
* extensions:
* enabled: [Codeception\Extension\RunFailed]
* ```
*
* On each execution failed tests are logged and saved into `tests/_output/failed` file.
*/
class RunFailed extends Extension
{
public static $events = [
Events::RESULT_PRINT_AFTER => 'saveFailed'
];
protected $config = ['file' => 'failed'];
public function _initialize()
{
$logPath = str_replace($this->getRootDir(), '', $this->getLogDir()); // get local path to logs
$this->_reconfigure(['groups' => ['failed' => $logPath . $this->config['file']]]);
}
public function saveFailed(PrintResultEvent $e)
{
$file = $this->getLogDir() . $this->config['file'];
$result = $e->getResult();
if ($result->wasSuccessful()) {
if (is_file($file)) {
unlink($file);
}
return;
}
$output = [];
foreach ($result->failures() as $fail) {
$output[] = $this->localizePath(Descriptor::getTestFullName($fail->failedTest()));
}
foreach ($result->errors() as $fail) {
$output[] = $this->localizePath(Descriptor::getTestFullName($fail->failedTest()));
}
file_put_contents($file, implode("\n", $output));
}
protected function localizePath($path)
{
$root = realpath($this->getRootDir()) . DIRECTORY_SEPARATOR;
if (substr($path, 0, strlen($root)) == $root) {
return substr($path, strlen($root));
}
return $path;
}
}