how to test specific test class using phpunit in laravel

laravel env=testing
laravel integration testing
phpunit run specific test
laravel test assertions
php unit testing
laravel testing relationships
laravel create test database
laravel 5.8 test

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

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.

Getting Started with PHPUnit in Laravel, The first step when using PHPUnit is to create a new test class. The convention This is where you configure the specific options for your tests. Create Laravel Unit Test Case. While using PHPUnit, the first step is to create new test classes. These test classes are stored in the./tests/ directory of your Laravel application. Each test class is named as Test.php inside a folder.

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

Testing: Getting Started - Laravel, In fact, support for testing with PHPUnit is included out of the box and a phpunit.​xml file is In fact, most unit tests probably focus on a single method. <?php namespace Tests\Unit; use PHPUnit\Framework\TestCase; class ExampleTest  Testing: Getting Started. Introduction; Environment; Creating & Running Tests. Artisan Test Runner; Introduction. Laravel is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box and a phpunit.xml file is already set up for your application. The framework also ships with convenient helper methods that allow you to expressively test your applications.

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"

Testing - Laravel, Use Laravel Unit Testing to Avoid Project-Wrecking Mistakes. Pardeep Kumar While using PHPUnit, the first step is to create new test classes. These test Within this file, you can configure specific operations for your tests. To create a new test class, we can either create a new file manually or run the helpful Artisan make:test command provided by Laravel. In order to create a test class called BasicTest, we just need to run this artisan command: $ cd phpunit-test $ php artisan make:test BasicTest. Laravel will create a basic test class that looks like this:

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

Laravel Unit Testing Tutorial Using PHPUnit, I want to test something specifically in Laravel. Let's say How can I do that instead to running test on ev phpunit --filter {TestMethodName}. Use this command to run a specific test from your test suite. phpunit --filter {TestMethodName} If you want to be more specific about your file then pass the file path as a second argument. phpunit --filter {TestMethodName} {FilePath} Example: phpunit --filter testExample path/to/filename.php Note:

Example of use:

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

How to test specific test in Laravel., Sometimes when you're trying to nail down a failing test you need a quick way to run just the thing thats Fortunately it's pretty simple with the --filter flag provided by PHPUnit. class PagesControllerTest extends \TestCase { /** @test */ public function A blog about Laravel & Rails by Dwight Watson; Laravel provides a variety of helpful tools to make it easier to test your database driven applications. First, you may use the assertDatabaseHas helper to assert that data exists in the database matching a given set of criteria. For example, if you would like to verify that there is a record in the users table with the email value of sally@

Runing a single test case in PHPUnit, The following code shows how to run tests with the PHPUnit command-line test runner: working directory, load it, and expect to find a ArrayTest test case class. --random-order-seed=<N> Use a specific random seed <N> for random order  So you should rename the it_fetches_posts() method to test_it_fetches_posts or testItFetchesPosts. The camel case naming is optional but useful if you use the --testdox option later. Also, as stated in other answer you can also add the @test annotation to any method and it will be considered a test by PHPUnit.

3. The Command-Line Test Runner, In Testing array operations with PHPUnit, the fixture was simply the array that is run once for each test method (and on fresh instances) of the test case class. Following are some basic conventions and steps for writing tests with PHPUnit: Test File names should have a suffix Test. For example, if First.php needs to be tested,the name of the test file name will be FirstTest.php. Similarly, If the class name is `MyFirstClass` than the test class name will be `MyFirstClassTest`.

4. Fixtures, There are probably two ways to run a specific test in Laravel dusk, by the file location Consider again, you want to run test by class name or any specific method name. The above In that case, we can use --filter keyword like PHP Unit. If you are using Laravel’s RefreshDatabase trait, you will run into issues as a test may be rolling back or migrating the database as another is trying to write to it. Instead, skip persisting data by utilizing the DatabaseTransactions trait, which also does not attempt to change the database structure during the test suite run.

Comments
  • what's the output of the command you execute?
  • Try checking to some inclusion/exclusion configuration in your phpunit.xml(.dist) files. Can you post this files?