天天看點

phpunit學習 3:

16:17 2015/12/11

phpunit學習 3:

單元測試的大概步驟是:編寫待測試類,編寫測試用例類,編寫測試類,測試。

1.如果你有多個類,多個測試類的test類,那麼可以編寫一個AllTests套件。包含所有的待測試的測試類,然後在phpunit下統一執行就行。這個類可能是這樣的:

2.最後編寫測試類,不編寫這個類也可以,在指令行下直接跑phpunitcalculatorTest就行。

參見:http://blog.csdn.net/jucrazy/article/details/6720935

斷言

布爾類型

assertTrue 斷言為真

assertFalse 斷言為假

NULL類型

assertNull 斷言為NULL

assertNotNull 斷言非NULL

數字類型

assertEquals 斷言等于

assertNotEquals 斷言不等于

assertGreaterThan 斷言大于

assertGreaterThanOrEqual 斷言大于等于

assertLessThan 斷言小于

assertLessThanOrEqual 斷言小于等于

字元類型

assertContains 斷言包含

assertNotContains 斷言不包含

assertContainsOnly 斷言隻包含

assertNotContainsOnly 斷言不隻包含

數組類型

assertArrayHasKey 斷言有鍵

assertArrayNotHasKey 斷言沒有鍵

對象類型

assertAttributeContains 斷言屬性包含

assertAttributeContainsOnly 斷言屬性隻包含

assertAttributeEquals 斷言屬性等于

assertAttributeGreaterThan 斷言屬性大于

assertAttributeGreaterThanOrEqual 斷言屬性大于等于

assertAttributeLessThan 斷言屬性小于

assertAttributeLessThanOrEqual 斷言屬性小于等于

assertAttributeNotContains 斷言不包含

assertAttributeNotContainsOnly 斷言屬性不隻包含

assertAttributeNotEquals 斷言屬性不等于

assertAttributeNotSame 斷言屬性不相同

assertAttributeSame 斷言屬性相同

assertSame 斷言類型和值都相同

assertNotSame 斷言類型或值不相同

assertObjectHasAttribute 斷言對象有某屬性

assertObjectNotHasAttribute 斷言對象沒有某屬性

class類型

class類型包含對象類型的所有斷言,還有

assertClassHasAttribute 斷言類有某屬性

assertClassHasStaticAttribute 斷言類有某靜态屬性

assertClassNotHasAttribute 斷言類沒有某屬性

assertClassNotHasStaticAttribute 斷言類沒有某靜态屬性

檔案相關

assertFileEquals 斷言檔案内容等于

assertFileExists 斷言檔案存在

assertFileNotEquals 斷言檔案内容不等于

assertFileNotExists 斷言檔案不存在

XML相關

assertXmlFileEqualsXmlFile 斷言XML檔案内容相等

assertXmlFileNotEqualsXmlFile 斷言XML檔案内容不相等

assertXmlStringEqualsXmlFile 斷言XML字元串等于XML檔案内容

assertXmlStringEqualsXmlString 斷言XML字元串相等

assertXmlStringNotEqualsXmlFile 斷言XML字元串不等于XML檔案内容

assertXmlStringNotEqualsXmlString 斷言XML字元串不相等

以下可能更全 但可能沒有分類

分類: phpunit(7)

斷言(Assertions)是PHPUnit提供的一系列對程式執行結果測試的方法。通俗的講,就是斷言執行程式結果為我們期待的值,如果不是則測試失敗,下面是斷言方法的詳細介紹,内容全部來翻譯自PHPUnit的官方文檔,部分方法官方介紹的很模糊,我根據官方的源碼注釋增加了說明和注釋

assertArrayHasKey(mixed $key, array $array[, string $message = '']) s斷言數組$array含有索引$key, $message用于自定義輸出的錯誤資訊,後同

assertClassHasAttribute(string $attributeName, string $className[, string $message = '']) 斷言類$className含有屬性$attributeName

assertClassHasStaticAttribute(string $attributeName, string $className[, string $message = '']) 斷言類$className含有靜态屬性$attributeName

assertContains(mixed $needle, Iterator|array $haystack[, string $message = ''])斷言疊代器對象$haystack/數組$haystack含有$needle

assertNotContains() 與上條相反

assertAttributeContains(mixed $needle, Class|Object $haystack[, string $message = '']) 斷言$needle為一個類/對象$haystack可通路到的屬性(public, protected 和 private)

assertAttributeNotContains() 與上條相反

assertContains(string $needle, string $haystack[, string $message = '']) 斷言字元串$needle在字元串$haystack中

