Skip to content

Commit

Permalink
TestCase: added method skip() for skipping tests [Closes #379]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 31, 2021
1 parent 4e4599a commit d09fdf1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
27 changes: 25 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ public function run(): void
$this->sendMethodList($methods);
return;
}
$this->runTest($method);

try {
$this->runTest($method);
} catch (TestCaseSkippedException $e) {
Environment::skip($e->getMessage());
}

} else {
foreach ($methods as $method) {
$this->runTest($method);
try {
$this->runTest($method);
} catch (TestCaseSkippedException $e) {
echo "\nSkipped:\n{$e->getMessage()}\n";
}
}
}
}
Expand Down Expand Up @@ -213,6 +222,15 @@ private function silentTearDown(): void
}


/**
* Skips the test.
*/
protected function skip(string $message = ''): void
{
throw new TestCaseSkippedException($message);
}


private function sendMethodList(array $methods): void
{
Environment::$checkAssertions = false;
Expand Down Expand Up @@ -243,3 +261,8 @@ private function sendMethodList(array $methods): void
class TestCaseException extends \Exception
{
}


class TestCaseSkippedException extends \Exception
{
}
28 changes: 28 additions & 0 deletions tests/Framework/TestCase.skip.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


class TestCaseTest extends Tester\TestCase
{
public function testSkip()
{
$this->skip('foo');
thisIsNotExecuted();
}
}


Assert::exception(function () {
$test = new TestCaseTest;
$test->runTest('testSkip');
}, Tester\TestCaseSkippedException::class, 'foo');

Assert::noError(function () {
$test = new TestCaseTest;
$test->run();
});
9 changes: 8 additions & 1 deletion tests/Runner/Runner.multiple-fails.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ Assert::match(
Assert::same(Test::FAILED, $logger->results['testcase-syntax-error.phptx'][0]);


Assert::same(5, count($logger->results));
Assert::match(
'foo',
trim($logger->results['testcase-skip.phptx'][1])
);
Assert::same(Test::SKIPPED, $logger->results['testcase-skip.phptx'][0]);


Assert::same(6, count($logger->results));
20 changes: 20 additions & 0 deletions tests/Runner/multiple-fails/testcase-skip.phptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* @testcase
*/

use Tester\TestCase;

require __DIR__ . '/../../bootstrap.php';


class MyTest extends TestCase
{
public function testSkipped()
{
$this->skip('foo');
}
}

(new MyTest)->run();

0 comments on commit d09fdf1

Please sign in to comment.