Skip to content

Commit

Permalink
Print duration of the tests events in the TeamCity logger
Browse files Browse the repository at this point in the history
  • Loading branch information
wbars authored and sebastianbergmann committed Aug 13, 2018
1 parent c103f2e commit ad254f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/Util/Log/TeamCity.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function addError(Test $test, \Exception $e, $time)
'name' => $test->getName(),
'message' => self::getMessage($e),
'details' => self::getDetails($e),
'duration' => self::toMilliseconds($time),
]
);
}
Expand All @@ -94,7 +95,8 @@ public function addWarning(Test $test, Warning $e, $time)
[
'name' => $test->getName(),
'message' => self::getMessage($e),
'details' => self::getDetails($e)
'details' => self::getDetails($e),
'duration' => self::toMilliseconds($time),
]
);
}
Expand All @@ -112,6 +114,7 @@ public function addFailure(Test $test, AssertionFailedError $e, $time)
'name' => $test->getName(),
'message' => self::getMessage($e),
'details' => self::getDetails($e),
'duration' => self::toMilliseconds($time),
];

if ($e instanceof ExpectationFailedException) {
Expand Down Expand Up @@ -150,7 +153,7 @@ public function addFailure(Test $test, AssertionFailedError $e, $time)
*/
public function addIncompleteTest(Test $test, \Exception $e, $time)
{
$this->printIgnoredTest($test->getName(), $e);
$this->printIgnoredTest($test->getName(), $e, $time);
}

/**
Expand All @@ -177,21 +180,22 @@ public function addSkippedTest(Test $test, \Exception $e, $time)
$testName = $test->getName();
if ($this->startedTestName != $testName) {
$this->startTest($test);
$this->printIgnoredTest($testName, $e);
$this->printIgnoredTest($testName, $e, $time);
$this->endTest($test, $time);
} else {
$this->printIgnoredTest($testName, $e);
$this->printIgnoredTest($testName, $e, $time);
}
}

public function printIgnoredTest($testName, \Exception $e)
public function printIgnoredTest($testName, \Exception $e, $time)
{
$this->printEvent(
'testIgnored',
[
'name' => $testName,
'message' => self::getMessage($e),
'details' => self::getDetails($e),
'duration' => self::toMilliseconds($time),
]
);
}
Expand Down Expand Up @@ -302,7 +306,7 @@ public function endTest(Test $test, $time)
'testFinished',
[
'name' => $test->getName(),
'duration' => (int) (\round($time, 2) * 1000)
'duration' => self::toMilliseconds($time),
]
);
}
Expand Down Expand Up @@ -420,4 +424,13 @@ private static function getFileName($className)

return $reflectionClass->getFileName();
}

/**
* @param float $time microseconds
* @return int
*/
private static function toMilliseconds($time)
{
return \round($time * 1000);
}
}
4 changes: 2 additions & 2 deletions tests/TextUI/teamcity-inner-exceptions.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ PHPUnit %s by Sebastian Bergmann and contributors.

##teamcity[testStarted name='testPrintingChildException' locationHint='php_qn://%s%etests%e_files%eExceptionStackTest.php::\ExceptionStackTest::testPrintingChildException' flowId='%d']

##teamcity[testFailed name='testPrintingChildException' message='Child exception|nmessage|nFailed asserting that two arrays are equal.|n--- Expected|n+++ Actual|n@@ @@|n Array (|n- 0 => 1|n+ 0 => 2|n' details=' %s%eExceptionStackTest.php:14|n |n Caused by|n message|n Failed asserting that two arrays are equal.|n --- Expected|n +++ Actual|n @@ @@|n Array (|n - 0 => 1|n + 0 => 2|n |n %s%eExceptionStackTest.php:10|n ' flowId='%d']
##teamcity[testFailed name='testPrintingChildException' message='Child exception|nmessage|nFailed asserting that two arrays are equal.|n--- Expected|n+++ Actual|n@@ @@|n Array (|n- 0 => 1|n+ 0 => 2|n' details=' %s%eExceptionStackTest.php:14|n |n Caused by|n message|n Failed asserting that two arrays are equal.|n --- Expected|n +++ Actual|n @@ @@|n Array (|n - 0 => 1|n + 0 => 2|n |n %s%eExceptionStackTest.php:10|n ' duration='%d' flowId='%d']

##teamcity[testFinished name='testPrintingChildException' duration='%d' flowId='%d']

##teamcity[testStarted name='testNestedExceptions' locationHint='php_qn://%s%etests%e_files%eExceptionStackTest.php::\ExceptionStackTest::testNestedExceptions' flowId='%d']

##teamcity[testFailed name='testNestedExceptions' message='Exception : One' details=' %s%etests%e_files%eExceptionStackTest.php:22|n |n Caused by|n InvalidArgumentException: Two|n |n %s%etests%e_files%eExceptionStackTest.php:21|n |n Caused by|n Exception: Three|n |n %s%etests%e_files%eExceptionStackTest.php:20|n ' flowId='%d']
##teamcity[testFailed name='testNestedExceptions' message='Exception : One' details=' %s%etests%e_files%eExceptionStackTest.php:22|n |n Caused by|n InvalidArgumentException: Two|n |n %s%etests%e_files%eExceptionStackTest.php:21|n |n Caused by|n Exception: Three|n |n %s%etests%e_files%eExceptionStackTest.php:20|n ' duration='%d' flowId='%d']

##teamcity[testFinished name='testNestedExceptions' duration='%d' flowId='%d']

Expand Down

0 comments on commit ad254f9

Please sign in to comment.