assertContainsOnly(string $type, Iterator|array $haystack[, boolean $isNativeType = NULL, string $message = '']) 斷言疊代器對象/數組$haystack中隻有$type類型的值, $isNativeType 設定為PHP原生類型,$message同上

assertNotContainsOnly() 與上條相反

assertAttributeContainsOnly() 和 assertAttributeNotContainsOnly() 斷言對象的屬性隻有$type類型和非含有$type類型

assertEmpty(mixed $actual[, string $message = '']) 斷言$actual為空

assertNotEmpty() 遇上條相反

assertAttributeEmpty() 和 assertAttributeNotEmpty() 斷言對象的所有屬性為空或不為空

assertEqualXMLStructure(DOMNode $expectedNode, DOMNode $actualNode[, boolean $checkAttributes = FALSE, string $message = '']) 斷言Dom節點$actualNode和DOM節點$expectedNode相同,$checkAttributes FALSE 不斷言節點屬性,TRUE則斷言屬性$message同上

assertEquals(mixed $expected, mixed $actual[, string $message = '']) 斷言複合類型$actual與$expected相同

assertNotEquals() 與上條相反

assertAttributeEquals() and assertAttributeNotEquals() 斷言類屬性$actual與$expected相同

assertEquals(array $expected, array $actual[, string $message = '']) 斷言數組$actual和數組$expected相同

assertFalse(bool $condition[, string $message = '']) 斷言$condition的結果為false

assertFileEquals(string $expected, string $actual[, string $message = '']) 斷言檔案$actual和$expected相同

assertFileExists(string $filename[, string $message = '']) 斷言檔案$filename存在

assertFileNotExists() 與上條相反

assertGreaterThan(mixed $expected, mixed $actual[, string $message = '']) 斷言$actual比$expected大

assertAttributeGreaterThan() 斷言類的屬性用

assertGreaterThanOrEqual(mixed $expected, mixed $actual[, string $message = '']) 斷言$actual大于等于$expected

assertAttributeGreaterThanOrEqual() 斷言類的屬性

assertInstanceOf($expected, $actual[, $message = '']) 斷言$actual為$expected的執行個體

assertNotInstanceOf() 與上相反

assertAttributeInstanceOf() and assertAttributeNotInstanceOf() 斷言類屬性用

assertInternalType($expected, $actual[, $message = '']) 斷言$actual的類型為$expected

assertNotInternalType() 與上相反

assertAttributeInternalType() and assertAttributeNotInternalType() 斷言類屬性用

assertLessThan(mixed $expected, mixed $actual[, string $message = '']) 斷言$actual小于$expected

assertAttributeLessThan() 斷言類屬性小于$expected

assertLessThanOrEqual(mixed $expected, mixed $actual[, string $message = '']) 斷言$actual小于等于$expected

assertAttributeLessThanOrEqual() 斷言類屬性小于等于$expected

assertNull(mixed $variable[, string $message = '']) 斷言$variable的值為null

assertNotNull() 與上條相反

assertObjectHasAttribute(string $attributeName, object $object[, string $message = '']) 斷言$object含有屬性$attributeName

assertObjectNotHasAttribute() 與上條相反

assertRegExp(string $pattern, string $string[, string $message = '']) 斷言字元串$string符合正規表達式$pattern

assertNotRegExp() 與上條相反

assertStringMatchesFormat(string $format, string $string[, string $message = '']) 斷言$string符合$format定義的格式,例如 %i %s等等

assertStringNotMatchesFormat()

與上條相反

assertStringMatchesFormatFile(string $formatFile, string $string[, string $message = ''])

斷言$string路徑的檔案的格式和$formatFile檔案的格式相同

assertStringNotMatchesFormatFile()

assertSame(mixed $expected, mixed $actual[, string $message = ''])

斷言$actual和$expected的類型和值相同

assertNotSame()

assertAttributeSame() and assertAttributeNotSame()

斷言類屬性用

assertSame(object $expected, object $actual[, string $message = ''])

斷言對象$actual和對象$expected相同

assertSelectCount(array $selector, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE])

斷言在$actual文檔中(格式為html或xml)css選擇器$selector有$count個,或有符合$selector的元素(設定$count為true),或沒有符合$selector的元素(設定$count為false)

assertSelectCount("#binder", true, $xml); // 有一個就行

assertSelectCount(".binder", 3, $xml); // 必須有3個?

assertSelectEquals(array $selector, string $content, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE])

斷言在文檔$actual中有符合根據$selector的找到符合$content的$count個元素,當$count等于true和false的時候作用如下:

assertSelectEquals("#binder .name", "Chuck", true, $xml); // 所有的name等于Chuck

assertSelectEquals("#binder .name", "Chuck", false, $xml); // 所有的name不等于Chuck

assertSelectRegExp(array $selector, string $pattern, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE])

assertStringEndsWith(string $suffix, string $string[, string $message = ''])

斷言$string的末尾為$suffix結束

assertStringEndsNotWith()

assertStringEqualsFile(string $expectedFile, string $actualString[, string $message = ''])

斷言$actualString在檔案$expectedFile的内容中

assertStringNotEqualsFile()

assertStringStartsWith(string $prefix, string $string[, string $message = ''])

斷言$string的開頭為$suffix

assertStringStartsNotWith()

assertTag(array $matcher, string $actual[, string $message = '', boolean $isHtml = TRUE])

斷言$actual的内容符合$matcher的定義,matcher的定義如下:

# id: 節點必須帶有id屬性且名稱與id設定的相同

# tags: 節點的名稱必須與tags的值比對

# attributes: 節點的屬性必須與$attributes數組中的值相比對

# content: 文本内容必須與$content的值相同.

# parent: 節點的父節點必須符合$parent數組中定義的内容.

# child: 節點的位元組點中有至少一個直系子節點滿足 $child 數組中定義的内容.

At least one of the node's immediate children must meet the criteria described by the $child associative array.

# ancestor: 節點的父節點中有至少一個節點滿足 $ancestor 數組中定義的内容.

At least one of the node's ancestors must meet the criteria described by the $ancestor associative array.

# descendant: 節點的位元組點中有至少一個子節點滿足 $descendant 數組中定義的内容.

At least one of the node's descendants must meet the criteria described by the $descendant associative array.

# children: 用于計算位元組點的聯合數組

Associative array for counting children of a node.

* count: 符合比對标準的位元組點數目需要和count的值相同

The number of matching children must be equal to this number.

* less_than: 符合比對标準的位元組點數目需要比count的值少

The number of matching children must be less than this number.

* greater_than: 符合比對标準的位元組點數目需要比count的值多

The number of matching children must be greater than this number.

* only: 另外一個聯合數組用于定義配合标準的節點,隻有這些節點才會被計算入内

Another associative array consisting of the keys to use to match on the children, and only matching children will be counted

assertTag的代碼例子(圖檔點選放大):

點選檢視原圖

點選檢視原圖

More complex assertions can be formulated using the PHPUnit_Framework_Constraint classes

更加複雜的斷言可以通過PHPUnit_Framework_Constraint類來制定

PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)

限制允許另外一個限制類為一個類或對象的屬性

Constraint that applies another constraint to an attribute of a class or an object.

PHPUnit_Framework_Constraint_IsAnything anything()

限制接受任意的輸入值

Constraint that accepts any input value.

PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key)

Constraint that asserts that the array it is evaluated for has a given key.

限制斷言評估數組有傳入的$key

PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value)

Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains a given value.

限制斷言一個數組或者實作疊代器接口的對象含有$value

PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10)

Constraint that checks if one value is equal to another.

限制斷言$value和其他的相同

PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10)

Constraint that checks if a value is equal to an attribute of a class or of an object.

限制斷言$value和一個類或對象的屬性的值相同

PHPUnit_Framework_Constraint_FileExists fileExists()

Constraint that checks if the file(name) that it is evaluated for exists.

限制斷言檔案是存在的

PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value)

Constraint that asserts that the value it is evaluated for is greater than a given value.

限制斷言$value是大于傳入的值的

PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value)

Constraint that asserts that the value it is evaluated for is greater than or equal to a given value.

限制斷言$value是大于或等于傳入的值的

PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName)

Constraint that asserts that the class it is evaluated for has a given attribute.

限制斷言類含有屬性$attributeName

PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName)

Constraint that asserts that the class it is evaluated for has a given static attribute.

限制斷言類含有靜态屬性$attributeName

PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName)

Constraint that asserts that the object it is evaluated for has a given attribute.

限制斷言對象含有屬性$attributeName

PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value)

Constraint that asserts that one value is identical to another.

限制斷言$value和其他的完全相同

PHPUnit_Framework_Constraint_IsFalse isFalse()

Constraint that asserts that the value it is evaluated is FALSE.

限制斷言$value的值為false

PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className)

Constraint that asserts that the object it is evaluated for is an instance of a given class.

限制斷言對象是$className的執行個體

PHPUnit_Framework_Constraint_IsNull isNull()

Constraint that asserts that the value it is evaluated is NULL.

