/** * Create the test result and splice on our code coverage reports. * * @return PHPUnit_Framework_TestResult */ protected function createTestResult() { $result = new PHPUnit_Framework_TestResult; if (isset($this->_params['codeCoverage'])) { $result->collectCodeCoverageInformation(true); } return $result; }
/** * Runs the test suite using the result specified in the constructor * * @param array $groups Optional array of groups to test * @param bool $collect_cc Optional, Should code coverage be collected? * @return Kohana_PHPUnit Instance of $this */ public function run(array $groups = array(), $collect_cc = FALSE) { if ($collect_cc and !extension_loaded('xdebug')) { throw new Kohana_Exception('Code coverage cannot be collected because the xdebug extension is not loaded'); } $this->result->collectCodeCoverageInformation((bool) $collect_cc); // Run the tests. $this->suite->run($this->result, FALSE, $groups); return $this; }
/** * Main function - runs the tests and outputs HTML code * * @return void * @author Robert Lemke <*****@*****.**> * @author Karsten Dambekalns <*****@*****.**> * @internal Preliminary solution - there surely will be nicer ways to implement a test runner */ public function run() { $this->renderPageHeader(); $this->renderTestForm(); if (!empty($this->packageKey)) { $testcaseFileNamesAndPaths = $this->getTestcaseFilenames(); if (count($testcaseFileNamesAndPaths) > 0) { $this->renderInfoAndProgressbar(); $this->requireTestCaseFiles($testcaseFileNamesAndPaths); $testListener = new \F3\Testing\TestListener(); $testListener->baseUri = $this->request->getBaseUri(); $testResult = new \PHPUnit_Framework_TestResult(); $testResult->addListener($testListener); $testResult->collectCodeCoverageInformation($this->collectCodeCoverage); $startTime = microtime(TRUE); foreach (get_declared_classes() as $className) { if (substr($className, -4, 4) == 'Test') { $class = new \ReflectionClass($className); if ($class->isSubclassOf('PHPUnit_Framework_TestCase') && substr($className, 0, 8) !== 'PHPUnit_') { $testSuite = new \PHPUnit_Framework_TestSuite($class); $testSuite->run($testResult); } } } $endTime = microtime(TRUE); // Display test statistics: if ($testResult->wasSuccessful()) { echo '<script type="text/javascript">document.getElementById("progress-bar").style.backgroundColor = "green";document.getElementById("progress-bar").style.backgroundImage = "none";</script> <h1 class="success">SUCCESS</h1> ' . $testResult->count() . ' tests, ' . $testResult->failureCount() . ' failures, ' . $testResult->errorCount() . ' errors. </h1>'; } else { echo ' <script>document.getElementById("progress-bar").style.backgroundColor = "red";document.getElementById("progress-bar").style.backgroundImage = "none";</script> <h1 class="failure">FAILURE</h1> ' . $testResult->count() . ' tests, ' . $testResult->failureCount() . ' failures, ' . $testResult->errorCount() . ' errors. '; } echo '<p>Peak memory usage was: ~' . floor(memory_get_peak_usage() / 1024 / 1024) . ' MByte.<br />'; echo 'Test run took ' . round($endTime - $startTime, 4) . ' seconds.</p>'; if ($this->collectCodeCoverage === TRUE) { \F3\FLOW3\Utility\Files::emptyDirectoryRecursively($this->coverageOutputPath); \PHPUnit_Util_Report::render($testResult, $this->coverageOutputPath); echo '<a href="_Resources/CodeCoverageReport/index.html">See code coverage report...</a>'; } } else { echo '<p>No testcase found. Did you specify the intended pattern?</p>'; } } $this->renderPageFooter(); }
/** * Run a test */ public function run(PHPUnit_Framework_TestSuite $suite) { $res = new PHPUnit_Framework_TestResult(); if ($this->codecoverage) { $res->collectCodeCoverageInformation(TRUE); } $res->addListener($this); foreach ($this->formatters as $formatter) { $res->addListener($formatter); } /* Set PHPUnit error handler */ if ($this->useCustomErrorHandler) { $oldErrorHandler = set_error_handler(array('PHPUnitTestRunner', 'handleError'), E_ALL | E_STRICT); } $suite->run($res, false, $this->groups, $this->excludeGroups); foreach ($this->formatters as $formatter) { $formatter->processResult($res); } /* Restore Phing error handler */ if ($this->useCustomErrorHandler) { restore_error_handler(); } if ($this->codecoverage) { $coverageInformation = $res->getCodeCoverageInformation(); PHPUnit_Util_CodeCoverage::clearSummary(); $summary = PHPUnit_Util_CodeCoverage::getSummary($coverageInformation); CoverageMerger::merge($this->project, $summary); } if ($res->errorCount() != 0) { $this->retCode = self::ERRORS; } else { if ($res->failureCount() != 0) { $this->retCode = self::FAILURES; } else { if ($res->notImplementedCount() != 0) { $this->retCode = self::INCOMPLETES; } else { if ($res->skippedCount() != 0) { $this->retCode = self::SKIPPED; } } } } }
/** * Run a test */ function run($test) { $res = new PHPUnit_Framework_TestResult(); if ($this->codecoverage) { $res->collectCodeCoverageInformation(TRUE); } $res->addListener($this); foreach ($this->formatters as $formatter) { $res->addListener($formatter); } /* Set PHPUnit error handler */ $oldErrorHandler = set_error_handler(array('PHPUnitTestRunner', 'handleError'), E_ALL | E_STRICT); $test->run($res, false, $this->groups, $this->excludeGroups); /* Restore Phing error handler */ restore_error_handler(); if ($this->codecoverage) { $coverageInformation = $res->getCodeCoverageInformation(); foreach ($coverageInformation as $coverage_info) { CoverageMerger::merge($this->project, array($coverage_info['files'])); } } if ($res->errorCount() != 0) { $this->retCode = self::ERRORS; } else { if ($res->failureCount() != 0) { $this->retCode = self::FAILURES; } else { if ($res->notImplementedCount() != 0) { $this->retCode = self::INCOMPLETES; } else { if ($res->skippedCount() != 0) { $this->retCode = self::SKIPPED; } } } } }
/** * Initializes and runs the tests * * @param string $packageKey Package to test * @param string $testcaseClassName Testcase to run (all if not given) * @param string $outputPath Path to put the output XML files to * @return void * @author Sebastian Kurfürst <*****@*****.**> * @author Karsten Dambekalns <*****@*****.**> * @internal Preliminary solution - there surely will be nicer ways to implement a test runner */ public function run() { $testResult = new \PHPUnit_Framework_TestResult(); $testResult->addListener(new \PHPUnit_Util_Log_JUnit($this->testOutputPath . '/logfile.xml')); $testResult->collectCodeCoverageInformation($this->collectCodeCoverage); $testcaseFileNamesAndPaths = $this->getTestcaseFilenames(); if (count($testcaseFileNamesAndPaths) > 0) { $this->requireTestCaseFiles($testcaseFileNamesAndPaths); $startTime = microtime(TRUE); foreach (get_declared_classes() as $className) { if (substr($className, -4, 4) == 'Test') { $class = new \ReflectionClass($className); if ($class->isSubclassOf('PHPUnit_Framework_TestCase') && substr($className, 0, 8) !== 'PHPUnit_') { $testSuite = new \PHPUnit_Framework_TestSuite($class); $testSuite->run($testResult); } } } $endTime = microtime(TRUE); $testResult->flushListeners(); // Display test statistics: if ($testResult->wasSuccessful()) { echo 'SUCCESS' . PHP_EOL . $testResult->count() . ' tests, ' . $testResult->failureCount() . ' failures, ' . $testResult->errorCount() . ' errors.' . PHP_EOL; } else { echo 'FAILURE' . PHP_EOL . $testResult->count() . ' tests, ' . $testResult->failureCount() . ' failures, ' . $testResult->errorCount() . ' errors.' . PHP_EOL; } echo 'Peak memory usage was: ~' . floor(memory_get_peak_usage() / 1024 / 1024) . ' MByte.' . PHP_EOL; echo 'Test run took ' . round($endTime - $startTime, 4) . ' seconds.' . PHP_EOL; if ($this->collectCodeCoverage === TRUE) { $report = new \PHPUnit_Util_Log_CodeCoverage_XML_Clover($this->coverageOutputPath . '/clover.xml'); $report->process($testResult); } } else { echo 'No testcase found. Did you specify the intended pattern?' . PHP_EOL; } }
/** * @param array $classList * @param boolean $coverage */ function runTests($classList, $coverage = false) { $startTime = microtime(true); // XDEBUG seem to cause problems with test execution :-( if (function_exists('xdebug_disable')) { xdebug_disable(); } ini_set('max_execution_time', 0); $this->setUp(); // Optionally skip certain tests $skipTests = array(); if ($this->request->getVar('SkipTests')) { $skipTests = explode(',', $this->request->getVar('SkipTests')); } $classList = array_diff($classList, $skipTests); // run tests before outputting anything to the client $suite = new PHPUnit_Framework_TestSuite(); natcasesort($classList); foreach ($classList as $className) { // Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this. class_exists($className); $suite->addTest(new SapphireTestSuite($className)); } // Remove the error handler so that PHPUnit can add its own restore_error_handler(); /*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/ if (Director::is_cli()) { $reporter = new CliTestReporter(); } else { $reporter = new SapphireTestReporter(); } self::$default_reporter->writeHeader("Sapphire Test Runner"); if (count($classList) > 1) { self::$default_reporter->writeInfo("All Tests", "Running test cases: ", implode(", ", $classList)); } else { self::$default_reporter->writeInfo($classList[0], ""); } $results = new PHPUnit_Framework_TestResult(); $results->addListener($reporter); if ($coverage === true) { foreach (self::$coverage_filter_dirs as $dir) { PHPUnit_Util_Filter::addDirectoryToFilter(BASE_PATH . '/' . $dir); } $results->collectCodeCoverageInformation(true); $suite->run($results); if (!file_exists(ASSETS_PATH . '/coverage-report')) { mkdir(ASSETS_PATH . '/coverage-report'); } PHPUnit_Util_Report::render($results, ASSETS_PATH . '/coverage-report/'); $coverageApp = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/', '_', preg_replace('/(\\/$)|(^\\/)/', '', Director::baseFolder())) . '.html'; $coverageTemplates = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/', '_', preg_replace('/(\\/$)|(^\\/)/', '', realpath(TEMP_FOLDER))) . '.html'; echo "<p>Coverage reports available here:<ul>\n\t\t\t\t<li><a href=\"{$coverageApp}\">Coverage report of the application</a></li>\n\t\t\t\t<li><a href=\"{$coverageTemplates}\">Coverage report of the templates</a></li>\n\t\t\t</ul>"; } else { $suite->run($results); } if (!Director::is_cli()) { echo '<div class="trace">'; } $reporter->writeResults(); $endTime = microtime(true); if (Director::is_cli()) { echo "\n\nTotal time: " . round($endTime - $startTime, 3) . " seconds\n"; } else { echo "<p>Total time: " . round($endTime - $startTime, 3) . " seconds</p>\n"; } if (!Director::is_cli()) { echo '</div>'; } // Put the error handlers back Debug::loadErrorHandlers(); if (!Director::is_cli()) { self::$default_reporter->writeFooter(); } $this->tearDown(); // Todo: we should figure out how to pass this data back through Director more cleanly if (Director::is_cli() && $results->failureCount() + $results->errorCount() > 0) { exit(2); } }
function runTests($classList, $coverage = false) { // XDEBUG seem to cause problems with test execution :-( if(function_exists('xdebug_disable')) xdebug_disable(); ini_set('max_execution_time', 0); $this->setUp(); // run tests before outputting anything to the client $suite = new PHPUnit_Framework_TestSuite(); foreach($classList as $className) { // Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this. class_exists($className); $suite->addTest(new PHPUnit_Framework_TestSuite($className)); } // Remove the error handler so that PHPUnit can add its own restore_error_handler(); /*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/ if(Director::is_cli()) $reporter = new CliTestReporter(); else $reporter = new SapphireTestReporter(); self::$default_reporter->writeHeader("Sapphire Test Runner"); if (count($classList) > 1) { self::$default_reporter->writeInfo("All Tests", "Running test cases: " . implode(", ", $classList)); } else { self::$default_reporter->writeInfo($classList[0], ""); } $results = new PHPUnit_Framework_TestResult(); $results->addListener($reporter); if($coverage) { $results->collectCodeCoverageInformation(true); $suite->run($results); if(!file_exists(ASSETS_PATH . '/coverage-report')) mkdir(ASSETS_PATH . '/coverage-report'); PHPUnit_Util_Report::render($results, ASSETS_PATH . '/coverage-report/'); $coverageApp = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/','_',preg_replace('/(\/$)|(^\/)/','',Director::baseFolder())) . '.html'; $coverageTemplates = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/','_',preg_replace('/(\/$)|(^\/)/','',realpath(TEMP_FOLDER))) . '.html'; echo "<p>Coverage reports available here:<ul> <li><a href=\"$coverageApp\">Coverage report of the application</a></li> <li><a href=\"$coverageTemplates\">Coverage report of the templates</a></li> </ul>"; } else { $suite->run($results); } if(!Director::is_cli()) echo '<div class="trace">'; $reporter->writeResults(); if(!Director::is_cli()) echo '</div>'; // Put the error handlers back Debug::loadErrorHandlers(); if(!Director::is_cli()) self::$default_reporter->writeFooter(); $this->tearDown(); // Todo: we should figure out how to pass this data back through Director more cleanly if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2); }
/** * Run a test suite and return the results. * * @param Streamwide_PHPUnit_Runner_TestCase $testCase test case * @param boolean $codeCoverage (optional) whether to collect code coverage * @return PHPUnit_Framework_TestResult phunit test result */ private function _runTestCase($testCase, $codeCoverage = false) { require_once $testCase->getFile(); $result = new PHPUnit_Framework_TestResult(); $result->collectCodeCoverageInformation($codeCoverage); $class = $testCase->getClass(); $testCase = new $class($testCase->getTest()); $testCase->setInIsolation(true); // run the test case $testCase->run($result); return $result; }
/** * Runs the main testSuite and attaches to it a reporter * * @param PHPUnit_Framework_TestListener $reporter Reporter instance to use with the group test being run. * @return PHPUnit_Framework_TestResult Result object of the test run. */ protected function run($reporter, $codeCoverage = false) { restore_error_handler(); restore_error_handler(); $result = new PHPUnit_Framework_TestResult(); $result->collectCodeCoverageInformation($codeCoverage); $result->addListener($reporter); $reporter->paintHeader(); $testSuite = $this->getTestSuite(); $testSuite->setFixtureManager($this->getFixtureManager()); $testSuite->run($result, $this->filter); $reporter->paintResult($result); return $result; }
function runTests($classList, $coverage = false) { global $TESTING_CONFIG; $startTime = microtime(true); Config::inst()->update('Director', 'environment_type', 'dev'); if (isset($TESTING_CONFIG['database']) && $TESTING_CONFIG['database'] != 'silverstripe_testing') { global $databaseConfig; $newConfig = $databaseConfig; $newConfig = array_merge($databaseConfig, $TESTING_CONFIG); $newConfig['memory'] = isset($TESTING_CONFIG['memory']) ? $TESTING_CONFIG['memory'] : true; $type = isset($newConfig['type']) ? $newConfig['type'] : 'MySQL'; Debug::message("Connecting to new database {$type} as defined by testing config"); DB::connect($newConfig); DB::getConn()->selectDatabase($TESTING_CONFIG['database']); $dbadmin = new DatabaseAdmin(); $dbadmin->clearAllData(); if (!(isset($_REQUEST['build']) && $_REQUEST['build'] == 0)) { $dbadmin->doBuild(true); } } // XDEBUG seem to cause problems with test execution :-( if (function_exists('xdebug_disable')) { xdebug_disable(); } ini_set('max_execution_time', 0); $this->setUp(); // Optionally skip certain tests $skipTests = array(); if ($this->request->getVar('SkipTests')) { $skipTests = explode(',', $this->request->getVar('SkipTests')); } $classList = array_diff($classList, $skipTests); // run tests before outputting anything to the client $suite = new PHPUnit_Framework_TestSuite(); natcasesort($classList); foreach ($classList as $className) { // Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this. class_exists($className); $suite->addTest(new SapphireTestSuite($className)); } // Remove the error handler so that PHPUnit can add its own restore_error_handler(); // CUSTOMISATION if (Director::is_cli()) { if (isset($TESTING_CONFIG['reporter'])) { $clazz = $TESTING_CONFIG['reporter']; } else { $clazz = "CliTestReporter"; } } else { $clazz = "SapphireTestReporter"; } // END CUSTOMISATION $reporter = new $clazz(); $default = self::$default_reporter; self::$default_reporter->writeHeader("Sapphire Test Runner"); if (count($classList) > 1) { self::$default_reporter->writeInfo("All Tests", "Running test cases: " . implode(",", $classList)); } else { self::$default_reporter->writeInfo($classList[0], ""); } $results = new PHPUnit_Framework_TestResult(); $results->addListener($reporter); if ($coverage === true) { $results->collectCodeCoverageInformation(true); $suite->run($results); if (!file_exists(ASSETS_PATH . '/coverage-report')) { mkdir(ASSETS_PATH . '/coverage-report'); } PHPUnit_Util_Report::render($results, ASSETS_PATH . '/coverage-report/'); $coverageApp = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/', '_', preg_replace('/(\\/$)|(^\\/)/', '', Director::baseFolder())) . '.html'; $coverageTemplates = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/', '_', preg_replace('/(\\/$)|(^\\/)/', '', realpath(TEMP_FOLDER))) . '.html'; echo "<p>Coverage reports available here:<ul>\n\t\t\t\t<li><a href=\"{$coverageApp}\">Coverage report of the application</a></li>\n\t\t\t\t<li><a href=\"{$coverageTemplates}\">Coverage report of the templates</a></li>\n\t\t\t</ul>"; } else { $suite->run($results); } if (!Director::is_cli()) { echo '<div class="trace">'; } // CUSTOMISATION $outputFile = null; if ($TESTING_CONFIG['logfile']) { $outputFile = BASE_PATH . '/' . $TESTING_CONFIG['logfile']; } $reporter->writeResults($outputFile); // END CUSTOMISATION $endTime = microtime(true); if (Director::is_cli()) { echo "\n\nTotal time: " . round($endTime - $startTime, 3) . " seconds\n"; } else { echo "<p>Total time: " . round($endTime - $startTime, 3) . " seconds</p>\n"; } if (!Director::is_cli()) { echo '</div>'; } // Put the error handlers back Debug::loadErrorHandlers(); if (!Director::is_cli()) { self::$default_reporter->writeFooter(); } $this->tearDown(); // Todo: we should figure out how to pass this data back through Director more cleanly if (Director::is_cli() && $results->failureCount() + $results->errorCount() > 0) { exit(2); } }