http://pear.phpunit.de/ phpunit官方站点
https://github.com/sebastianbergmann/phpunit/ phpunit Github地址
phpunit安装命令:
pear channel-discover pear.phpunit.de
pear install -o phpunit/phpunit
运行上门的命令安装phpunit,当然必须是你的pear安装良好的情况下
如果安装的时候遇到pear/Mail_Mime requires PEAR Installer (version >= 1.9.4), installed version is 1.9.0类似的错误
就将pear升级下再安装
pear升级
pear upgrade --force PEAR
安装成功后如果在cmd命令运行phpunit 提示 ".php.exe"' 不是内部或外部命令,也不是可运行的程序或批处理文件。”
修改php安装目录的phpunit.bat,将 PHPBIN=.php.exe 改为你自己的php目录PHPBIN=D:wampphpphp.exe
然后关闭cmd再打开应该可以了
现在创建第一个测试用例
创建下面两个类
vendorWIGoodsGoodsAbstract.php
vendorWIGoodsBaoGoods.php
vendorWILoaderSplClassLoader.php
代码分别为:
GoodsAbstract.php
<?php namespace WIGoods; abstract class GoodsAbstract{ private $good_name; public function SetGoods($goods_name) { $this->goods_name=$goods_name; } public function GetGoods() { return $this->goods_name; } }
BaoGoods.php
<?php namespace WIGoods; class BaoGoods extends GoodsAbstract{ }
SplClassLoader.php (类库的自动加载器)
<?php /** * SplClassLoader implementation that implements the technical interoperability * standards for PHP 5.3 namespaces and class names. * * http://groups.google.com/group/php-standards/web/final-proposal * * // Example which loads classes for the Doctrine Common package in the * // DoctrineCommon namespace. * $classLoader = new SplClassLoader('DoctrineCommon', '/path/to/doctrine'); * $classLoader->register(); * * @author Jonathan H. Wage <jonwage@gmail.com> * @author Roman S. Borschel <roman@code-factory.org> * @author Matthew Weier O'Phinney <matthew@zend.com> * @author Kris Wallsmith <kris.wallsmith@gmail.com> * @author Fabien Potencier <fabien.potencier@symfony-project.org> */ class SplClassLoader { private $_fileExtension = '.php'; private $_namespace; private $_includePath; private $_namespaceSeparator = '\'; /** * Creates a new <tt>SplClassLoader</tt> that loads classes of the * specified namespace. * * @param string $ns The namespace to use. */ public function __construct($ns = null, $includePath = null) { $this->_namespace = $ns; $this->_includePath = $includePath; } /** * Sets the namespace separator used by classes in the namespace of this class loader. * * @param string $sep The separator to use. */ public function setNamespaceSeparator($sep) { $this->_namespaceSeparator = $sep; } /** * Gets the namespace seperator used by classes in the namespace of this class loader. * * @return void */ public function getNamespaceSeparator() { return $this->_namespaceSeparator; } /** * Sets the base include path for all class files in the namespace of this class loader. * * @param string $includePath */ public function setIncludePath($includePath) { $this->_includePath = $includePath; } /** * Gets the base include path for all class files in the namespace of this class loader. * * @return string $includePath */ public function getIncludePath() { return $this->_includePath; } /** * Sets the file extension of class files in the namespace of this class loader. * * @param string $fileExtension */ public function setFileExtension($fileExtension) { $this->_fileExtension = $fileExtension; } /** * Gets the file extension of class files in the namespace of this class loader. * * @return string $fileExtension */ public function getFileExtension() { return $this->_fileExtension; } /** * Installs this class loader on the SPL autoload stack. */ public function register() { spl_autoload_register(array($this, 'loadClass')); } /** * Uninstalls this class loader from the SPL autoloader stack. */ public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } /** * Loads the given class or interface. * * @param string $className The name of the class to load. * @return void */ public function loadClass($className) { if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { $fileName = ''; $namespace = ''; if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; } } }
只是简单的一个添加商品和显示商品功能,BaoGoods继承GoodsAbstract,主要是为了按常用的类组织模式来测试
然后在vendor目录下新建目录WItest
新建文件如下,这样可以将不同组件
vendorWItestGoodBaoGoodsTest.php
vendorWItestautoload_factory.php
autoload_factory.php
<?php include "WI/Loader/SplClassLoader.php"; $autoloader=new SplClassLoader("WI",""); $autoloader->register();
BaoGoodsTest.php
<?php require_once 'WItest/autoload_factory.php'; require_once 'PHPUnit/Framework/TestCase.php'; use WIGoodsBaoGoods; class BaoGoodsTest extends PHPUnit_Framework_TestCase{ private $Goods_obj;
//用于初始化测试所需的数据,在每个方法运行前被调用 public function setUp() { $this->Goods_obj=new BaoGoods(); }
//在每个方法运行后被调用 public function tearDown() { } public function testAddGoods() { $Goods_name="双肩包"; $this->Goods_obj->SetGoods($Goods_name); $this->assertEquals($this->Goods_obj->GetGoods(),"双肩包1"); } }
然后在cmd里进入vendor目录运行 phpunit witest
会得出如下提示:
PHPUnit 3.7.22 by Sebastian Bergmann. F Time: 0 seconds, Memory: 3.00Mb There was 1 failure: 1) BaoGoodsTest::testAddGoods Failed asserting that two strings are equal. --- Expected (前面为-符号的为期望的结果) +++ Actual (前面为+符号的是实际的结果) @@ @@ -'鍙岃偐鍖? (貌似命令行下不支持中文) +'鍙岃偐鍖?' D:wwwmytoolsvendorWItestGoodBaoGoodsTest.php:24 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
对于phpunit命令行下中文显示为乱码的问题,可以运行 phpunit witest>c:witest.txt 后查看witest.txt即可正常显示中文
也可以在vendor目录下新建witest.bat,内容为:phpunit witest>witest.txt
这样双击下就可以查看测试结果了