PHP單元測試利器:PHPUNIT深入用法(二)

來源:互聯網
上載者:User

在上一篇PHP單元測試利器:PHPUNIT初探文章中,我們對phpunit有了一個初步的認識,在本文中將繼續深入講解下phpunit中的一些用法。

  1、markTestSkipped和markTestIncomplete

  在phpunit中,有兩個有用的方法markTestSkipped和markTestIncomplete。它們能允許你編寫的單元測試中不單是只有通過和失敗兩種結果。markTestSkipped能讓PHPUNIT不去執行某個已經編寫好的測試方法。舉個例子說明,比如下面的程式:

<?php
public function testThisMightHaveADb()
{
  $myObject->createObject();
  try {
    $db = new Database();
    $this->assertTrue($db->rowExists());
  } catch (DatabseException $e) {
    $this->markTestSkipped("This test was skipped because there was a database problem");
  }
}
?>

   在上面的程式中,是一個串連資料庫後,判斷資料是否存在的測試方法,但如果考慮資料庫的串連異常的話,則應該在拋出異常時,使用markTestSkipped指出該測試方法應該是被忽略的,因為出現了異常,而注意的時,此時有可能你寫的代碼是正確的,只不過是出現了異常而已,這樣phpunit在輸出時就不會只是簡單的輸出fail。

  而markTestIncomplete也有點類似,但有點不同的是,它是當開發人員在編寫一個未完成的測試方法時使用的,標記出某個測試方法還沒編寫完成,同樣測試結果也不會是fail,只是告訴phpunit這個測試方法還沒編寫完成而已,例子如下:

