• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    PHP测试框架PHPUnit组织测试操作示例

    本文实例讲述了PHP测试框架PHPUnit组织测试操作。分享给大家供大家参考,具体如下:

    首先是目录结构

    源文件夹为 src/
    测试文件夹为 tests/

    User.php

    ?php
    class Errorcode
    {
      const NAME_IS_NULL = 0;
    }
    class User
    {
      public $name;
      public function __construct($name)
      {
        $this->name=$name;
      }
      public function Isempty()
      {
        try{
          if(empty($this->name))
          {
            throw new Exception('its null',Errorcode::NAME_IS_NULL);
          }
        }catch(Exception $e){
          return $e->getMessage();
        }
        return 'welcome '.$this->name;
      }
    }
    
    

    对应的单元测试文件  UserTest.php

    ?php
    use PHPUnit\Framework\TestCase;
    class UserTest extends TestCase
    {
      protected $user;
      public function setUp()
      {
        $this->user = new User('');
      }
      public function testIsempty()
      {
        $this->user->name='mark';
        $result =$this->user->Isempty();
        $this->assertEquals('welcome mark',$result);
        $this->user->name='';
        $results =$this->user->Isempty();
        $this->assertEquals('its null',$results);
      }
    }
    
    

    第二个单元测试代码因为要引入 要测试的类  这里可以用 自动载入 避免文件多的话 太多include

    所以在src/ 文件夹里写 autoload.php

    ?php
    function __autoload($class){
      include $class.'.php';
    }
    spl_autoload_register('__autoload');
    
    

    当需要User类时,就去include User.php。写完__autoload()函数之后要用spl_autoload_register()注册上。

    虽然可以自动载入,但是要执行的命令变得更长了。

    打开cmd命令如下

    phpunit --bootstrap src/autoload.php tests/UserTest
    
    

    所以我们还可以在根目录写一个配置文件phpunit.xml来为项目指定bootstrap,这样就不用每次都写在命令里了。

    phpunit.xml

    phpunit bootstrap="src/autoload.php">
    /phpunit>
    
    

    然后

    打开cmd命令 执行MoneyTest 命令如下

    phpunit tests/UserTest
    
    

    打开cmd命令 执行tests下面所有的文件 命令如下

    phpunit tests
    
    

    更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP错误与异常处理方法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》及《php优秀开发框架总结》

    希望本文所述对大家PHP程序设计有所帮助。

    您可能感兴趣的文章:
    • Windows下安装PHP单元测试环境PHPUnit图文教程
    • PHP单元测试利器 PHPUNIT深入用法(三)
    • PHP单元测试利器 PHPUNIT初探
    • PHP单元测试利器 PHPUNIT深入用法(二)
    • php单元测试phpunit入门实例教程
    • PHP单元测试PHPUnit简单用法示例
    • PHPUnit PHP测试框架安装方法
    • 使用phpunit进行接口自动化测试
    • 详解Yaf框架PHPUnit集成测试方法
    • PHPUnit测试私有属性和方法功能示例
    • PHP单元测试框架PHPUnit用法详解
    上一篇:php empty 函数判断结果为空但实际值却为非空的原因解析
    下一篇:Windows下wamp php单元测试工具PHPUnit安装及生成日志文件配置方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    PHP测试框架PHPUnit组织测试操作示例 PHP,测试,框架,PHPUnit,组织,