您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > PHPUnit
利用PHPUnit單元測試對PHP代碼進行檢查
作者:網(wǎng)絡轉載 發(fā)布時間:[ 2014/1/2 13:13:06 ] 推薦標簽:PHPUnit 代碼 檢查

清單 3. 測試失敗

% phpunit TestAdd.php
PHPUnit 2.2.1 by Sebastian Bergmann.

FF

Time: 0.0031270980834961
There were 2 failures:
1) test1(TestAdd)

2) test2(TestAdd)


FAILURES!!!
Tests run: 2, Failures: 2, Errors: 0, Incomplete Tests: 0.

現(xiàn)在我知道這兩個測試都可以正常工作了。因此,可以修改add()函數(shù)來真正地做實際的事情了。

<?php
function add( $a, $b ) { return $a+$b; }
?>

現(xiàn)在這兩個測試都可以通過了。

清單 4. 測試通過

% phpunit TestAdd.php
PHPUnit 2.2.1 by Sebastian Bergmann.

..

Time: 0.0023679733276367

OK (2 tests)
%

    盡管這個測試驅動開發(fā)的例子非常簡單,但是我們可以從中體會到它的思想。我們首先創(chuàng)建了測試用例,并且有足夠多的代碼讓這個測試運行起來,不過結果是錯誤的。然后我們驗證測試的確是失敗的,接著實現(xiàn)了實際的代碼使這個測試能夠通過。

    我發(fā)現(xiàn)在實現(xiàn)代碼時我會一直不斷地添加代碼,直到擁有一個覆蓋所有代碼路徑的完整測試為止。在本文的后,您會看到有關編寫什么測試和如何編寫這些測試的一些建議。

數(shù)據(jù)庫測試

    在進行模塊測試之后,可以進行數(shù)據(jù)庫訪問測試了。數(shù)據(jù)庫訪問測試帶來了兩個有趣的問題。首先,我們必須在每次測試之前將數(shù)據(jù)庫恢復到某個已知點。其次,要注意這種恢復可能會對現(xiàn)有數(shù)據(jù)庫造成破壞,因此我們必須對非生產(chǎn)數(shù)據(jù)庫進行測試,或者在編寫測試用例時注意不能影響現(xiàn)有數(shù)據(jù)庫的內容。

數(shù)據(jù)庫的單元測試是從數(shù)據(jù)庫開始的。為了闡述這個問題,我們需要使用下面的簡單模式。


清單 5. Schema.sql

DROP TABLE IF EXISTS authors;
CREATE TABLE authors (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name TEXT NOT NULL,
PRIMARY KEY ( id )
);

清單 5 是一個 authors 表,每條記錄都有一個相關的 ID。

接下來,可以編寫測試用例了。


清單 6. TestAuthors.php

<?php
require_once 'dblib.php';
require_once 'PHPUnit2/Framework/TestCase.php';

class TestAuthors extends PHPUnit2_Framework_TestCase
{
function test_delete_all() {
$this->assertTrue( Authors::delete_all() );
}
function test_insert() {
$this->assertTrue( Authors::delete_all() );
$this->assertTrue( Authors::insert( 'Jack' ) );
}
function test_insert_and_get() {
$this->assertTrue( Authors::delete_all() );
$this->assertTrue( Authors::insert( 'Jack' ) );
$this->assertTrue( Authors::insert( 'Joe' ) );
$found = Authors::get_all();
$this->assertTrue( $found != null );
$this->assertTrue( count( $found ) == 2 );
}
}
?>

上一頁123456下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd