R
R
Rishat Sultanov2017-06-14 13:43:14
PHPUnit
Rishat Sultanov, 2017-06-14 13:43:14

How does the test syntax from Laravel - PHPUnit work?

Good afternoon :)
In general, I'm sitting here writing tests and noticed a strange difference in the syntax. Or am I missing something in the documentation again.
In short, there are such tests.

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ThreadsTest extends TestCase
{
    use DatabaseMigrations;

    public function testA_user_can_view_all_threads()
    {
        $thread = factory('App\Thread')->create();

        $response = $this->get('/threads');

        $response->assertSee($thread->title);
    }

    function testA_user_can_read_a_single_thread()
    {
        $thread = factory('App\Thread')->create();

        $responce = $this->get('/threads/'. $thread->id);
        $responce->assertSee($thread->title);
    }
    function testNew_test()
    {
        $thread = factory('App\Thread')->create();

        $responce = $this->get('/thread');
        $responce->assertStatus(200);
    }
}

PHPUnit 5.7.20 by Sebastian Bergmann and contributors.

....                                                                4 / 4 (100%)

Time: 1.81 seconds, Memory: 16.00MB

OK (4 tests, 4 assertions)

I write the name of the function here with the test prefix as it is written in the documentation.
When running phpunit, the tests pass all 3 successfully as they should.
But on Laracast they write methods without the test prefix. That is like this:
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ThreadsTest extends TestCase
{
    use DatabaseMigrations;

    public function a_user_can_view_all_threads()
    {
        $thread = factory('App\Thread')->create();

        $response = $this->get('/threads');

        $response->assertSee($thread->title);
    }

    function a_user_can_read_a_single_thread()
    {
        $thread = factory('App\Thread')->create();

        $responce = $this->get('/threads/'. $thread->id);
        $responce->assertSee($thread->title);
    }
    function new_test()
    {
        $thread = factory('App\Thread')->create();

        $responce = $this->get('/threads');
        $responce->assertStatus(200);
    }
}

PHPUnit 5.7.20 by Sebastian Bergmann and contributors.

W.                                                                  2 / 2 (100%)

Time: 652 ms, Memory: 8.00MB

There was 1 warning:

1) Warning
No tests found in class "Tests\Feature\ThreadsTest".

WARNINGS!
Tests: 2, Assertions: 1, Warnings: 1.

And everything works for him. But I can’t find it ..
I’m also confused by the number of tests, why are there 4 of them? And in the second version, only 2 ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2017-06-14
@rishatss

Maybe in the lessons the methods have an annotation @test ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question