How to run a specific test in Laravel Dusk?

How to run a specific test in Laravel Dusk?

Posted on:September 1, 2019 at 10:00 AM

If you ever stuck how to run a specific test in Laravel Dusk instead of all the test, this post might help you. There are probably two ways to run a specific test in Laravel dusk, by the file location and by using --filter keyword. Let’s dig into it.

Once you install dusk, you will able to see the list of the command of dusk in the php artisan command. It will be like this-

Laravel Dusk Command List

The problem is that running a specific test command is not listed there. So, here is the heck to run a specific test.

Imagine that, I have a test called HomePageTest. Now I want to run this particular test only. I can easily run by the file location in the test command.

php artisan dusk tests/Browser/HomePageTest.php

The above command will run the HomePageTest.php only. The rest of the test will be untouched.

Consider again, you want to run test by class name or any specific method name. The above solution won’t suitable to address this issue. In that case, we can use --filter keyword like PHP Unit.

HomePageTest.php

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class WelcomePageTest extends DuskTestCase
{
    /** @test */
    public function a_visitor_should_able_to_visit_the_homepage()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit(route('welcome'))
                    ->assertSee('A Platform for Conference Seekers');
        });
    }

Now I can call like follows, that will run the WelcomePageTest class only.

php artisan dusk --filter WelcomePageTest

By the same way, we can run any specific method. For example, we want to run only a_visitor_should_able_to_visit_the_homepage method. In this situation, we can run simply-

php artisan dusk --filter a_visitor_should_able_to_visit_the_homepage

Grouping

Besides, you can make a grouping of your methods and then run any specific group. For example-

class UserAndEventTests extends TestCase
{
    /**
    * @test
    * @group user
    * @group link
    */
    public function checkUserProfileLink(){...}

    /**
    * @test
    * @group event
    * @group link
    */
     public function checkEventLink(){...}
    }
}

Now if you run-

php artisan dusk --group user   // checkUserProfileLink()
php artisan dusk --group event  // checkEventLink()
php artisan dusk --group link   // Running both tests

Hope these tips will be helpful for you. Feel free to share your idea, if you have something else in your mind.

Thank you.