RunFailed.php 2.4 KB

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