PHPUnit 入門案例,phpunit入門案例_PHP教程
Jul 12, 2016 am 08:57 AMPHPUnit 入門案例,phpunit入門案例
了解PHPUnit
本案例是關(guān)于創(chuàng)建三角形的一個(gè)單元測(cè)試入門案例,在netbeans環(huán)境中完成,關(guān)于在此環(huán)境中搭建phpunit這里不再描述,可以參考以下資料完成搭建工作:
http://www.cnblogs.com/x3d/p/phpunit-in-netbeans8.html
https://phpunit.de/manual/current/zh_cn/installation.html
https://github.com/sebastianbergmann/phpunit-skeleton-generator
?
原代碼類:
?
<?php class Triangle { /** * 三條邊 第一條邊 * @var int */ protected $a; /** * 三條邊 第二條邊 * @var int */ protected $b; /** * 三條邊 第三條邊 * @var int */ protected $c; /** * 類型 * @var string */ protected $type; /** * 等邊 */ const TYPE_EQUILATERAL = 'Equilateral'; /** * 等腰 */ const TYPE_ISOSCELES = 'Isosceles'; /** * 普通 */ const TYPE_ORDINARY = 'Ordinary'; public function __construct($a = 0, $b = 0, $c = 0) { $this->initSide($a, $b, $c); } /** * 初始化三邊 * @param int $a * @param int $b * @param int $c */ protected function initSide(&$a = 0, &$b = 0, &$c = 0) { $this->a = intval($a); $this->b = intval($b); $this->c = intval($c); return $this; } /** * 組建 */ public function create($a, $b, $c) { return $this->initSide($a, $b, $c)->verifySideIsValid(); } /** * 獲取類型 */ public function getType() { return $this->verifyType()->type; } /** * 驗(yàn)證三邊是否有效 * @return boolean */ protected function verifySideIsValid() { if (intval($this->a) <= 0 || intval($this->b) <= 0 || intval($this->c) <= 0) { return false; } if ($this->a + $this->b <= $this->c) { return false; } if ($this->a + $this->c <= $this->b) { return false; } if ($this->b + $this->c <= $this->a) { return false; } if ($this->a - $this->b >= $this->c) { return false; } if ($this->a - $this->c >= $this->b) { return false; } if ($this->b - $this->c >= $this->a) { return false; } return true; } /** * 驗(yàn)證類型 */ protected function verifyType() { if ($this->isEquilateral()) { $this->type = self::TYPE_EQUILATERAL; return $this; } if ($this->isIsosceles()) { $this->type = self::TYPE_ISOSCELES; return $this; } $this->type = self::TYPE_ORDINARY; return $this; } /** * 是否為等邊三角形 */ protected function isEquilateral() { return (($this->a == $this->b ) && ($this->b == $this->c)) ? true : false; } /** * 是否為等腰三角形 */ protected function isIsosceles() { return (($this->a == $this->b ) || ($this->b == $this->c) || ($this->a == $this->c)) ? true : false; } }
生成的測(cè)試類文件:
<?php /** * Generated by PHPUnit_SkeletonGenerator on 2016-03-13 at 19:49:12. */ class TriangleTest extends PHPUnit_Framework_TestCase { /** * @var Triangle */ protected $object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new Triangle; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @dataProvider addDataProvider * @covers Triangle::create * @todo Implement testCreate(). */ public function testCreate($a, $b, $c) { // Remove the following lines when you implement this test. /** $this->markTestIncomplete( 'This test has not been implemented yet.' ); * */ /* 實(shí)現(xiàn)代碼 */ $this->assertTrue($this->object->create($a, $b, $c)); } /** * @covers Triangle::getType * @todo Implement testGetType(). */ public function testGetType() { // Remove the following lines when you implement this test. $this->markTestIncomplete( 'This test has not been implemented yet.' ); } /** * 測(cè)試用例 * @return array */ public function addDataProvider() { return [ [3, 4, 5], //yes [2, 2, 2], //yes [8, 10, 8], //yes [2, 3, 4], //yes [1, 2, 3], //no [5, 6, 7], //yes [8, 8, 15], //yes [0, 0, 0], //no [-10, 2, 5], //no [0, 2, 1], //no ]; } }
這里需要注意,在我們執(zhí)行“創(chuàng)建/更新測(cè)試”后生成的測(cè)試文件類與上面會(huì)有些不同,這里的測(cè)試用例是手動(dòng)加上去的,這里具體實(shí)現(xiàn)可以查看手冊(cè)里的說(shuō)明!
附執(zhí)行結(jié)果:
"/usr/bin/php" "/usr/local/bin/phpunit" "--colors" "--log-junit" "/tmp/nb-phpunit-log.xml" "--bootstrap" "/var/www/html/phpunit/test/bootstrap.php" "/usr/local/netbeans-8.1/php/phpunit/NetBeansSuite.php" "--" "--run=/var/www/html/phpunit/test/core/triangleTest.php" PHPUnit 5.2.10 by Sebastian Bergmann and contributors. ....F..FFFI 11 / 11 (100%) Time: 105 ms, Memory: 10.50Mb There were 4 failures: 1) TriangleTest::testCreate with data set #4 (1, 2, 3) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 2) TriangleTest::testCreate with data set #7 (0, 0, 0) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 3) TriangleTest::testCreate with data set #8 (-10, 2, 5) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 4) TriangleTest::testCreate with data set #9 (0, 2, 1) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 FAILURES! Tests: 11, Assertions: 10, Failures: 4, Incomplete: 1. 完成。
?

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)

