whitebear
whitebear

Reputation: 12455

How to write the basic test message on PHPUnit

I am a quite newbee for phpunit, so it might be stupid though .... I google around but not found.

This is my code and I have multiple API and URL to test.

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);


    }
}

then I test like this (I am using symfony2 framework)

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)

There is no message I expected by echo("first test").

So, even error happens, I can't tell which url shows the error.

My basic idea is wrong??

Upvotes: 1

Views: 1690

Answers (1)

Paladin
Paladin

Reputation: 1637

You should write one test for each test and in assertTrue you can put a message there.

Example:

     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');
    }

In your test you can now see the test what goes wrong (the message in assertTrue) and see, what test is failed (the name of the test).

Hope, this helps....

Upvotes: 1

Related Questions