how to test specific test class using phpunit in laravel

PhpLaravelPhpunitLaravel 5.2

Php Problem Overview


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!

any help? thanks

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Solution 3 - Php

To do the same thing using artisan run:

php artisan test --filter ApplicationVersionFormatTest

Solution 4 - Php

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

Solution 5 - Php

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"

Solution 6 - Php

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

Solution 7 - Php

Example of use:

php phpunit-5.7.27.phar -c app/ "src/package/TestBundle/Tests/Controller/ExampleControllerTest.php"

Solution 8 - Php

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.

Solution 9 - Php

For laravel 8

php artisan test --filter ExampleTest

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGujarat SantanaView Question on Stackoverflow
Solution 1 - PhpGujarat SantanaView Answer on Stackoverflow
Solution 2 - PhpJignesh JoisarView Answer on Stackoverflow
Solution 3 - Phptbone1000View Answer on Stackoverflow
Solution 4 - PhpOsman RafiView Answer on Stackoverflow
Solution 5 - PhpBVengerovView Answer on Stackoverflow
Solution 6 - PhpBhuvaneshwar NagarajView Answer on Stackoverflow
Solution 7 - Phpjoan16vView Answer on Stackoverflow
Solution 8 - Phpf_iView Answer on Stackoverflow
Solution 9 - PhpEdham RabuanView Answer on Stackoverflow