-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Like Junit, but generates testsuite for each test file
- Loading branch information
1 parent
c4272e2
commit ebc9c63
Showing
3 changed files
with
130 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
language: php | ||
|
||
env: | ||
CODECEPTION_VERSION: '2.4.x-dev' | ||
CODECEPTION_VERSION: 'dev-phpunit-xml-reports' | ||
|
||
php: | ||
- 7.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,116 @@ | ||
<?php | ||
namespace Codeception\PHPUnit\Log; | ||
|
||
use Codeception\Configuration; | ||
use Codeception\Test\Interfaces\Reported; | ||
use Codeception\Test\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\TestSuite; | ||
|
||
class PhpUnit extends \PHPUnit\Util\Log\JUnit | ||
{ | ||
const SUITE_LEVEL = 1; | ||
const FILE_LEVEL = 2; | ||
|
||
protected $strictAttributes = ['file', 'name', 'class']; | ||
|
||
private $currentFile; | ||
private $currentFileSuite; | ||
|
||
public function startTest(\PHPUnit\Framework\Test $test):void | ||
{ | ||
if (method_exists($test, 'getFileName') ) { | ||
$filename = $test->getFileName(); | ||
} else { | ||
$reflector = new \ReflectionClass($test); | ||
$filename = $reflector->getFileName(); | ||
} | ||
|
||
if ($filename !== $this->currentFile) { | ||
if ($this->currentFile !== null) { | ||
parent::endTestSuite(new TestSuite()); | ||
} | ||
|
||
//initialize all values to avoid warnings | ||
$this->testSuiteAssertions[self::FILE_LEVEL] = 0; | ||
$this->testSuiteTests[self::FILE_LEVEL] = 0; | ||
$this->testSuiteTimes[self::FILE_LEVEL] = 0; | ||
$this->testSuiteErrors[self::FILE_LEVEL] = 0; | ||
$this->testSuiteFailures[self::FILE_LEVEL] = 0; | ||
$this->testSuiteSkipped[self::FILE_LEVEL] = 0; | ||
|
||
$this->testSuiteLevel = self::FILE_LEVEL; | ||
|
||
$this->currentFile = $filename; | ||
$this->currentFileSuite = $this->document->createElement('testsuite'); | ||
$this->currentFileSuite->setAttribute('file', $filename); | ||
|
||
$this->testSuites[self::SUITE_LEVEL]->appendChild($this->currentFileSuite); | ||
$this->testSuites[self::FILE_LEVEL] = $this->currentFileSuite; | ||
} | ||
|
||
if (!$test instanceof Reported) { | ||
parent::startTest($test); | ||
return; | ||
} | ||
|
||
$this->currentTestCase = $this->document->createElement('testcase'); | ||
|
||
$isStrict = Configuration::config()['settings']['strict_xml']; | ||
|
||
foreach ($test->getReportFields() as $attr => $value) { | ||
if ($isStrict and !in_array($attr, $this->strictAttributes)) { | ||
continue; | ||
} | ||
$this->currentTestCase->setAttribute($attr, $value); | ||
} | ||
} | ||
|
||
public function endTest(\PHPUnit\Framework\Test $test, float $time):void | ||
{ | ||
if ($this->currentTestCase !== null && $test instanceof Test) { | ||
$numAssertions = $test->getNumAssertions(); | ||
$this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions; | ||
|
||
$this->currentTestCase->setAttribute( | ||
'assertions', | ||
$numAssertions | ||
); | ||
} | ||
|
||
if ($test instanceof TestCase) { | ||
parent::endTest($test, $time); | ||
return; | ||
} | ||
|
||
// In PhpUnit 7.4.*, parent::endTest ignores tests that aren't instances of TestCase | ||
// so I copied this code from PhpUnit 7.3.5 | ||
|
||
$this->currentTestCase->setAttribute( | ||
'time', | ||
\sprintf('%F', $time) | ||
); | ||
$this->testSuites[$this->testSuiteLevel]->appendChild( | ||
$this->currentTestCase | ||
); | ||
$this->testSuiteTests[$this->testSuiteLevel]++; | ||
$this->testSuiteTimes[$this->testSuiteLevel] += $time; | ||
$this->currentTestCase = null; | ||
} | ||
|
||
/** | ||
* Cleans the mess caused by test suite manipulation in startTest | ||
*/ | ||
public function endTestSuite(TestSuite $suite): void | ||
{ | ||
if ($suite->getName()) { | ||
if ($this->currentFile) { | ||
//close last file in the test suite | ||
parent::endTestSuite(new TestSuite()); | ||
$this->currentFile = null; | ||
} | ||
$this->testSuiteLevel = self::SUITE_LEVEL; | ||
} | ||
parent::endTestSuite($suite); | ||
} | ||
} |
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