search

PHPUnit笔记(一)

PHPUnit笔记(一)
  最近构想自己要做的项目,涉及到web上的东西,要用php来做,因此自然就接触到了phpunit这个东西。
 
  开发平台:Debian
 
  首先是安装PHPUnit,配置环境当然是越简单越好,我是个懒人,我认...

  PHPUnit安装
1 pear channel-discover pear.phpunit.de
2 pear channel-discover components.ez.no
3 pear channel-discover pear.symfony-project.com
4 pear install phpunit/PHPUnit

   结束后,会等一阵子,一开始我心急了,看见没啥反映,就直接ctrl+c了。事实证明我2了。
   安装需要pear,当然,获取方法也继续 my style。

   pear安装
1 apt-get install php5-curl php-pear 安装curl,pear
2 pear upgrade-all 更新软件包

  
   一切就绪后,可以在终端里输入phpunit,如果没有反映,就准备好好找原因吧。反正我相信linux上的软件问题,大多是依赖问题,以前尝试玩过lfs,好家伙,没让我少死脑细胞。
  


   接着是使用phpunit。
   根据baidu和google,大多使用如下:
  
<?php require_once 'PHPUnit/Framework.php';
 
   class ExceptionTest extends PHPUnit_Framework_TestCase
   {
      /**
      * @expectedException InvalidArgumentException
      */
      public function testException()
      {
      }
   }
?>

   然后在终端输入
  
1 phpuint test.php test.php为测试代码文件

   悲剧的是,找不到PHPUnit/Framework.php这个文件,于是查看php.ini的include_path参数后,找到了phpunit目录,发现了Framework目录。
  
<?php require_once 'PHPUnit/Framework/TestCase.php';

	class ArrayTest extends PHPUnit_Framework_TestCase{

		public function testNewArrayIsEmpty(){
			/*Create the Array fixture*/
			$fixture = array();
			/* Assert that the size of the Array * fixture is 0*/
			$this->assertEquals(0, sizeof($fixture));
		}

		public function testArrayContainsAnElement(){
			/* Create the Array fixture*/
			$fixture = array();
			/*Add an element to the Array * fixture*/
			$fixture[] = 'Element';
			/*Assert that the size of the * Array fixture is 1*/
			$this->assertEquals(1, sizeof($fixture));
		}
	}
?>

   这次ok了,基本上一个简单的testcase就是跑通了。

   当然,如果测试文件比较多,这么挨个phpunit,是相当蛋疼的事情。所以,我去找到了xml配置文件的使用方法。
  
<phpunit>
  <testsuites>
    <testsuite name="Object_Freezer_Directory">
      <directory>Tests</directory>
    </testsuite>
     <testsuite name="Object_Freezer_File">
     <file>Tests/Freezer/HashGenerator/NonRecursiveSHA1Test.php</file>
      <file>Tests/Freezer/IdGenerator/UUIDTest.php</file>
      <file>Tests/Freezer/UtilTest.php</file>
      <file>Tests/FreezerTest.php</file>
      <file>Tests/Freezer/StorageTest.php</file>
      <file>Tests/Freezer/Storage/CouchDB/WithLazyLoadTest.php</file>
      <file>Tests/Freezer/Storage/CouchDB/WithoutLazyLoadTest.php</file>
    </testsuite>
  </testsuites>
</phpunit>

   大概就是这样,可以去http://www.phpunit.de/manual/3.6/en/index.html了解更多的用法。
   终端命令
1 phpunit --configuration test.xml --debug test.xnl为测试配置文件


   ok,第一份笔记完毕。
   最后感慨下,phpunit真的是相当不错,想java用JUnit,要mock还需要Jmock,easymock;net也差不多,单元测试NUnit,mock需要Rhino Mocks;c就更要泪奔了。phpunit,不仅仅包含mock,连Code Coverage都有,不得不赞叹下啊。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use