PHPUnit 入门案例,phpunit入门案例
PHPUnit 入门案例,phpunit入门案例
了解PHPUnit
本案例是关于创建三角形的一个单元测试入门案例,在netbeans环境中完成,关于在此环境中搭建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.' ); * */ /* 实现代码 */ $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 ]; } }
这里需要注意,在我们执行“创建/更新测试”后生成的测试文件类与上面会有些不同,这里的测试用例是手动加上去的,这里具体实现可以查看手册里的说明!
附执行结果:
"/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工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

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

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

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

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

掌握核心技術:Go語言專案開發經驗總結近年來,隨著網路產業的快速發展,各種新的程式語言也紛紛出現,並成為開發者們的新寵。其中,Go語言作為一門開源的靜態編譯型語言,以其並發性能好、執行效率高等優勢,備受眾多開發者的喜愛。作為一個Go語言開發者,我在多個專案中進行了實踐,並累積了一些經驗和總結。在本文中,我將分享一些關於Go語言專案開發的核心技術和經驗,希望對

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

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

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