国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院

首頁 > 編程 > PHP > 正文

對于PHPUnit測試私有屬性和方法的功能分析

2020-03-22 20:31:51
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHPUnit測試私有屬性和方法功能,結合實例形式較為詳細的分析了使用PHPUnit針對私有屬性和方法進行測試的相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了PHPUnit測試私有屬性和方法功能。分享給大家供大家參考,具體如下:

一、測試類中的私有方法:

html' target='_blank'>class Sample private $a = 0; private function run() echo $a;}

上面只是簡單的寫了一個類包含,一個私有變量和一個私有方法。對于protected和private方法,由于無法像是用public方法一樣直接調用,所以在使用phpunit進行單測的時候,多有不便,特別是當一個類中,對外只提供少量接口,內部使用了大量private方法的情況。

對于protected方法,建議使用繼承的方式進行測試,在此就不再贅述。而對于private方法的測試,建議使用php的反射機制來進行。話不多說,上代碼:

class testSample() $method = new ReflectionMethod( Sample , run  $method- setAccessible(true); //將run方法從private變成類似于public的權限 $method- invoke(new Sample()); //調用run方法}

如果run方法是靜態的,如:

private static function run() echo run is a private static function }

那么invoke函數還可以這么寫:

$method- invoke(null); //只有靜態方法可以不必傳類的實例化

如果run還需要傳參,比如:

private function run($x, $y) return $x + $y;}

那么,測試代碼可以改為:

$method- invokeArgs(new Sample(), array(1, 2));//array中依次寫入要傳的參數。執行結果返回3

【注意】:利用反射的方法測試私有方法雖好,但setAccessible函數是php5.3.2版本以后才支持的( =5.3.2)

二、私有屬性的get/set

說完了私有方法,再來看看私有屬性,依舊拿Sample類作為例子,想要獲取或設置Sample類中的私有屬性$a的值可以用如下方法:

