PHPUnit 入門案例,phpunit入門案例
Jun 13, 2016 am 08:44 AMPHPUnit 入門案例,phpunit入門案例
了解PHPUnit
本案例是關于創(chuàng)建三角形的一個單元測試入門案例,在netbeans環(huá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; } /** * 驗證三邊是否有效 * @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; } /** * 驗證類型 */ 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; } }
生成的測試類文件:
<?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.' ); * */ /* 實現(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.' ); } /** * 測試用例 * @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)建/更新測試”后生成的測試文件類與上面會有些不同,這里的測試用例是手動加上去的,這里具體實現(xiàn)可以查看手冊里的說明!
附執(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
免費脫衣圖片

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

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

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

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

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

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

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

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

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

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

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

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

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

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