<?php
public function testAreNotEnoughHours()
{
  $this->markTestIncomplete("There aren"t enough hours in the day to have my tests go green");
  $trueVariable = true;
  $this->assertTrue($trueVariable);
}
?>

   2、更深入瞭解phpunit中的斷言

  在上一篇文章中,已經基本講解了一些基本的phpunit中的斷言的使用,這裡以一個例子,下面是一個類的代碼:

<?php
class Testable
{
  public $trueProperty = true;
  public $resetMe = true;
  public $testArray = array(
    "first key" => 1,
    "second key" => 2
  );
  private $testString = "I do love me some strings";
  public function __construct()
  {
  }
  public function addValues($valueOne,$valueTwo) {
    return $valueOne+$valueTwo;
  }
  public function getTestString()
  {
    return $this->testString;
  }
}
?>

   我們編寫的單元測試代碼初步的架構如下:

<?php
class TestableTest extends PHPUnit_Framework_TestCase
{
  private $_testable = null;
  public function setUp()
  {
    $this->_testable = new Testable();
  }
  public function tearDown()
  {
    $this->_testable = null;
  }
  /** test methods will go here */
}
?>

   在上一篇文章中,已經介紹了setUp方法和tearDown方法,這裡的setUp方法中,建立了Testable()執行個體並儲存在變數$_testable中,而在tearDown方法中,銷毀了該對象。

  接下來,開始編寫一些斷言去測試,首先看assertTrue和assertFalase:

<?php
public function testTruePropertyIsTrue()
{
  $this->assertTrue($this->_testable->trueProperty,"trueProperty isn"t true");
}
public function testTruePropertyIsFalse()
{
  $this->assertFalse($this->_testable->trueProperty, "trueProperty isn"t false");
}
?>

在上一篇文章中已經介紹過assertTrue和assertFalse了,這裡留意一下其中的第二個參數,其含義是,當該斷言的測試不通過時,自訂的顯示資訊。比如在這個測試方法中,當trueProperty不為真值時,將顯示“trueProperty isn"t true”的資訊。

  接下來再看下在數值方面上phpunit的斷言使用執行個體:

<?php
public function testValueEquals()
{
  $valueOne = 4;
  $valueTwo = 2;
  $this->assertEquals($this->_testable->addValues($valueOne,$valueTwo),6);
}
public function testValueGreaterThan()
{
  $valueOne = 4;
  $valueTwo = 2;
  $this->assertGreaterThan($valueTwo,$valueOne);
}
public function testLessThanOrEqual()
{
  $valueOne = 4;
  $valueTwo = 2;
  $this->assertLessThanOrEqual($valueTwo,$valueOne);
}
public function testAreObjectsEqual()
{
  $testTwo = new Testable();
  $this->_testable->resetMe = false;
  $this->assertEquals($this->_testable,$testTwo);
}
?>

   其中,assertEquals為判斷是否相等,assertGreaterThan為判斷是否大於,assertLessThanOrEqual判斷是否小於或等於,而assertEquals這裡要注意一下,它還可以用來判斷兩個對象是否相等,比如這裡就判斷了$testTwo這個Testable類的執行個體是否和新設定的resetMe這個對象相等。

  除了在數值方面的斷言外,在字元方面還有一些很多斷言的功能,看下面的代碼:

<?php
public function testStringEnding()
{
  $testString = $this->_testable->getTestString();
  $this->assertStringEndsWith("frood",$testString);
}
public function testStringStarts()
{
  $testString = $this->_testable->getTestString();
  $this->assertStringStartsWith("hoopy",$testString);
}
public function testEqualFileContents()
{
  $this->assertStringEqualsFile("/path/to/textfile.txt","foo");
}
public function testDoesStringMatchFormat()
{
  $testString = $this->_testable->getTestString();
  $this->assertStringMatchesFormat("%s",$testString);
}
?>

   其中, assertStringStartsWith斷言是判斷字串是否以指定的字串開頭,assertStringEndsWith斷言判斷字串是否以指定的字串結尾。assertStringEqualsFile斷言判斷給定的檔案中是否含有指定的字元,比如這裡就判斷textfile.txt這個檔案中是否包含字串foo。

  而assertStringMatchesFormat可以讓使用者指定匹配的模式去判斷一個字串是否符合要求,如 $this->assertStringMatchesFormat("%s",$testString);

  這裡則判斷$testString是否是字串類型,具體的可以參考phpunit手冊。

  再來看如下的代碼:

<?php
public function testStringIsNotNull()
{
  $notANull = “i"m not a null!”;
  $this->assertNull($notANull);
}
public function testStringIsSame()
{
  $numberAsString = "1234";
  $this->assertSame(1234,$numberAsString);
}
?>

   其中assertNull判斷某個變數是否為null,而assertSame則嚴格判斷兩個變數是否同一個類型,儘管在PHP中是弱類型語言,但這裡通過assertSame還是能判斷出$numberAsString為字串類型,跟期望的1234數字類型不符,所以測試不能通過。

  最後我們來看一下平常可能不大常用的斷言,但又可能對你的單元測試工作十分有協助的,先看代碼如下:

<?php
public function testArrayKeyExists()
{
    $this->assertArrayHasKey("first key",$this->_testable->testArray);
}
public function testAttributeExists()
{
    $this->assertClassHasAttribute("resetMe",get_class($this->_testable));
}
public function testFileIsReal()
{
    $this->assertFileExists("/path/to/file.txt");
}
public function testIsInstance()
{
    $this->assertInstanceOf("OtherClass",$this->_testable);
}
<?php
public function testDoesMatchRegex()
{
  $testString = $this->_testable->getTestString();
  $this->assertRegExp("/[a-z]+/",$testString);
}
?>

   代碼中第一個斷言assertArrayHasKey,是用來檢查一個數組中是否每個索引值都是存在的,比如我們的數組中,“firstkey”這個值是有鍵1與其對應的,所以測試能通過。而assertClassHasAttribute則能判斷某個類是否有相應的屬性,這個例子中測試也能通過;

  而assertFileExists則判斷在本地檔案系統中是否存在指定的檔案。而assertInstanceOf則判斷某個你正在建立的對象是否為某個類的執行個體。assertRegExp相信大家都知道,這個是判斷某個字串中是否與給定的Regex相匹配。

  總結

  本文進一步探討了PHPUNIT中一些重要的方法和斷言,PHPUNIT中還有大量豐富的斷言,對提高單元測試十分有協助,具體的請參考PHPUNIT使用者手冊。




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.