在現(xiàn)代化的網(wǎng)路開發(fā)中,Vue作為一款靈活、易上手且功能強(qiáng)大的前端框架,被廣泛應(yīng)用於各種網(wǎng)站和應(yīng)用程式的開發(fā)中。在開發(fā)大型專案時(shí),如何簡(jiǎn)化程式碼的複雜度,讓專案更容易維護(hù),是每個(gè)開發(fā)者都必須面對(duì)的問(wèn)題。而模組化開發(fā),可以幫助我們更好地組織程式碼,提高開發(fā)效率和程式碼可讀性。下面,我將分享一些在Vue大型專案中實(shí)現(xiàn)模組化開發(fā)的經(jīng)驗(yàn)和指南:1.分工明確在一個(gè)大型專案中

如何進(jìn)行Java開發(fā)專案的前端與後端分離隨著網(wǎng)路的快速發(fā)展,以及使用者對(duì)於Web應(yīng)用體驗(yàn)的不斷提升,前端與後端分離的開發(fā)模式逐漸成為了主流。在Java開發(fā)專案中,更是有著廣泛的應(yīng)用。那麼,如何進(jìn)行Java開發(fā)專案的前端與後端分離呢?本文將從概念解釋、開發(fā)流程、技術(shù)選型以及優(yōu)勢(shì)與挑戰(zhàn)等面向進(jìn)行闡述。一、概念解釋前端與後端分離是一種開發(fā)方式,它將使用者介面與業(yè)務(wù)邏

近年來(lái),前端技術(shù)發(fā)展迅速,其中Vue.js作為一款優(yōu)秀的前端架構(gòu)備受關(guān)注。而隨著Vue.js3的正式發(fā)布以及Django4的即將到來(lái),結(jié)合兩者開發(fā)全新的技術(shù)專案無(wú)疑是一個(gè)能夠突破技術(shù)瓶頸、提升專案開發(fā)效率的好方案。本文將分享一些實(shí)用技巧,幫助開發(fā)者在Vue3+Django4的技術(shù)專案開發(fā)過(guò)程中更加得心應(yīng)手。首先,要開始一個(gè)全新的Vue3+Django4項(xiàng)目,

