Dean's avatar
Level 7

PHPUnit won't run one test in a file but the rest are fine

I added a new test to one of my test files, and all the other tests run fine as does running all tests but when I run just the specific test via vendor/bin/phpunit --filter only_the_validated_fields_are_set_when_creating_a_thread the test doesnt run and I jsut get a No tests executed! message.

function only_the_validated_fields_are_set_when_creating_a_thread()
{
    $this->withExceptionHandling()->signIn();

    $thread = make('App\Thread', ['title' => 'Test Title']);

    $thread['slug'] = 'Mass Assigned';
    $thread['id'] = 999;

    $response = $this->post(route('threads'), $thread->toArray());

    $this->get($response->headers->get('Location'))
        ->assertSee('Test Title');

    $thread = \App\Thread::first();

    $this->assertEquals($thread->title, 'Test Title');

    $this->assertEquals($thread->slug, 'test-title');
    $this->assertEquals($thread->id, 1);

}
0 likes
3 replies
Jaytee's avatar
Jaytee
Best Answer
Level 39

Make sure you're telling PHPUnit about the test. Either with a doc block or prefixing the function with test_

Doc Block:

/* @test */
1 like
burlresearch's avatar

PhpUnit is pretty flexible when comes to filtering for tests, but there are a couple things to check - and more information is probably needed.

  1. if your test function name doesn't begin with test - you need to annotate the function with @test, to expose to PhpUnit, ensure:
/** @test */
function only_the_validated_fields_are_set_when_creating_a_thread() 
{ 
// ... 
}
  1. from the command line, you still have to specify the filename: $ phpunit --filter only_the_validated tests/Unit/<filename>

there could be other things, but this is my first guess.

Dean's avatar
Level 7

Thanks guys, yes the issue was not having the Doc Block, I've written so many tests I cant believe this is the first time I've forgotten it!

Please or to participate in this conversation.