Skip to content

Commit

Permalink
Enhancement: Allow using duration as option value for execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed May 6, 2019
1 parent b2da7e6 commit aeaa29a
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/TextUI/Command.php
Expand Up @@ -1282,6 +1282,11 @@ private function handleOrderByOption(string $value): void

break;

case 'duration':
$this->arguments['executionOrder'] = TestSuiteSorter::ORDER_DURATION;

break;

default:
$this->exitWithErrorMessage("unrecognized --order-by option: $order");
}
Expand Down
2 changes: 1 addition & 1 deletion src/TextUI/Help.php
Expand Up @@ -100,7 +100,7 @@ final class Help
['arg' => '--printer <printer>', 'desc' => 'TestListener implementation to use'],
['spacer' => ''],

['arg' => '--order-by=<order>', 'desc' => 'Run tests in order: default|reverse|random|defects|no-depends'],
['arg' => '--order-by=<order>', 'desc' => 'Run tests in order: default|reverse|random|defects|no-depends|duration'],
['arg' => '--random-order-seed=<N>', 'desc' => 'Use a specific random seed <N> for random order'],
['arg' => '--cache-result', 'desc' => 'Write test results to cache file'],
['arg' => '--do-not-cache-result', 'desc' => 'Do not write test results to cache file'],
Expand Down
4 changes: 4 additions & 0 deletions src/Util/Configuration.php
Expand Up @@ -853,6 +853,10 @@ public function getPHPUnitConfiguration(): array
case 'no-depends':
$result['resolveDependencies'] = false;

break;
case 'duration':
$result['executionOrder'] = TestSuiteSorter::ORDER_DURATION;

break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/cli/_files/output-cli-help-color.txt
Expand Up @@ -99,7 +99,7 @@
--printer <printer>  TestListener implementation to use

--order-by=<order>  Run tests in order:
default|reverse|random|defects|no-depends
default|reverse|random|defects|no-depends|duration
--random-order-seed=<N>  Use a specific random seed <N> for random
order
--cache-result  Write test results to cache file
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/cli/_files/output-cli-usage.txt
Expand Up @@ -77,7 +77,7 @@ Test Execution Options:
--testdox-exclude-group Exclude tests from the specified group(s)
--printer <printer> TestListener implementation to use

--order-by=<order> Run tests in order: default|reverse|random|defects|no-depends
--order-by=<order> Run tests in order: default|reverse|random|defects|no-depends|duration
--random-order-seed=<N> Use a specific random seed <N> for random order
--cache-result Write test results to cache file
--do-not-cache-result Do not write test results to cache file
Expand Down
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;

final class TestWithDifferentDurations extends TestCase
{
public function testOne(): void
{
$this->assertTrue(true);
}

public function testTwo(): void
{
$this->assertTrue(true);
}

public function testThree(): void
{
$this->assertTrue(true);
}
}
@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":199:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:3:{s:35:"TestWithDifferentDurations::testOne";d:1.000;s:35:"TestWithDifferentDurations::testTwo";d:0.500;s:37:"TestWithDifferentDurations::testThree";d:1.500;}}}
@@ -0,0 +1,11 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
executionOrder="duration"
>
<testsuites>
<testsuite name="order-by-duration">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
34 changes: 34 additions & 0 deletions tests/end-to-end/execution-order/order-by-duration-via-cli.phpt
@@ -0,0 +1,34 @@
--TEST--
phpunit --order-by=duration ./tests/end-to-end/execution-order/_files/TestWithDifferentDurations.php
--FILE--
<?php

$tmpResultCache = tempnam(sys_get_temp_dir(), __FILE__);
file_put_contents($tmpResultCache, file_get_contents(__DIR__ . '/_files/TestWithDifferentDurations.phpunit.result.cache.txt'));

$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = '--debug';
$_SERVER['argv'][3] = '--order-by=duration';
$_SERVER['argv'][4] = '--cache-result';
$_SERVER['argv'][5] = '--cache-result-file=' . $tmpResultCache;
$_SERVER['argv'][6] = 'TestWithDifferentDurations';
$_SERVER['argv'][7] = __DIR__ . '/_files/TestWithDifferentDurations.php';

require __DIR__ . '/../../bootstrap.php';
PHPUnit\TextUI\Command::main();

unlink($tmpResultCache);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Test 'TestWithDifferentDurations::testTwo' started
Test 'TestWithDifferentDurations::testTwo' ended
Test 'TestWithDifferentDurations::testOne' started
Test 'TestWithDifferentDurations::testOne' ended
Test 'TestWithDifferentDurations::testThree' started
Test 'TestWithDifferentDurations::testThree' ended


Time: %s, Memory: %s

OK (3 tests, 3 assertions)
@@ -0,0 +1,35 @@
--TEST--
phpunit --order-by=duration ./tests/end-to-end/execution-order/_files/TestWithDifferentDurations.php
--FILE--
<?php

$tmpResultCache = tempnam(sys_get_temp_dir(), __FILE__);
file_put_contents($tmpResultCache, file_get_contents(__DIR__ . '/_files/TestWithDifferentDurations.phpunit.result.cache.txt'));

$phpunitXmlConfig = __DIR__ . '/_files/order-by-duration.phpunit.xml';

$_SERVER['argv'][1] = '--configuration=' . $phpunitXmlConfig;
$_SERVER['argv'][2] = '--debug';
$_SERVER['argv'][4] = '--cache-result';
$_SERVER['argv'][5] = '--cache-result-file=' . $tmpResultCache;
$_SERVER['argv'][6] = 'TestWithDifferentDurations';
$_SERVER['argv'][7] = __DIR__ . '/_files/TestWithDifferentDurations.php';

require __DIR__ . '/../../bootstrap.php';
PHPUnit\TextUI\Command::main();

unlink($tmpResultCache);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Test 'TestWithDifferentDurations::testTwo' started
Test 'TestWithDifferentDurations::testTwo' ended
Test 'TestWithDifferentDurations::testOne' started
Test 'TestWithDifferentDurations::testOne' ended
Test 'TestWithDifferentDurations::testThree' started
Test 'TestWithDifferentDurations::testThree' ended


Time: %s, Memory: %s

OK (3 tests, 3 assertions)

0 comments on commit aeaa29a

Please sign in to comment.