We have started writing test in our application in the previous article. In you have noticed, when we run the vendor/bin/phpunit tests
command, all of our test were running. In a small application, there are few number of tests and it won’t be a problem. But, as the system grows, the number of tests also grows. Luckily, we can filter the tests.
Using Filename
In this article, we see how we can filter the different tests we have written. Let’s start by running a single test filtered by filename. To do so, we can run the command:
//syntax
vendor/bin/phpunit directory/filename.php
//example
vendor/bin/phpunit tests/ReceiptTest.php
This will run the single file ReceiptTest.php
.
Using Filter
Next, we will use Filter
parameter to filter the test. Filter parameter accepts string in the form of Classes, Methods or Namespaces. Look at the example below. The code will only run the testTax
method although ReceiptTest
contains another method also testTotal()
.
vendor/bin/phpunit tests --filter=ReceiptTest::testTax
Using XML File
We will filter our test using XML file. XML file contains configurations for our test cases. We can create xml configuration for tests using following command:
vendor/bin/phpunit --migrate-configuration
This will create a phpunit.xml
file on the root directory. If the directory already contains a file with the same name, it will back up the old file and create a new configuration file. A newly generated configuration file looks like below:
Now, when we run our test, it will pass with color syntax. Similarly, we don’t need to pass the directory while running test because it is configured in our xml file.
Now, we can filter the specific test by selecting the testsuite
name like app
or receipt
. Let’s check by running the below command:
vendor/bin/phpunit --testsuite=app
OR
vendor/bin/phpunit --testsuite=app --filter=testTax
When I run the second command, it will give the following output.

These are the basic methods for filtering test cases. It becomes extremely helpful when the system is large and we need to test hundreds or even thousands of test cases.