PHPUnit札记(一)

Jun 13, 2016 pm 01:14 PM
file gt nbsp phpunit

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-&gt;assertEquals(1, sizeof($fixture));
		}
	}
?&gt;
로그인 후 복사

   这次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都有,不得不赞叹下啊。
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

해결 방법: 조직에서 PIN 변경을 요구합니다. 해결 방법: 조직에서 PIN 변경을 요구합니다. Oct 04, 2023 pm 05:45 PM

해결 방법: 조직에서 PIN 변경을 요구합니다.

Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경 Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경 Sep 22, 2023 am 11:37 AM

Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경

Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까? Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까? Sep 14, 2023 pm 03:33 PM

Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까?

Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법 Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법 Sep 15, 2023 pm 03:57 PM

Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법

Windows 11/10 복구의 OOBELANGUAGE 오류 문제 Windows 11/10 복구의 OOBELANGUAGE 오류 문제 Jul 16, 2023 pm 03:29 PM

Windows 11/10 복구의 OOBELANGUAGE 오류 문제

화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? 화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? Dec 29, 2023 pm 02:27 PM

화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까?

Windows 11의 디스플레이 크기 조정 가이드 Windows 11의 디스플레이 크기 조정 가이드 Sep 19, 2023 pm 06:45 PM

Windows 11의 디스플레이 크기 조정 가이드

Windows 11에서 밝기를 조정하는 10가지 방법 Windows 11에서 밝기를 조정하는 10가지 방법 Dec 18, 2023 pm 02:21 PM

Windows 11에서 밝기를 조정하는 10가지 방법

See all articles