Golang模板程式設(shè)計(jì)實(shí)踐:在Golang中有效地利用模板完成專案開發(fā),需要具體程式碼範(fàn)例摘要:隨著Golang在軟體開發(fā)領(lǐng)域的不斷發(fā)展,越來(lái)越多的開發(fā)者開始關(guān)注並使用Golang進(jìn)行專案開發(fā)。在Golang中,模板程式設(shè)計(jì)是一個(gè)非常重要的技術(shù),能夠幫助開發(fā)者有效率地完成專案開發(fā)。本文將介紹如何在Golang中利用範(fàn)本進(jìn)行專案開發(fā),並提供具體的程式碼範(fàn)例。引言:Gola

掌握核心技術(shù):Go語(yǔ)言專案開發(fā)經(jīng)驗(yàn)總結(jié)近年來(lái),隨著網(wǎng)路產(chǎn)業(yè)的快速發(fā)展,各種新的程式語(yǔ)言也紛紛出現(xiàn),並成為開發(fā)者們的新寵。其中,Go語(yǔ)言作為一門開源的靜態(tài)編譯型語(yǔ)言,以其並發(fā)性能好、執(zhí)行效率高等優(yōu)勢(shì),備受眾多開發(fā)者的喜愛(ài)。作為一個(gè)Go語(yǔ)言開發(fā)者,我在多個(gè)專案中進(jìn)行了實(shí)踐,並累積了一些經(jīng)驗(yàn)和總結(jié)。在本文中,我將分享一些關(guān)於Go語(yǔ)言專案開發(fā)的核心技術(shù)和經(jīng)驗(yàn),希望對(duì)

踩過(guò)的坑:Go語(yǔ)言專案開發(fā)經(jīng)驗(yàn)與教訓(xùn)在軟體開發(fā)的道路上,每個(gè)開發(fā)者都會(huì)不可避免地踩過(guò)一些坑。當(dāng)然,對(duì)於Go語(yǔ)言的開發(fā)者來(lái)說(shuō)也不例外。本文將分享我在使用Go語(yǔ)言進(jìn)行專案開發(fā)過(guò)程中所踩過(guò)的坑,希望能為其他開發(fā)者帶來(lái)一些經(jīng)驗(yàn)和教訓(xùn)。不同版本的Go語(yǔ)言在使用Go語(yǔ)言進(jìn)行專案開發(fā)時(shí),我們必須專注於Go語(yǔ)言的版本。不同版本之間可能存在一些語(yǔ)言上的差異或API的變動(dòng),這些

作為一種相對(duì)新興的程式語(yǔ)言,Go語(yǔ)言在近年來(lái)的發(fā)展中受到了越來(lái)越多的關(guān)注。特別是在專案開發(fā)方面,Go語(yǔ)言有著優(yōu)勢(shì),因?yàn)樗绕渌Z(yǔ)言更適合開發(fā)高效能、並發(fā)、分散式的系統(tǒng)。但是,即使使用Go語(yǔ)言,也會(huì)遇到專案開發(fā)中的瓶頸和挑戰(zhàn),因此在本文中,我們將分享一些經(jīng)驗(yàn),以幫助突破這些瓶頸。一、學(xué)習(xí)與掌握Go語(yǔ)言:在開始專案開發(fā)之前,必須先掌握Go語(yǔ)言的基礎(chǔ)知識(shí)與程式設(shè)計(jì)技

Go語(yǔ)言作為一種高效能、簡(jiǎn)潔易用的程式語(yǔ)言,越來(lái)越多的開發(fā)者開始選擇它作為專案開發(fā)的首選語(yǔ)言。然而,在實(shí)際的專案開發(fā)過(guò)程中,我們也會(huì)遇到一些常見(jiàn)的問(wèn)題。本文將介紹一些這樣的問(wèn)題,並提供相應(yīng)的解決方法,幫助開發(fā)者更好地應(yīng)對(duì)這些挑戰(zhàn)。問(wèn)題一:依賴管理在Go語(yǔ)言的專案開發(fā)中,依賴管理是常見(jiàn)的問(wèn)題。由於Go語(yǔ)言的模組化特性,專案往往依賴許多第三方套件和函式庫(kù)。而如
