<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 第 4 章?基境(fixture) 在編寫測試時,最費時的部分之一是編寫代碼來將整個場景設置成某個已知的狀態,并在測試結束后將其復原到初始狀態。這個已知的狀態稱為測試的 *基境(fixture)*。 在[Example?2.1, “用 PHPUnit 測試數組操作”](# "Example?2.1.?用 PHPUnit 測試數組操作")中,基境十分簡單,就是存儲在 `$stack` 變量中的數組。然而,絕大多數時候基境均遠比一個簡單數組要復雜,用于建立基境的代碼量也會隨之增長。測試的真正內容就被淹沒于建立基境帶來的干擾中。當編寫多個需要類似基境的測試時這個問題就變得更糟糕了。如果沒有來自于測試框架的幫助,就不得不在寫每一個測試時都將建立基境的代碼重復一次。 PHPUnit 支持共享建立基境的代碼。在運行某個測試方法前,會調用一個名叫 `setUp()` 的模板方法。`setUp()` 是創建測試所用對象的地方。當測試方法運行結束后,不管是成功還是失敗,都會調用另外一個名叫 `tearDown()` 的模板方法。`tearDown()` 是清理測試所用對象的地方。 在[Example?2.2, “用 `@depends` 標注來表達依賴關系”](# "Example?2.2.?用 @depends 標注來表達依賴關系")中,我們在測試之間運用生產者-消費者關系來共享基境。這并非總是預期的方式,甚至有時是不可能的。[Example?4.1, “用 setUp() 建立棧的基境”](# "Example?4.1.?用 setUp() 建立棧的基境")展示了另外一個編寫測試 `StackTest` 的方式。在這個方式中,不再重用基境本身,而是重用建立基境的代碼。首先聲明一個實例變量,`$stack`,用來替代方法內的局部變量。然后把 `array` 基境的建立放到 `setUp()` 方法中。最后,從測試方法中去除冗余代碼,在 `assertEquals()` 斷言方法中使用新引入的實例變量 `$this->stack`替代方法內的局部變量 `$stack`。 **Example?4.1.?用 setUp() 建立棧的基境** ~~~ <?php class StackTest extends PHPUnit_Framework_TestCase { protected $stack; protected function setUp() { $this->stack = array(); } public function testEmpty() { $this->assertTrue(empty($this->stack)); } public function testPush() { array_push($this->stack, 'foo'); $this->assertEquals('foo', $this->stack[count($this->stack)-1]); $this->assertFalse(empty($this->stack)); } public function testPop() { array_push($this->stack, 'foo'); $this->assertEquals('foo', array_pop($this->stack)); $this->assertTrue(empty($this->stack)); } } ?> ~~~ 測試類的每個測試方法都會運行一次 `setUp()` 和 `tearDown()` 模板方法(同時,每個測試方法都是在一個全新的測試類實例上運行的)。 另外,`setUpBeforeClass()` 與 `tearDownAfterClass()` 模板方法將分別在測試用例類的第一個測試運行之前和測試用例類的最后一個測試運行之后調用。 下面這個例子中展示了測試用例類中所有可用的模板方法。 **Example?4.2.?展示所有可用模板方法的例子** ~~~ <?php class TemplateMethodsTest extends PHPUnit_Framework_TestCase { public static function setUpBeforeClass() { fwrite(STDOUT, __METHOD__ . "\n"); } protected function setUp() { fwrite(STDOUT, __METHOD__ . "\n"); } protected function assertPreConditions() { fwrite(STDOUT, __METHOD__ . "\n"); } public function testOne() { fwrite(STDOUT, __METHOD__ . "\n"); $this->assertTrue(TRUE); } public function testTwo() { fwrite(STDOUT, __METHOD__ . "\n"); $this->assertTrue(FALSE); } protected function assertPostConditions() { fwrite(STDOUT, __METHOD__ . "\n"); } protected function tearDown() { fwrite(STDOUT, __METHOD__ . "\n"); } public static function tearDownAfterClass() { fwrite(STDOUT, __METHOD__ . "\n"); } protected function onNotSuccessfulTest(Exception $e) { fwrite(STDOUT, __METHOD__ . "\n"); throw $e; } } ?> ~~~ ~~~ phpunit TemplateMethodsTest PHPUnit 5.0.0 by Sebastian Bergmann and contributors. TemplateMethodsTest::setUpBeforeClass TemplateMethodsTest::setUp TemplateMethodsTest::assertPreConditions TemplateMethodsTest::testOne TemplateMethodsTest::assertPostConditions TemplateMethodsTest::tearDown .TemplateMethodsTest::setUp TemplateMethodsTest::assertPreConditions TemplateMethodsTest::testTwo TemplateMethodsTest::tearDown TemplateMethodsTest::onNotSuccessfulTest FTemplateMethodsTest::tearDownAfterClass Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) TemplateMethodsTest::testTwo Failed asserting that <boolean:false> is true. /home/sb/TemplateMethodsTest.php:30 FAILURES! Tests: 2, Assertions: 2, Failures: 1. ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看