74

I am trying to learn how to test with phpunit and laravel. When start the test using phpunit command, I am getting a warning :

There was 1 failure:

1) Warning
No tests found in class "PostsTest".

FAILURES!                            
Tests: 2, Assertions: 1, Failures: 

My test classname and filename matches. I have read other problems about unmatching names. my filename is PostsTest.php and my test file :

class PostsTest extends ApiTester {


    public function it_fetches_posts()

    {
        $this->times(5)->makePost();

        $this->getJson('api/v1/posts');

        $this->assertResponseOk();

    }

    private function makePost($postFields=[])
    {
        $post = array_merge([
            'title' => $this->fake->sentence,
            'content' => $this->fake->paragragraph
        ], $postFields);

        while($this->times --)Post::create($post);
    }
}

if necessary my ApiTester :

use Faker\Factory as Faker;

class ApiTester extends TestCase {
    protected $fake;
    protected $times = 1;
    function __construct($faker)
    {
        $this->fake = Faker::create();
    }
}

I dont have any clue where the error is. Laravel or my local phpunit settings or anything else. Any helps is appreciated.

Thanks.

ytsejam
  • 3,291
  • 7
  • 39
  • 69
  • I have read the manual in laravel. and I am following the tutorial in laracasts. Am I missing any other manual ? – ytsejam Dec 22 '14 at 08:42
  • 2
    I understand now. In the tut video I watched the function name was used like this and It was working. I did not guess the function name shall be "testItFetchesPosts". I thought it was just an example name. – ytsejam Dec 22 '14 at 08:53

4 Answers4

171

Annotations are the answer.

/** @test */
public function it_tests_something()
{
  ...
}

Adding that @test tells phpunit to treat the function as a test, regardless of the name.

bishop
  • 37,830
  • 11
  • 104
  • 139
CJ Thompson
  • 2,729
  • 2
  • 22
  • 26
112

The only methods that PHPUnit will recognize as tests are those with names starting with test.

So you should rename the it_fetches_posts() method to test_it_fetches_posts or testItFetchesPosts. The camel case naming is optional but useful if you use the --testdox option later.

Also, as stated in other answer you can also add the @test annotation to any method and it will be considered a test by PHPUnit.

gontrollez
  • 6,372
  • 2
  • 28
  • 36
16

Either begin its name with word 'test' like test_something_should_work or update the test docs with this annotation /** @test */

Yamen Ashraf
  • 2,637
  • 2
  • 20
  • 26
1

Additionally, consider a case where you are testing a class A that requires a class B (that you will mock). When $a->someMethod($mocked_B_class) is called, make sure you don't have any warnings in it such as trying to access a property of an array like you would access the property of a class ($array = ['one','two']; $array->one).

In this case it wont give you any information about the test or the error

shabany
  • 799
  • 8
  • 13