背景:一個小腳本,保證穩定為主;所以試用了下phpunit,快捷方便
phpunit 的安裝
phpunit是一個輕量級的php單元測試框架,通過pear安裝
安裝過程
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version
成功之后顯示如下:
cuihuan:~ cuixiaohuan$ phpunit --version
PHPUnit 4.8.6 by Sebastian Bergmann and contributors.
簡單試用
測試類集成框架
class PsCaseTest extends PHPUnit_Framework_TestCase{}
其中phpunit
默認首先執行 setup
默認最后執行 teardown
舉個栗子:
<?php
/***************************************************************************** $Id: PsCaseTest,v 1.0 PsCaseTest cuihuan Exp$***************************************************************************//*** @file PsCaseTest.php* @author cuihuan* @date 2015/09/11 10:09:31* @version $Revision:1.0$* @brief pscase接口單元測試***/require_once dirname(__FILE__) . ('/PsCase.php');class PsCaseTest extends PHPUnit_Framework_TestCase
{/*** @var object pscase類*/protected $pscase;/*** @brief setup: Sets up the fixture, for example, opens a network connection.** This method is called before a test is executed. */public function setup(){$this->pscase = new PsCase();}/*** @brief teardown: Tears down the fixture, for example, closes a network connection.* * This method is called after a test is executed.*/public function teardown(){}/*** @brief : 測試config文件的獲取**/public function testGetConfig(){$this->assertEquals(true,$this->pscase->debugText("11"));}
}
運行
運行方式:phpunit —bootstrap [源文件] 測試文件
具體如下:
cuihuande:newcode cuixiaohuan$ phpunit --bootstrap ./PsCase.php ./PsCaseTest.php32015-09-11 02:09:36:11<br>
5Time: 116 ms, Memory: 11.75MbOK (1 test, 1 assertion) 【表示運行成功】
部署
部署就不不贅述了,寫個shell腳本,crontab天極運行,加個報警郵件,簡單的單元測試ok,從此再也不用擔心錯誤和回歸測試了。
個人小站原文鏈接