限制斷言$value的值為NULL

PHPUnit_Framework_Constraint_IsTrue isTrue()

Constraint that asserts that the value it is evaluated is TRUE.

限制斷言$value的值為TRUE

PHPUnit_Framework_Constraint_IsType isType(string $type)

Constraint that asserts that the value it is evaluated for is of a specified type.

限制斷言對象的類型的為$type

PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value)

Constraint that asserts that the value it is evaluated for is smaller than a given value.限制斷言對象的值小于$value

PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value)

Constraint that asserts that the value it is evaluated for is smaller than or equal to a given value.限制斷言對象的值小于等于$value

logicalAnd()

Logical AND.邏輯的And

logicalNot(PHPUnit_Framework_Constraint $constraint)

Logical NOT.邏輯的

logicalOr()

Logical OR.邏輯的OR

logicalXor()

Logical XOR.邏輯的XOR

PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern)

Constraint that asserts that the string it is evaluated for matches a regular expression.限制斷言字元串符合傳入的正規表達式$pattern

PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case)

Constraint that asserts that the string it is evaluated for contains a given string.限制斷言字元串中含有$string

PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix)

Constraint that asserts that the string it is evaluated for ends with a given suffix.限制斷言字元串由$string結尾

PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix)

Constraint that asserts that the string it is evaluated for starts with a given prefix. 限制斷言字元串由$string開頭

assertThat的用法,使用多個限制和限制邏輯來實作斷言(圖檔點選放大)點選檢視原圖

assertTrue(bool $condition[, string $message = ''])

斷言$condition為True,否則就報告錯誤

assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile[, string $message = ''])

斷言$actualFile和$expectedFile的xml檔案的内容相同,否則就報告錯誤

assertXmlStringEqualsXmlFile(string $expectedFile, string $actualXml[, string $message = ''])

斷言$actualXml的内容和$expectedFile相同,否則就報告錯誤

assertXmlStringEqualsXmlString(string $expectedXml, string $actualXml[, string $message = ''])

斷言$actualXml的内容和$expectedXml相同,否則就報告錯誤

上一篇PHPUnit學習筆記(三)測試方法進階

下一篇PHPUnit學習筆記(五)PHPUnit參數詳

------------

11:27 2015/12/9

phpunit測試學習 2: 分類總結斷言涉及哪些方面

先推薦windows快速打開某處路徑下的cmd,進入測試狀态:

可以在檔案夾中,按住Shift+滑鼠右鍵,這時候你就會看到下面的圖檔。然後點選在此處打開指令行視窗就行了

參照:http://www.jb51.net/os/windows/111622.html

正常打開cmd進入目錄太麻煩太慢了。

基本(assertNotEquals):

真假 是否為空 相等不等 大小等于 類型相同

類(assertClassHasStaticAttribute):

執行個體,對象(assertInstanceOf):

某對象是某類的執行個體 不是某對象的執行個體

字元串(assertStringMatchesFormat):

是否以某字元串為開頭 是否符合某正規表達式(assertRegExp) 是否符合定義的格式(例如 %i %s等等)

類屬性(assertAttributeContains):

屬性類型 有類型 隻有類型 含有某類型 含有某屬性 真假 相等不等

含有靜态屬性 含有某靜态屬性 大小等于 某屬性屬于某個類 是否是某種類型

數組:

含有某索引

函數:

傳回值 傳回類型

檔案(assertFileExists):

某檔案存在與否 檔案相同 檔案路徑下檔案的類型: assertStringMatchesFormat斷言$string符合$format定義

某段内容在某檔案中 的格式,例如 %i %s等等

css(assertSelectCount):

某文檔中某選擇器有n個 有n個符合的元素

