Skip to content

Commit

Permalink
Add test execution time and error data (if any) as a test execution c…
Browse files Browse the repository at this point in the history
…omment
  • Loading branch information
lpopov committed May 16, 2019
1 parent 0a6d30b commit 92b9c29
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
42 changes: 34 additions & 8 deletions src/Api/JsonRpc/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public function createBuild(): Build
return $this->buildRepository->create($build);
}

public function addTestResult($name, $result, $containingClass, $exception = "")
public function addTestResult($name, $result, $containingClass, $text = "", $executionTime = null)
{
$summary = $containingClass . '::' . $name;
$testResultId = md5($containingClass . '::' . $name);
Expand All @@ -311,7 +311,9 @@ public function addTestResult($name, $result, $containingClass, $exception = "")

$this->testResults[$testResultId] = [
'testCase' => $testCase,
'testExecutionStatus' => $this->getCaseRunStatusIdByResult($result)
'testExecutionStatusId' => $this->getCaseRunStatusIdByResult($result),
'testExecutionTime' => $executionTime,
'testExecutionText' => $text,
];
}
}
Expand All @@ -331,7 +333,7 @@ public function createTestExecution(int $testCaseId, int $statusId): TestExecuti
);
}

private function updateOrCreateTestExecution(TestCase $existingTestCase, int $statusId)
private function updateOrCreateTestExecution(TestCase $existingTestCase, int $statusId): TestExecution
{
$testExecution = $this->testExecutionRepository->findByTestCaseIdAndTestRunId(
$existingTestCase->getCaseId(),
Expand All @@ -343,15 +345,29 @@ private function updateOrCreateTestExecution(TestCase $existingTestCase, int $st
} else {
$testExecution = $this->createTestExecution($existingTestCase->getCaseId(), $statusId);
}

return $testExecution;
}

private function createTestCaseAndTestExecution(TestCase $newTestCase, int $statusId)
private function createTestCaseAndTestExecution(TestCase $newTestCase, int $statusId): TestExecution
{
$testCase = $this->testCaseRepository->createModel($newTestCase);

$this->testPlanRepository->addTestCase($this->testRun->getPlanId(), $testCase->getCaseId());

$this->createTestExecution($testCase->getCaseId(), $statusId);
$testExecution = $this->createTestExecution($testCase->getCaseId(), $statusId);

return $testExecution;
}

private function createTextExecutionComment(
TestExecution $testExecution,
string $executionText,
float $executionTime
) {
$executionTimeStr = sprintf("Execution time: %fs\n\n", $executionTime);
$executionText = $executionTimeStr . $executionText;
$this->testExecutionRepository->addComment($testExecution->getCaseRunId(), $executionText);
}

private function addTestResultsToTestRun()
Expand All @@ -364,14 +380,24 @@ private function addTestResultsToTestRun()
$existingTestCases = $this->testCaseRepository->findByTestRunId($this->testRun->getRunId());

foreach ($this->testResults as $testResultId => $testResult) {
$testExecution = null;
if (isset($existingTestCases[$testResultId])) {
$this->updateOrCreateTestExecution(
$testExecution = $this->updateOrCreateTestExecution(
$existingTestCases[$testResultId],
$testResult['testExecutionStatus']
$testResult['testExecutionStatusId']
);
} else {
$this->createTestCaseAndTestExecution($testResult['testCase'], $testResult['testExecutionStatus']);
$testExecution = $this->createTestCaseAndTestExecution(
$testResult['testCase'],
$testResult['testExecutionStatusId']
);
}

$this->createTextExecutionComment(
$testExecution,
$testResult['testExecutionText'],
$testResult['testExecutionTime']
);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Api/JsonRpc/Repository/TestExecutionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ public function create(TestExecution $model): TestExecution

return $newModel;
}

public function addComment(int $testExecutionId, string $comment)
{
/** @var Response $response */
$this->client->send($this->client->request(123, 'TestExecution.add_comment', [
'case_run_id' => $testExecutionId,
'comment' => $comment
]));
}
}
29 changes: 22 additions & 7 deletions src/PHPUnit/PHPUnitTestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class PHPUnitTestListener implements TestListener
{
protected $kiwiTcmsClient;
private $testStartTime;

public function __construct()
{
Expand Down Expand Up @@ -49,7 +50,8 @@ public function addError(Test $test, \Throwable $t, float $time): void
$test->getName(),
'ERROR',
get_class($test),
"Error: " . $t->getMessage() . "\n\n" . $t->getFile() . ':' . $t->getLine()
"Error: " . $t->getMessage() . "\n\n" . $t->getFile() . ':' . $t->getLine(),
$this->getExecutionTime()
);
}

Expand All @@ -59,7 +61,8 @@ public function addWarning(Test $test, Warning $e, float $time): void
$test->getName(),
'WARNING',
get_class($test),
"Warning: " . $e->getMessage() . "\n\n" . $e->getFile() . ':' . $e->getLine()
"Warning: " . $e->getMessage() . "\n\n" . $e->getFile() . ':' . $e->getLine(),
$this->getExecutionTime()
);
}

Expand All @@ -69,7 +72,8 @@ public function addFailure(Test $test, AssertionFailedError $e, float $time): vo
$test->getName(),
'FAIL',
get_class($test),
"Failure: " . $e->getMessage() . "\n\n" . $e->getFile() . ':' . $e->getLine()
"Failure: " . $e->getMessage() . "\n\n" . $e->getFile() . ':' . $e->getLine(),
$this->getExecutionTime()
);
}

Expand All @@ -79,7 +83,8 @@ public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
$test->getName(),
'INCOMPLETE',
get_class($test),
"Incomplete: " . $t->getMessage()
"Incomplete: " . $t->getMessage(),
$this->getExecutionTime()
);
}

Expand All @@ -89,7 +94,8 @@ public function addRiskyTest(Test $test, \Throwable $t, float $time): void
$test->getName(),
'RISKY',
get_class($test),
"Risky: " . $t->getMessage()
"Risky: " . $t->getMessage(),
$this->getExecutionTime()
);
}

Expand All @@ -99,20 +105,24 @@ public function addSkippedTest(Test $test, \Throwable $t, float $time): void
$test->getName(),
'SKIPPED',
get_class($test),
"Skipped: " . $t->getMessage()
"Skipped: " . $t->getMessage(),
$this->getExecutionTime()
);
}

public function startTest(Test $test): void
{
$this->testStartTime = microtime(true);
}

public function endTest(Test $test, float $time): void
{
$this->kiwiTcmsClient->addTestResult(
$test->getName(),
'PASS',
get_class($test)
get_class($test),
"",
$this->getExecutionTime()
);
}

Expand All @@ -123,4 +133,9 @@ public function startTestSuite(TestSuite $suite): void
public function endTestSuite(TestSuite $suite): void
{
}

private function getExecutionTime()
{
return microtime(true) - $this->testStartTime;
}
}

0 comments on commit 92b9c29

Please sign in to comment.