From 946a9e44a3338d2ca376d54d567254f94a7c9d5c Mon Sep 17 00:00:00 2001 From: Edward Speyer Date: Tue, 24 Apr 2012 19:16:45 -0700 Subject: [PATCH] Allow tests to be skipped Summary: Allow tests to be skipped by calling assertSkipped(). It's not really an assertion of anything tangible; more like "assert that we can't really assert anything right now". Test Plan: Added a new test to the PhutilUnitTestEngineTestCase. Reviewers: epriestley Reviewed By: epriestley CC: aran, Koolvin Differential Revision: https://secure.phabricator.com/D2312 --- src/__phutil_library_map__.php | 1 + .../PhutilUnitTestEngineTestCase.php | 6 +++- .../testcase/ArcanistPhutilTestCase.php | 35 +++++++++++++++++++ .../ArcanistPhutilTestSkippedException.php | 24 +++++++++++++ .../phutil/testcase/exception/__init__.php | 1 + 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/unit/engine/phutil/testcase/exception/ArcanistPhutilTestSkippedException.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 930431b7..5b9b0239 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -82,6 +82,7 @@ phutil_register_library_map(array( 'ArcanistPatchWorkflow' => 'workflow/patch', 'ArcanistPhutilModuleLinter' => 'lint/linter/phutilmodule', 'ArcanistPhutilTestCase' => 'unit/engine/phutil/testcase', + 'ArcanistPhutilTestSkippedException' => 'unit/engine/phutil/testcase/exception', 'ArcanistPhutilTestTerminatedException' => 'unit/engine/phutil/testcase/exception', 'ArcanistPyFlakesLinter' => 'lint/linter/pyflakes', 'ArcanistPyLintLinter' => 'lint/linter/pylint', diff --git a/src/unit/engine/phutil/__tests__/PhutilUnitTestEngineTestCase.php b/src/unit/engine/phutil/__tests__/PhutilUnitTestEngineTestCase.php index 32723029..d7efb573 100644 --- a/src/unit/engine/phutil/__tests__/PhutilUnitTestEngineTestCase.php +++ b/src/unit/engine/phutil/__tests__/PhutilUnitTestEngineTestCase.php @@ -40,7 +40,7 @@ final class PhutilUnitTestEngineTestCase extends ArcanistPhutilTestCase { self::$allTestsCounter--; - $actual_test_count = 4; + $actual_test_count = 5; $this->assertEqual( $actual_test_count, @@ -86,6 +86,10 @@ final class PhutilUnitTestEngineTestCase extends ArcanistPhutilTestCase { $this->assertFailure('This test is expected to fail.'); } + public function testSkip() { + $this->assertSkipped('This test is expected to skip.'); + } + public function testTryTestCases() { $this->tryTestCases( array( diff --git a/src/unit/engine/phutil/testcase/ArcanistPhutilTestCase.php b/src/unit/engine/phutil/testcase/ArcanistPhutilTestCase.php index a6b1dc01..9d46e457 100644 --- a/src/unit/engine/phutil/testcase/ArcanistPhutilTestCase.php +++ b/src/unit/engine/phutil/testcase/ArcanistPhutilTestCase.php @@ -109,6 +109,19 @@ abstract class ArcanistPhutilTestCase { throw new ArcanistPhutilTestTerminatedException($message); } + /** + * End this test by asserting that the test should be skipped for some + * reason. + * + * @param string Reason for skipping this test. + * @return void + * @task assert + */ + final protected function assertSkipped($message) { + $this->skipTest($message); + throw new ArcanistPhutilTestTerminatedException($message); + } + /* -( Exception Handling )------------------------------------------------- */ @@ -367,6 +380,26 @@ abstract class ArcanistPhutilTestCase { } + /** + * Mark the current running test as skipped. + * + * @param string Description for why this test was skipped. + * @return void + * @task internal + */ + final private function skipTest($reason) { + $coverage = $this->endCoverage(); + + $result = new ArcanistUnitTestResult(); + $result->setCoverage($coverage); + $result->setName($this->runningTest); + $result->setResult(ArcanistUnitTestResult::RESULT_SKIP); + $result->setDuration(microtime(true) - $this->testStartTime); + $result->setUserData($reason); + $this->results[] = $result; + } + + /** * Execute the tests in this test case. You should not call this directly; * use @{class:PhutilUnitTestEngine} to orchestrate test execution. @@ -411,6 +444,8 @@ abstract class ArcanistPhutilTestCase { } } catch (ArcanistPhutilTestTerminatedException $ex) { // Continue with the next test. + } catch (ArcanistPhutilTestSkippedException $ex) { + // Continue with the next test. } catch (Exception $ex) { $ex_class = get_class($ex); $ex_message = $ex->getMessage(); diff --git a/src/unit/engine/phutil/testcase/exception/ArcanistPhutilTestSkippedException.php b/src/unit/engine/phutil/testcase/exception/ArcanistPhutilTestSkippedException.php new file mode 100644 index 00000000..a9cf91a6 --- /dev/null +++ b/src/unit/engine/phutil/testcase/exception/ArcanistPhutilTestSkippedException.php @@ -0,0 +1,24 @@ +