Add a phpunit extension to flag slow tests

See https://aaronsaray.com/2021/finding-slow-tests-in-phpunit-9/
This commit is contained in:
Corey McKrill 2024-08-16 15:40:02 -07:00
parent db9623395d
commit c6953426d7
No known key found for this signature in database
GPG Key ID: 84BBFE669C4D97B8
3 changed files with 36 additions and 0 deletions

View File

@ -49,4 +49,7 @@
<file>./includes/wc-widget-functions.php</file>
</exclude>
</coverage>
<extensions>
<extension class="Automattic\WooCommerce\Tests\SlowTests" />
</extensions>
</phpunit>

View File

@ -287,6 +287,9 @@ class WC_Unit_Tests_Bootstrap {
require_once $this->tests_dir . '/framework/traits/trait-wc-rest-api-complex-meta.php';
require_once dirname( $this->tests_dir ) . '/php/helpers/HPOSToggleTrait.php';
require_once dirname( $this->tests_dir ) . '/php/helpers/SerializingCacheTrait.php';
// Extensions.
require_once dirname( $this->tests_dir ) . '/php/helpers/SlowTests.php';
}
/**

View File

@ -0,0 +1,30 @@
<?php
namespace Automattic\WooCommerce\Tests;
use Automattic\Jetpack\IdentityCrisis\Exception;
use PHPUnit\Runner\AfterTestHook;
/**
*
*/
class SlowTests implements AfterTestHook {
/**
* Threshold of what's considered "slow".
*/
protected const MAX_SECONDS_ALLOWED = 3;
/**
*
*
* @param string $test Name of the test.
* @param float $time Elapsed time in seconds of the test.
*
* @return void
*/
public function executeAfterTest( string $test, float $time ): void {
if ( $time > self::MAX_SECONDS_ALLOWED ) {
fwrite( STDERR, sprintf( "\nThe %s test took %s seconds!\n", $test, $time ) );
}
}
}