abstract class ArcanistPhutilTestCase {
private $runningTest;
+ private $testStartTime;
private $results = array();
$result = new ArcanistUnitTestResult();
$result->setName($this->runningTest);
$result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
+ $result->setDuration(microtime(true) - $this->testStartTime);
$result->setUserData($reason);
$this->results[] = $result;
}
$result = new ArcanistUnitTestResult();
$result->setName($this->runningTest);
$result->setResult(ArcanistUnitTestResult::RESULT_PASS);
+ $result->setDuration(microtime(true) - $this->testStartTime);
$result->setUserData($reason);
$this->results[] = $result;
}
$name = $method->getName();
if (preg_match('/^test/', $name)) {
$this->runningTest = $name;
+ $this->testStartTime = microtime(true);
try {
$this->willRunOneTest($name);
private $namespace;
private $name;
private $result;
+ private $duration;
private $userData;
public function setName($name) {
return $this->result;
}
+ public function setDuration($duration) {
+ $this->duration = $duration;
+ return $this;
+ }
+
+ public function getDuration() {
+ return $this->duration;
+ }
+
public function setUserData($user_data) {
$this->userData = $user_data;
return $this;
$unresolved[] = $result;
} else {
if ($this->engine->shouldEchoTestResults()) {
- echo ' '.$status_codes[$result_code].' '.$result->getName()."\n";
+ echo ' '.$status_codes[$result_code];
+ if ($result_code == ArcanistUnitTestResult::RESULT_PASS) {
+ echo ' '.self::formatTestDuration($result->getDuration());
+ }
+ echo ' '.$result->getName()."\n";
}
if ($result_code != ArcanistUnitTestResult::RESULT_PASS) {
if ($this->engine->shouldEchoTestResults()) {
}
}
+ private static function formatTestDuration($seconds) {
+ // Very carefully define inclusive upper bounds on acceptable unit test
+ // durations. Times are in milliseconds and are in increasing order.
+ $acceptableness = array(
+ 50 => "<fg:green>%s</fg><fg:yellow>\xE2\x98\x85</fg> ",
+ 200 => '<fg:green>%s</fg> ',
+ 500 => '<fg:yellow>%s</fg> ',
+ INF => '<fg:red>%s</fg> ',
+ );
+
+ $milliseconds = $seconds * 1000;
+ $duration = self::formatTime($seconds);
+ foreach ($acceptableness as $upper_bound => $formatting) {
+ if ($milliseconds <= $upper_bound) {
+ return phutil_console_format($formatting, $duration);
+ }
+ }
+ return phutil_console_format(end($acceptableness), $duration);
+ }
+
+ private static function formatTime($seconds) {
+ if ($seconds >= 60) {
+ $minutes = floor($seconds / 60);
+ return sprintf('%dm%02ds', $minutes, round($seconds % 60));
+ }
+
+ if ($seconds >= 1) {
+ return sprintf('%4.1fs', $seconds);
+ }
+
+ $milliseconds = $seconds * 1000;
+ if ($milliseconds >= 1) {
+ return sprintf('%3dms', round($milliseconds));
+ }
+
+ return ' <1ms';
+ }
}