public function testPrivateProperty() $reflectedClass = new ReflectionClass( Sample  $reflectedProperty = $reflectedClass- getProperty( a  $reflectedProperty- setAccessible(true); $reflectedProperty- getValue(); //獲取$a的值 $reflectedProperty- setValue(123); //給$a賦值:$a = 123;}

上述方法對靜態屬性依然有效。

到此,是不是瞬間感覺測試私有方法或屬性變得很容易了。

附:PHPunit 測試私有方法(英文原文)

This article is part of a series on testing untestable code:

Testing private methods

Testing code that uses singletons

Stubbing static methods

Stubbing hard-coded dependencies

No, not those privates. If you need help with those, this book might help.

One question I get over and over again when talking about Unit Testing is this:

How do I test the private attributes and methods of my objects?

Lets assume we have a class Foo:

 ?phpclass Foo private $bar = baz  public function doSomething() return $this- bar = $this- doSomethingPrivate(); private function doSomethingPrivate() return blah ? 

Before we explore how protected and private attributes and methods can be tested directly, lets have a look at how they can be tested indirectly.

The following test calls the testDoSomething() method which in turn calls thedoSomethingPrivate() method:

 ?phpclass FooTest extends PHPUnit_Framework_TestCase * @covers Foo::doSomething * @covers Foo::doSomethingPrivate public function testDoSomething() $foo = new Foo; $this- assertEquals( blah , $foo- doSomething());? 

The test above assumes that testDoSomething() only works correctly whentestDoSomethingPrivate() works correctly. This means that we have indirectly testedtestDoSomethingPrivate(). The problem with this approach is that when the test fails we do not know directly where the root cause for the failure is. It could be in eithertestDoSomething() or testDoSomethingPrivate(). This makes the test less valuable.

PHPUnit supports reading protected and private attributes through thePHPUnit_Framework_Assert::readAttribute() method. Convenience wrappers such asPHPUnit_Framework_TestCase::assertAttributeEquals() exist to express assertions onprotected and private attributes:

 ?phpclass FooTest extends PHPUnit_Framework_TestCase public function testPrivateAttribute() $this- assertAttributeEquals( baz , /* expected value */ bar , /* attribute name */ new Foo /* object */? 

PHP 5.3.2 introduces the ReflectionMethod::setAccessible() method to allow the invocation of protected and private methods through the Reflection API:

 ?phpclass FooTest extends PHPUnit_Framework_TestCase * @covers Foo::doSomethingPrivate public function testPrivateMethod() $method = new ReflectionMethod( Foo , doSomethingPrivate  $method- setAccessible(TRUE); $this- assertEquals( blah , $method- invoke(new Foo)? 

In the test above we directly test testDoSomethingPrivate(). When it fails we immediately know where to look for the root cause.

I agree with Dave Thomas and Andy Hunt, who write in their book Pragmatic Unit Testing :

In general, you don t want to break any encapsulation for the sake of testing (or as Mom used to say, don t expose your privates! ). Most of the time, you should be able to test a class by exercising its public methods. If there is significant functionality that is hidden behind private or protected access, that might be a warning sign that there s another class in there struggling to get out.

So: Just because the testing of protected and private attributes and methods is possible does not mean that this is a good thing .

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !

相關推薦:

PHP和redis實現悲觀鎖機制的解析

php中可變函數的使用總結

以上就是對于PHPUnit測試私有屬性和方法的功能分析的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院
四虎在线免费视频| 999精品网| 九九热视频精品在线观看| av大片在线| 免费看的毛片| 国产色在线观看| 日本不卡1区2区3区| 国产一区二区三区福利| 精品久久亚洲一级α| 日本在线天堂| 国产叼嘿网站免费观看不用充会员| 国产激情在线| 九九热在线播放| 中文字幕av网| 日本中文字幕在线播放| 人人九九精品| 精品孕妇一区二区三区| 69精品视频| 日本中文字幕在线2020| 亚洲欧美小说国产图片| 国产午夜在线观看| 福利在线国产| 免费精品国产自产拍在| 在线视频三级| 91欧美在线视频| 中文字幕中文字幕在线中高清免费版| 亚洲色婷婷综合开心网| 国产国产国产国产国产国产| 最近中文av字幕在线中文| 久久国产综合视频| 性网站在线看| 国产精品久久久精品a级小说| 开心激情五月婷婷| 三级小说一区| gogo在线高清视频| av日韩在线免费| 伊人影院在线播放| 国产不卡视频| 精精国产xxxx视频在线中文版| 中文字幕免费在线视频| 国产有码在线| 国产香蕉免费精品视频| 国产香蕉尹人视频在线| 午夜视频在线免费| 国产精品一区二区三区高清在线| 欧美aaa一级片| 欧美日韩视频精品一区二区| www亚洲天堂| www在线视频| 狠狠狠综合7777久夜色撩人| 精品资源在线看| 丁香花高清在线观看完整版| 国产美女在线免费观看| 国产在线一二三区| 国产精品久久久久久久久鸭| 国产精品666| av三级在线观看| 女同一区二区免费aⅴ| 懂色av一区| 国产人成在线观看| 精品视频vs精品视频| 精品一区二区在线欧美| www.成人.com| 国产区成人精品视频| 伊人免费在线| 二人午夜免费观看在线视频| 国产女主播在线观看| 亚洲欧美一区二区三区在线播放| 免费在线高清av| 国产女主播在线| 麻豆国产视频| 精品美女调教视频| 成视频年人免费看黄网站| 999在线视频| 五月亚洲综合| 免费国产在线视频| 国产网站av| 日日夜夜中文字幕| 国产偷窥老熟盗摄视频| 青青草在线免费观看| 国产porny蝌蚪视频| 国内精品不卡| 久热国产在线视频| 亚洲欧美日韩成人网| 国产青青草在线| 国产伦精品一区二区三区高清版禁 | 国产黄色av免费看| 久久99国产视频| 中文在线官网天堂| 国产porny蝌蚪视频| 国产麻豆高清视频在线第一页| av在线你懂的| av在线播放网| 亚洲欧美日韩一区成人| 91麻豆精品国产91久久| 国产区卡一卡二卡三乱码免费| 国产精品臀控福利在线观看| 日本不卡视频一区二区| 日本精品一区二区三区在线播放| 精精国产xxxx视频在线中文版| 国产欧美日韩专区| 国产深夜福利| 国产人成在线视频| 91蜜桃在线视频| 国产精品爱久久久久久久小说| 国产视频三区| 中文字幕av免费| av在线网页| 麻豆网站在线| 成人无遮挡免费网站视频在线观看| 麻豆国产在线播放| 国产美女在线看| 日本中文字幕视频| 国产叼嘿网站免费观看不用充会员| 国产传媒在线播放| 日本在线视频www鲁啊鲁| 亚洲色婷婷综合开心网| 欧美亚洲另类在线观看| 国产在线一区二区视频| 国产精品剧情一区二区在线观看| 2018中文字幕在线观看| 亚洲成人av高清| 国产美女一区视频| 中文一区在线观看| 国产在线观看a视频| 国产探花视频在线观看| 亚洲精品久久久成人| 黄色网页网址在线免费| 国产三级在线看| 免费精品国产自产拍观看| 国产精品免费视频二三区| 一区二区精品区| 天堂在线免费av| 在线视频三级| 国产丝袜视频在线播放| 伊人中文在线| 国产精选一区二区三区不卡催乳| 精品国产白色丝袜高跟鞋| 国产鲁鲁视频在线观看免费| 在线观看av的网站| 国产亚洲精品久久久久久移动网络| 在线播放黄色网址| 国产人成高清视频观看| 国产男女av| 国产在线一区二区视频| 日本一卡二卡四卡精品| 国产成人综合亚洲欧美在| 国产在线日本| 免费视频中文字幕| 在线播放一区二区精品产| 国产黄a三级三级三级av在线看| 福利视频网址导航| 黄色在线视频观看网站| 牛牛精品视频在线| gogo高清在线播放免费| 中文字幕av网| 永久免费在线观看| 亚洲综合在线网| 亚洲成人电视网| 日本视频一二三区中文字幕| av在线1区2区| 亚洲国产aⅴ精品| 精品一区二区三区免费站| 国产深夜视频在线观看| 国产经典av| 在线一二三区| 国产在线一区二区视频| 国产毛片毛片毛片| 国产激情在线视频| 在线播放av网站| 欧美卡一卡二| **三级三级97片毛片| 国产视频福利在线| 精品福利影院| 天天操夜夜添| 国产九色视频| 国产成在线观看免费视频| 国产黄色一级电影| sese在线视频| 国产三级视频在线看| 2021天堂中文幕一二区在线观| 一本大道久久a久久精品| 久久99精品久久久久久野外| 国产深夜福利| 亚洲视频精品在线观看| 麻豆网站在线| 2020国产在线视频| www中文字幕在线观看| 国产精品一区在线看| 精品一区二区三区在线成人| sese一区| 在线视频色在线| 欧美精品一区二区三区免费| 国产一二区在线观看| 亚洲成av人影片在线观看| 最近中文字幕mv免费高清电影| 91啦中文在线| 午夜视频在线观看网站午夜视频在线| 在线免费观看污| 国产精品久久麻豆|