How to execute specific test class using PHPUnit in Laravel
Asked Answered
G

10

153

I want to test specific testClass in my project since there are a lot of test class that's failure and I just want to test one Class at a time.

I've created the test Class in following folder \test\repositories\ApplicationVersionFormat.php :

<?php
use App\Repositories\ApplicationVersionFormat;

class ApplicationVersionFormatTest extends \TestCase
{
  public function testValidFormat()
  {
    $version = '1.2.3';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(true,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat()
  {
    $version = '11.2.3';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat2()
  {
    $version = '1.22.3';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat3()
  {
    $version = '1.2.33';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat4()
  {
    $version = '11.22.33';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }
}

so I've tried this follwing command but none of this works :

  • phpunit "repositories\AppVersionTest". => Cannot open file "test/repositories/AppVersionTest.php"
  • phpunit "test\repositories\AppVersionTest". => Cannot open file "test/repositories/AppVersionTest.php"
  • phpunit --filter "repositories\AppVersionTest". => No tests executed!
  • phpunit --testsuite "repositories\AppVersionTest". => No tests executed!
Genuflection answered 24/8, 2016 at 8:25 Comment(2)
what's the output of the command you execute?Moloch
Try checking to some inclusion/exclusion configuration in your phpunit.xml(.dist) files. Can you post this files?Moloch
G
171

After trying several ways, I found out that I don't need to include the folder to test the specific test class. This works for me it runs all the test on the class:

phpunit --filter ApplicationVersionFormatTest

I think it's because my ApplicationVersionFormatTest extend The TestCase and return application instance which serves as the "glue" for all the components of Laravel.

Genuflection answered 24/8, 2016 at 9:44 Comment(3)
In addition to the phpunit command, you may use the test Artisan command to run your tests. The Artisan test runner provides verbose test reports in order to ease development and debugging:Carolynecarolynn
I've tried to put the whole test namespace, but it didn't work; you should pass the class name only for it to work.Eutrophic
You last voted on this answer May 26, 2021 at 13:51. Your vote is now locked in unless this answer is edited. here we come again :) after 2 yearsVoucher
F
126

for run phpunit test in laravel by many ways ..

vendor/bin/phpunit --filter methodName className pathTofile.php

vendor/bin/phpunit --filter 'namespace\\directoryName\\className::methodName'

for test single class:

vendor/bin/phpunit tests/Feature/UserTest.php
vendor/bin/phpunit --filter  tests/Feature/UserTest.php
vendor/bin/phpunit --filter 'Tests\\Feature\\UserTest'
vendor/bin/phpunit --filter 'UserTest' 

for test single method:

 vendor/bin/phpunit --filter testExample 
 vendor/bin/phpunit --filter 'Tests\\Feature\\UserTest::testExample'
 vendor/bin/phpunit --filter testExample UserTest tests/Feature/UserTest.php

for run tests from all class within namespace:

vendor/bin/phpunit --filter 'Tests\\Feature'

for more way run test see more

Fossorial answered 28/9, 2018 at 14:47 Comment(2)
vendor/bin/phpunit --filter tests/Feature/UserTest.php returns to test excuted in my case.Eutrophic
Thank you so much for this one vendor/bin/phpunit tests/Feature/UserTest.php, finally in the end from all the others this one worked for me. Because I strictly wanted to test single file, with filter it goes into fuzzy search and then tests also similar tests that contained the Deal name in the above case, but your solution there was fool proofed. Thanks mate.Estaminet
N
97

To do the same thing using artisan run:

php artisan test --filter ApplicationVersionFormatTest
Northampton answered 10/8, 2020 at 20:45 Comment(3)
In my case it works with the default Feature and Unit folders, but if i create a new testsuite, it does not work .Reinaldo
@Reinaldo make sure your newly added test class ends with Test, else PHPUnit will not recognize it.Valedictorian
@Reinaldo be sure to add the new test suite to your phpunit.xml file.Northampton
S
30

For newer version this might work

php artisan test --filter {MethodName}

// for instance
php artisan test --filter test_example

OR

php artisan test --filter {ClassName}::{MethodName}

//for instance
php artisan test --filter ExampleTest::test_example
Scarberry answered 19/8, 2021 at 12:11 Comment(1)
What about filtering by directory?Reinaldo
D
11

You can probably manage that with this answer to a related question.

Just mark the class with @group annotation and run PHPUnit with --group <group_name>

Update

Your command with --filter is not complete. Try this: phpunit --filter AppVersionTest "repositories\ApplicationVersionFormat.php"

Disrespectable answered 24/8, 2016 at 8:47 Comment(0)
S
4

For laravel 8

php artisan test --filter ExampleTest
Selfimportant answered 2/4, 2022 at 8:41 Comment(0)
P
2

In laravel 10

For specific test class:

php artisan test --filter UserTest

for a specific method test of UserTest:

php artisan test --filter UserTest::test_create_user
Pritchard answered 20/9, 2023 at 10:27 Comment(0)
M
1

phpunit --filter <relative_path_to_file> for example , files to test are,test/repos/EloquentPushTest.php,test/repos/EloquentWebTest.php for testing EloquentWebTest.php

use phpunit --filter ./repos/ElqouentWebpush

Melbourne answered 17/10, 2017 at 8:6 Comment(0)
T
0

Example of use:

php phpunit-5.7.27.phar -c app/ "src/package/TestBundle/Tests/Controller/ExampleControllerTest.php"
Triecious answered 29/11, 2018 at 7:39 Comment(0)
T
-1

Using phpunit.xml

the @group option can be used on classes and methods.

class A

/**
 * @group active
 * */
class ArrayShiftRightTest extends TestCase
{
}

class B

/**
 * @group inactive
 * */
class BinaryGapTest extends TestCase
{    
}

in phpunit.xml

<groups>
    <include>
        <group>active</group>
    </include>
    <exclude>
        <group>inactive</group>
    </exclude>
</groups>

the classes that belongs to group active will be executed.

Teel answered 14/1, 2020 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.