Skip to content

Instantly share code, notes, and snippets.

@thiagooak
Created January 9, 2015 13:14
Show Gist options
  • Save thiagooak/95074254b430f88067d7 to your computer and use it in GitHub Desktop.
Save thiagooak/95074254b430f88067d7 to your computer and use it in GitHub Desktop.
phpunit slow tests
<phpunit>
<listeners>
<listener class="TestTimesListener" file="TestTimesListener.php" />
</listeners>
</phpunit>
<?php
/**
* source: https://jameshd.wordpress.com/2014/11/12/slow-phpunit-tests/
*/
class TestTimesListener implements PHPUnit_Framework_TestListener
{
/**
* Anything above this threshold is deemed slow
* @var float $seconds
*/
const MAX_EXECUTION_TIME = 0.2;
/**
* Absolute max time we should allow a unit test to run.
* @var float $seconds
*/
const FAIL_IF_LONGER_THAN = 20.0;
/**
* Helper variables for assinting with naming
* @var string
*/
protected $currentSuite;
protected $fullTestName;
public function startTest(PHPUnit_Framework_Test $test)
{
$this->fullTestName = $this->currentSuite->getName() . "::" . $test->getName();
}
public function endTest(PHPUnit_Framework_Test $test, $time)
{
if ($time > self::MAX_EXECUTION_TIME) {
printf(
PHP_EOL . "%s took %f seconds" . PHP_EOL,
$this->fullTestName,
$time
);
if ($time >= self::FAIL_IF_LONGER_THAN) {
$test->fail(
sprintf(
"%s is too slow, it takes %f seconds",
$this->fullTestName,
$time
)
);
}
}
}
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
}
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
$this->currentSuite = $suite;
}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment