【问题标题】:need some advice on what to test PHPUnit需要一些关于测试 PHPUnit 的建议
【发布时间】:2014-05-11 17:49:59
【问题描述】:

在为我的代码编写测试方面,我是一个初学者。我希望有人能给我一些好的建议。

这是我要测试的课程。

class Http_Client {

/**
 * @var string
 */
private $uri;

/**
 * @var string
 */
private $username;

/**
 * @var string
 */
private $password;

/**
 * @var Http_Adapter_Interface
 */
private $httpAdapter = null;

/**
 * Default Constructor
 * @param string
 * @param string
 */
public function __construct($username, $password) {
    $this->setUsername( $username );
    $this->setPassword( $password );
}

/**
 * @param string $username
 * @throws Exception
 */
public function setUsername( $username ) {

    if( is_string( $username ) )
    {
        $this->username = $username;
    }
    else
    {
        throw new Exception( 'Username must be of type String!' );
    }
}

/**
 * @param string $password
 * @throws Exception
 */
public function setPassword( $password ) {

    if( is_string( $password ) )
    {
        $this->password = $password;
    }
    else
    {
        throw new Exception( 'Password must be of type String!' );
    }
}

/**
 * @return string
 */
private function _getUsername()
{
    return $this->username;
}

/**
 * @return string
 */
private function _getPassword()
{
    return $this->password;
}

/**
 * @param Http_Adapter_Interface $httpAdapter
 * @throws Exception
 */
public function setHttpAdapter( Http_Adapter_Interface $httpAdapter)
{
    if( is_null( $this->httpAdapter) )
    {
        $this->httpAdapter = $httpAdapter;
    }
    else
    {
        throw new Exception( 'Http Adapter already set!' );
    }
}

/**
 * @return Http_Adapter_Interface
 */
private function _getHttpAdapter()
{
    return $this->httpAdapter;
}

/**
 * @param string
 * @throws Exception
 * @return  void
 */
public function setURI($uri) {

    if( is_string( $uri ) )
    {
        $this->uri = $uri;
    }
    else
    {
        throw new Exception( 'Uri must be of type String!' );
    }

}

/**
 * @return string
 */
private function _getURI()
{
    return $this->uri;
}

/**
 * @param  string $method
 * @param array $data
 * @throws Exception
 * @return Http_Response
 */
public function request($method, $data = array() ) {


    if( !is_string( $method ) )
    {
        throw new Exception( '$method must be of type String!' );
    }

    if( !is_array( $data ) )
    {
        throw new Exception( '$data must be of type Array!' );
    }

    $httpAdapter = $this->_getHttpAdapter();
    $username = $this->_getUsername();
    $password = $this->_getPassword();
    $uri = $this->_getURI();


    if($method == 'GET')
    {
        return $httpAdapter->get( $username, $password, $uri );
    }

    if($method == 'POST')
    {
        return $httpAdapter->post( $username, $password, $uri, $data );
    }
}


}

这是我目前的测试方法

public function testInstance()
{
    $this->assertInstanceOf('Http_Client', $this->Http_Client);
}


/**
 * @expectedException Exception
 */
public function testSetUsernameThrowsExceptionIfParamIsNotString() {
    $this->Http_Client->setUsername( 1 );
}

//test setPassword
/**
 * @expectedException Exception
 */
public function testSetPasswordThrowsExceptionIfParamIsNotString() {
    $this->Http_Client->setPassword( 1 );
}


/**
 * @expectedException Exception
 */
public function testSetHttpAdapterThrowsExceptionIfHttpAdapterIsAlreadySet()
{
    $httpAdapter = new Http_AdapterTest();
    $this->Http_Client->setHttpAdapter( $httpAdapter );

}


/**
 * @expectedException Exception
 */
public function testSetURIThrowsExceptionIfParamIsNotString() {
    $this->Http_Client->setURI( 1 );
}


//test request

/**
 * @expectedException Exception
 */
public function testRequestThrowsExceptionIfMethodParamIsNotString()
{
    $this->Http_Client->request( 1 );
}


public function testRequestReturnHttp_responseObject() {

    $response = $this->Http_Client->request( 'GET' );

    if( $response instanceof Http_Response )
    {
        $this->assertEquals( 200, $response->getStatusCode() );
    }
    else
    {
        $this->fail( 'Expected Http_Response, got something else!' );
    }
}

首先我认为它看起来像很多重复代码,我有很多metotds,我在其中进行测试,所以会抛出异常。

正如我所说,在测试方面我是个初学者,我很难弄清楚我需要测试什么。

一些建议和技巧对我很有帮助:)

【问题讨论】:

    标签: php unit-testing testing phpunit


    【解决方案1】:

    下面的测试还不够。您应该测试所有可能类型的异常(浮点数、布尔值等)

    编写一个名为 getRandomNonStringValues() 的方法该方法将返回一个包含除字符串之外的不同类型值的数组,并在 foreach 循环中将所有这些值发送到 setUsername 方法。并测试您是否对这些值中的每一个都有异常。

    /**
     * @expectedException Exception
     */
    public function testSetUsernameThrowsExceptionIfParamIsNotString() {
        $this->Http_Client->setUsername( 1 );
    }
    

    您还应该测试 setUsername 方法的成功案例。创建方法 getRandomString() 并使用此方法测试您的方法。

    【讨论】:

    • 不,如果参数不是字符串,你只测试它会抛出异常
    猜你喜欢
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多