首页 > 解决方案 > Phpunit 代码覆盖率没有捕获我的代码

问题描述

我正在尝试使用 codeception 和 guzzle 对我用 php 编写的 API 进行 phpunit 测试,所有测试都在运行,但代码覆盖率始终显示 0/0。我在下面分享我的测试功能

public function testInputget()
{
 $c = $this->id;
 // create our http client (Guzzle)
$client = new Client();
$url = 'http://localhost/unit_test/testApi/user';
if($c != '')
{
    $url .='/'.$c; 
}
// echo $url;
try
{

    // $response = $client->request('GET', 'http://localhost/unit_test/testApi/user');
    $response = $client->request('GET', $url);
    // var_dump(expression)
    $this->assertEquals(200, $response->getStatusCode());
    $this->assertEquals('success', json_decode($response->getBody()->getContents())->status);  
    // $this->assertInternalType('array', json_decode($response->getBody()->getContents())->users);   
    // print_r(json_decode($response->getBody())->users);  
    $this->assertEquals($this->username, json_decode($response->getBody())->users[0]->username);
    $this->assertEquals($this->age, json_decode($response->getBody())->users[0]->age);        
    $this->assertEquals($this->status, json_decode($response->getBody())->users[0]->status);        
    // var_dump(json_decode($response->getBody())->users);  


}
catch (RequestException $z) 
{

    // Catch all 4XX errors 

    // To catch exactly error 400 use 
    if($z->getResponse()->getStatusCode() != '200') 
    {
        // print_r($z->getResponse()->getBody()->getContents());
        // echo json_decode($z->getResponse()->getBody()->getContents())->remark;
        // $this->assertEquals($a, $z->getResponse()->getStatusCode());
        // $this->assertEquals($b, json_decode($z->getResponse()->getBody()->getContents())->status);
        // $this->assertContains('utsab', json_decode($response->getBody()->getContents())->users);        
    }

}     
}

我正在使用的命令是

./vendor/bin/codecept 运行 --coverage --coverage-html

标签: phpunitphp-code-coverage

解决方案


推荐阅读