Skip to content

Commit 8a4b60d

Browse files
committed
TestCase: added method skip() for skipping tests [Closes #379]
1 parent 1f4bce2 commit 8a4b60d

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

src/Framework/TestCase.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,20 @@ public function run(): void
4646
$this->sendMethodList($methods);
4747
return;
4848
}
49-
$this->runTest($method);
49+
50+
try {
51+
$this->runTest($method);
52+
} catch (TestCaseSkippedException $e) {
53+
Environment::skip($e->getMessage());
54+
}
5055

5156
} else {
5257
foreach ($methods as $method) {
53-
$this->runTest($method);
58+
try {
59+
$this->runTest($method);
60+
} catch (TestCaseSkippedException $e) {
61+
echo "\nSkipped:\n{$e->getMessage()}\n";
62+
}
5463
}
5564
}
5665
}
@@ -213,6 +222,15 @@ private function silentTearDown(): void
213222
}
214223

215224

225+
/**
226+
* Skips the test.
227+
*/
228+
protected function skip(string $message = ''): void
229+
{
230+
throw new TestCaseSkippedException($message);
231+
}
232+
233+
216234
private function sendMethodList(array $methods): void
217235
{
218236
Environment::$checkAssertions = false;
@@ -243,3 +261,8 @@ private function sendMethodList(array $methods): void
243261
class TestCaseException extends \Exception
244262
{
245263
}
264+
265+
266+
class TestCaseSkippedException extends \Exception
267+
{
268+
}

tests/Framework/TestCase.skip.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tester\Assert;
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
class TestCaseTest extends Tester\TestCase
11+
{
12+
public function testSkip()
13+
{
14+
$this->skip('foo');
15+
thisIsNotExecuted();
16+
}
17+
}
18+
19+
20+
Assert::exception(function () {
21+
$test = new TestCaseTest;
22+
$test->runTest('testSkip');
23+
}, Tester\TestCaseSkippedException::class, 'foo');
24+
25+
Assert::noError(function () {
26+
$test = new TestCaseTest;
27+
$test->run();
28+
});

tests/Runner/Runner.multiple-fails.phpt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ Assert::match(
8686
Assert::same(Test::FAILED, $logger->results['testcase-syntax-error.phptx'][0]);
8787

8888

89-
Assert::same(5, count($logger->results));
89+
Assert::match(
90+
'foo',
91+
trim($logger->results['testcase-skip.phptx'][1])
92+
);
93+
Assert::same(Test::SKIPPED, $logger->results['testcase-skip.phptx'][0]);
94+
95+
96+
Assert::same(6, count($logger->results));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @testcase
5+
*/
6+
7+
use Tester\TestCase;
8+
9+
require __DIR__ . '/../../bootstrap.php';
10+
11+
12+
class MyTest extends TestCase
13+
{
14+
public function testSkipped()
15+
{
16+
$this->skip('foo');
17+
}
18+
}
19+
20+
(new MyTest)->run();

0 commit comments

Comments
 (0)