如何在PHPUnit

时间:2017-03-03 09:57:50

标签: php symfony testing phpunit

我是一个非常新的phpunit,所以它可能是愚蠢的.... 我谷歌周围但没找到。

这是我的代码,我有多个要测试的API和URL。

namespace Acme\TopBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DefaultControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();


        echo ("first test");
        $crawler = $client->request('GET', '/api/getplaceinfo');
        $this->assertTrue($client->getResponse()->isSuccessful());

        echo ("second test");
        echo('test :' + '/api/getmetainfo/kibichuo');
        $crawler = $client->request('GET', '/api/getcat');
        $this->assertTrue($client->getResponse()->isSuccessful());

        echo ("third test");
        $crawler = $client->request('GET', '/admin/dashboard');    
        $this->assertTrue($crawler->filter('html:contains("My Server")')->count() > 0);


    }
}

然后我像这样测试(我正在使用symfony2框架)

whitebear$ phpunit -c app/
PHPUnit 4.8.35 by Sebastian Bergmann and contributors.

.0

Time: 3.69 seconds, Memory: 109.25MB

OK (1 test, 7 assertions)

回声(“第一次测试”)没有预期的消息。

因此,即使发生错误,我也无法分辨出哪个网址显示错误。

我的基本想法是错的?

1 个答案:

答案 0 :(得分:1)

您应该为每个测试编写一个测试,在assertTrue中,您可以在那里发送消息。

示例:

     public function testThirdTest() { 
      $client = static::createClient();      
      $crawler = $client->request('GET', '/admin/dashboard');    
      $this->assertTrue($crawler->filter('html:contains("My Server")')->count() > 0, 'third test goes wrong, put message here');
    }

在您的测试中,您现在可以看到测试出了什么问题(assertTrue中的消息),看看测试失败了什么(测试名称)。

希望,这有助于......