单元测试 PHPUnit

 

<?php
/**
 * 定义一个用来被测试的类RemoteConnect
 * @author json
 *
 */
class RemoteConnect{
    public function connectServer($serverName = null){
        if($serverName == null){
            throw new Exception("This is not a server name!");
        }
        
        $fp = fsockopen($serverName,80);
        return $fp?true:false;
    }
    
    public function returnSampleObject(){
        return $this;
    }
}

/**
 * 定义一个用来测试RemoteConnect的类,
 * 但是要继承PHPUnit_Framework_TestCase。
 * @author json
 * 类名以Test结尾
 * $this->assertTrue:是单元测试的方法,有很多这样的方法。
 * 模块安装和用法看PHPUnit官网。
 */
class RemoteConnectTest extends PHPUnit_Framework_TestCase{
    public function setUp(){}
    public function tearDown(){}
    //test to ensure that from the object is valid.
    public function testConnectIsValid(){
        echo "jaja";
        try{
            $connObj = new RemoteConnect();
            $serverName = null;
            $this->assertTrue($connObj->connectServer($serverName) !== false);
        }catch (Exception $e){
            $e->getMessage();
        }
    }
    public function testAbc(){
        $this->assertTrue(3+2 == 5);
    }
    
}
?>

 

相关文章:

  • 2022-01-11
  • 2021-10-30
  • 2021-06-16
  • 2021-10-28
  • 2021-08-27
  • 2021-12-12
  • 2022-02-08
  • 2021-06-06
猜你喜欢
  • 2021-05-28
  • 2022-12-23
  • 2021-04-30
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案