From 5c592928d48e7b149f1c13ad4f4bf6ff577ec0d2 Mon Sep 17 00:00:00 2001 From: luke Date: Tue, 10 Aug 2021 14:34:10 +0100 Subject: [PATCH] Adds a new method, `isInParallel`, to the `Testable` trait to allow a test to determine its parallel status. --- src/Concerns/Testable.php | 8 ++++++++ src/Functions.php | 3 ++- tests/.snapshots/success.txt | 7 ++++++- tests/Features/Parallel.php | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/Features/Parallel.php diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index 72bf02bb..8d5b5eed 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -304,4 +304,12 @@ trait Testable { return ltrim(self::class, 'P\\'); } + + /** + * Determine whether this test case is being executed in a parallel environment. + */ + public function isInParallel(): bool + { + return TestSuite::getInstance()->isInParallel; + } } diff --git a/src/Functions.php b/src/Functions.php index e7a230dc..55d4ba88 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Pest\Concerns\Testable; use Pest\Datasets; use Pest\Expectation; use Pest\PendingObjects\AfterEachCall; @@ -85,7 +86,7 @@ if (!function_exists('test')) { * is the test description; the second argument is * a closure that contains the test expectations. * - * @return TestCall|TestCase|mixed + * @return TestCall|TestCase|Testable|mixed */ function test(string $description = null, Closure $closure = null) { diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index a6f69129..bc9e5842 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -485,6 +485,11 @@ ✓ it can call chained macro method ✓ it will throw exception from call if no macro exists + WARN Tests\Features\Parallel + - it can determine in the test case if it is running in parallel + ✓ it can determine in the test case if it is not running in parallel + ✓ it can skip using the test case based on parallel status + PASS Tests\Features\PendingHigherOrderTests ✓ get 'foo' ✓ get 'foo' → get 'bar' → expect true → toBeTrue @@ -647,5 +652,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 419 passed + Tests: 4 incompleted, 10 skipped, 421 passed \ No newline at end of file diff --git a/tests/Features/Parallel.php b/tests/Features/Parallel.php new file mode 100644 index 00000000..9bfe6ea4 --- /dev/null +++ b/tests/Features/Parallel.php @@ -0,0 +1,15 @@ +isInParallel())->toBeTrue(); +})->skip(!TestSuite::getInstance()->isInParallel); + +it('can determine in the test case if it is not running in parallel', function () { + expect(test()->isInParallel())->toBeFalse(); +})->skip(TestSuite::getInstance()->isInParallel); + +it('can skip using the test case based on parallel status', function () { + expect(TestSuite::getInstance()->isInParallel)->toBeFalse(); +})->skip(function () { return $this->isInParallel(); });