(assertSelectEquals("#binder .name", "Chuck", true, $xml); // 所有的name等于Chuck)

xml(assertEqualXMLStructure):

dom節點相同,

assertContainsOnly

assertContains(可以斷言字元串中有某段字元)

assertAttributeGreaterThan(類的屬性某比某大)

assertLessThan

assertAttributeInternalType

...........

很有用:

setUp和tearDown這兩個方法來解決這個問題。

setUp會在類的每個測試用例運作之前被調用,你可以在裡面做一些相關的程式初始化的工作

tearDown方法則會在本類每個測試用例運作完畢之後調用,你可以在裡面進行一些相關的清理工作

需要注意的是,不一定寫了setUp就要對應寫tearDown,tearDown裡面用來回收那些占用資源比較大的對象,如連接配接上的資料庫,打開的檔案等等。

還有很多方法如下:

斷言之前/之後 調用測試用例之前/之後 類調用之前/完之後 出現沒有調用成功的測試用例時

參數:

指定生成指定格式的log日志 指定格式覆寫日志 列出可用測試組 現實測試程序 設定結果顯示顔色 遇到第一個失敗 第一個錯誤 第一個跳過 第一個未完成的測試是停止 某測試沒有定義任何斷言 生成概要測試類 讀測試代碼開啟文法檢查 測試前運作bootstrap的php檔案路徑 在php的inxlude_path中增加路徑 設定php配置屬性 輸出調試資訊(如測試的名稱 什麼時候開始執行)

這個不可好像還不錯,斷言函數分了類的:http://blog.sina.com.cn/s/blog_6d425e1a010199gd.html

--------

16:45 2015/12/8

phpunit測試學習 1:一點簡單的扼要有用的東西的總結 一點入門認識

具體的入門安裝和入門實踐請參照文中的推薦部落格或網上其他部落格

推薦部落格,我感覺這幾篇部落格寫得很不錯,雖然一和二可以結合在一起,内容并不多且給新手更容易帶來學習信心:

安裝建議參照我的另一篇安裝部落格,因為當時我是按照這種方式失敗的,文中提及網絡原因導緻失敗,我當時顯示的是安裝的某個路徑似乎沒有維護了,那個url已經不存在了(是以我也不知道是網絡原因還是真的那個路徑不存在了)(PHP教程:PHPUnit學習筆記(一)PHPUnit介紹及安裝:http://be-evil.org/phpunit-study-note-introduction-and-install.html)

PHP教程:PHPUnit學習筆記(二)PHPUnit基本用法:http://be-evil.org/phpunit-study-note-basic-use.html

階段三,四在階段二文章的底部有推薦連結,請自行進入學習

PHPUnit可以實作測試方法的依賴關系

class ArrayTest extends PHPUnit_Framework_TestCase 繼承于PHPUnit_Framework_TestCase,進一步實作子產品測試

@depends:依賴關系通過注釋@depends來定義,如果某個測試方法依賴的方法測試沒有功過,那麼PHPUnit會自動跳過後面所有的依賴測試

@dataProvider: dataProvider标簽标注給它提供資料的方法名,定義之後PHPUnit會自動的将資料提供者方法傳回的資料依次傳入到測試方法中測試.

有個執行個體:和前一個例子實作的測試内容相同,但是資料提供者傳回的是一個疊代器對象而不是是數組(即是對象内的資料提供方法傳回一個new的新對象---即是這個類的對象)

檢查異常: PHPUnit中,我們有3種方式來檢查異常是否抛出

@expectedException: 方法一: 注釋法, 用@expectedException 标定期待的異常

$this->setExpectedException: 方法二: 設定法,使用 $this->setExpectedException 設定期待的異常

try catch + fail: 方法三: try catch + fail法

有時候我們的代碼在運作時會出現php錯誤,如整除0,檔案不存在等等

PHPUnit中,它會自動把錯誤轉換為異常PHPUnit_Framework_Error并抛出,我們隻需要在測試方法中設定抓取這個異常即可:

執行個體:

/**

* @expectedException PHPUnit_Framework_Error // 期待PHPUnit_Framework_Error的異常

*/

function test (){.....}

斷言(Assertions)是PHPUnit提供的一系列對程式執行結果測試的方法。通俗的講,就是斷言執行程式結果為我們期待的值

assertArrayHasKey(mixed $key, array $array[, string $message = ''])

斷言數組$array含有索引$key, $message用于自定義輸出的錯誤資訊,後同

-----------

16:12 2015/12/8

phpunit測試成功, phpunit測試實踐代碼

代碼寫在www目錄下,以類名命名代碼檔案,我的檔案名為ArrayTest.php,類名為ArrayTest,内部寫了簡單的測試代碼:

<?php

// require_once 'PHPUnit/Autoload.php';

// require_once 'ArrayTeller.class.php';

// require_once 'PHPUnit/Framework.php';

class ArrayTest extends PHPUnit_Framework_TestCase

{

public function testNewArrayIsEmpty()

{

// 建立數組fixture。

$fixture = array();

// 斷言數組fixture的尺寸是0。

$this->assertEquals(0, sizeof($fixture));

}

/**

*定義test标簽聲明該方法的測試方法

*@test

*/

public function indexEquals()

$stack = array(1, 2, 3);

$this->assertEquals(2, $stack[1]);

}

,然後

cmd的DOS視窗進入www目錄下,

執行phpunit ArrayTest.php,回車執行即可

終于成功了