Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phpunit tests gives warning no tests found in class

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.

like image 285
ytsejam Avatar asked Dec 22 '14 08:12

ytsejam


People also ask

What is a PHPUnit test?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

What is assertion in PHPUnit?

The assertion methods are declared static and can be invoked from any context using PHPUnit\Framework\Assert::assertTrue() , for instance, or using $this->assertTrue() or self::assertTrue() , for instance, in a class that extends PHPUnit\Framework\TestCase .

How do I run a PHPUnit test?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.


1 Answers

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.

like image 69
CJ Thompson Avatar answered Sep 19 '22 12:09

CJ Thompson