476

I am struggling to run a single test method named testSaveAndDrop in the file escalation/EscalationGroupTest.php with phpunit. I tried the following combinations:

phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=escalation/EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=testSaveAndDrop

In each case all test methode in the file escalation/EscalationGroupTest.php are executed. How to select just ONE method instead?

The name of the class is EscalationGroupTest and the version of phpunit is 3.2.8.

1
  • What is the class name of your test class?
    – sectus
    Sep 29, 2014 at 8:06

14 Answers 14

657

The following command runs the test on a single method:

phpunit --filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php

phpunit --filter methodName ClassName path/to/file.php

For newer versions of phpunit, it is just:

phpunit --filter methodName path/to/file.php
9
  • 2
    Did writting EscalationGroupTest is required? What is the use of it?
    – mujaffars
    Feb 23, 2016 at 5:26
  • 3
    Ok I got it is the class name
    – mujaffars
    Feb 23, 2016 at 5:55
  • 26
    It will run test method with name testSaveAndDrop* ( example: testSaveAndDropSomething) too. To run exactly testSaveAndDrop use --filter '/::testSaveAndDrop$/' May 2, 2016 at 6:26
  • 4
    @mujaffars I doubt it, "I got it" is not a valid identifier in php due to the whitespace. Nov 27, 2018 at 20:40
  • 5
    This doesn't work now. It's just phpunit --filter methodName path/to/testing/file, no ClassName .
    – CJ Dennis
    Jan 11, 2019 at 1:03
279

I prefer marking the test in annotation as

/**
 * @group failing
 * Tests the api edit form
 */
public function testEditAction()

Then running it with

phpunit --group failing

No need to specify the full path in the command line, but you have to remember removing this before commit, not to clutter the code.

You may also specify several groups for a single test

/**
  * @group failing
  * @group bug2204 
  */
public function testSomethingElse()
{
}
6
  • 5
    I like this method, is it possible to assign multiple groups via the annotation? @group failing, Integration Aug 5, 2015 at 11:08
  • 5
    Yes, of course. But not comma separated. Edited the answer to illustrate this.
    – iamtankist
    Aug 5, 2015 at 12:14
  • I like this method better... for reference the <groups> element in phpunit.xml can also be used to filter the tests... use something like this right under <phpunit>: <groups><include><group>failing</group></include></groups> Sep 20, 2015 at 20:22
  • 2
    But the question was - to run SINGLE test. Also - group named failing is the worst you can use. Because after some tests fail you can run phpunit --group failing to run the failed tests without giving them the group. It's confusing. Sep 21, 2016 at 6:18
  • 1
    The best answer! there is NO SINGLE test in case it @depends from another so you have to pinpoint all interconnected tests and thus use the @group annotation.
    – centurian
    May 17, 2020 at 8:50
136

Here's the more generic answer:

If you are sure the method name is unique you can only filter by method name (this works for me)

phpunit --filter {TestMethodName}

However it is safer to specify the file path/reference as well

phpunit --filter {TestMethodName} {FilePath}

Example:

phpunit --filter testSaveAndDrop reference/to/escalation/EscalationGroupTest.php

Quick note: I've noticed that if I have a function named testSave and another function named testSaveAndDrop using command phpunit --filter testSave will also run testSaveAndDrop and any other function that starts with testSave*, it's weird!!

3
  • 8
    It's not weird at all, it is basically a substring match. If you want that behaviour, use end of string token: /testSave$/
    – Christian
    Jul 29, 2016 at 14:07
  • What if you only want to specify the file path so all tests in that file get run? Sep 9, 2016 at 18:26
  • 1
    @still_dreaming_1 Just don't add --filter {TestMethodName} and give only the path.
    – Den
    Jan 7, 2022 at 17:56
59

Following command will execute exactly testSaveAndDrop test.

phpunit --filter '/::testSaveAndDrop$/' escalation/EscalationGroupTest.php
4
  • 8
    Did not work for me. Using \b instead of $ finally did the trick: --filter '/::testSaveAndDrop\b/'.
    – Joe
    Apr 9, 2019 at 8:24
  • 1
    Both of the method name endings '/::testSaveAndDrop\b/' and '/::testSaveAndDrop$/' worked for me like this phpunit --filter ClassName::methodName$ and phpunit --filter ClassName::methodName\b (on Linux) Sep 11, 2019 at 12:28
  • Same here: both work (on Linux), plus the / characters are optional (PHPUnit 9.5.13).
    – SteeveDroz
    Jan 27, 2022 at 13:35
  • @Joe Just a hunch: do you have () after your method names in the PhpDoc comments? That may be why $ didn't work, and why \b works for both.
    – cautionbug
    Jun 1, 2022 at 21:34
57
  • Run this inside your project root directory i am using in laravel root directory.

vendor/bin/phpunit --filter 'Your method name'

Example with custom method name.

 /** @test //Initilize this for custom method name, without test keyword
  *  
  * Test case For Dashboard When User Not logged In it will redirect To login page
  */
  public function only_logged_in_user_see_dashboard()
  {
    $response = $this->get('/dashboard')
                   ->assertRedirect('/login');
  }

Example with test keyword

/**
* A basic test example.
*
* @return void
*/
 public function testBasicTest()
 {
  $this->assertTrue(true);
 }
0
47

for run phpunit test in laravel by many way ..

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

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

for test single class :

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

30

So, something like this

phpunit --filter 'EscalationGroupTest::testSaveAndDrop' EscalationGroupTest escalation/EscalationGroupTest.php 

Without = and with '

https://phpunit.de/manual/3.7/en/textui.html

5
  • 1
    No this does not work. All tests in the class EscalationGroupTest are being processed.
    – Alex
    Sep 29, 2014 at 8:11
  • Still all 9 tests are being run. phpunit version 3.2.8
    – Alex
    Sep 29, 2014 at 8:12
  • 2
    Move the --filter to before the filename and things should work right.
    – Schleis
    Jan 29, 2015 at 20:21
  • Worked for me on PHPUnit 4.8.10
    – Asaph
    Oct 12, 2015 at 22:19
  • This gives me No tests executed! on PHPUnit 4.8.35 @Schleis How exactly did you change the command?
    – Andru
    Jul 31, 2017 at 9:44
9

Given that you

vendor/bin/phpunit --filter=EscalationGroupTest::testSaveAndDrop
8

If you're in netbeans you can right click in the test method and click "Run Focused Test Method".

Run Focused Test Method menu

0
7

You Can try this i am able to run single Test cases

phpunit tests/{testfilename}

Eg:

phpunit tests/StackoverflowTest.php

If you want to run single Test cases in Laravel 5.5 Try

vendor/bin/phpunit tests/Feature/{testfilename} 

vendor/bin/phpunit tests/Unit/{testfilename} 

Eg:

vendor/bin/phpunit tests/Feature/ContactpageTest.php 

vendor/bin/phpunit tests/Unit/ContactpageTest.php
1
  • 1
    The question was how to run single test METHOD (not class) Aug 22, 2018 at 12:10
5

The reason your tests are all being run is that you have the --filter flag after the file name. PHPUnit is not reading the options at all and so is running all the test cases.

From the help screen:

 Usage: phpunit [options] UnitTest [UnitTest.php]
        phpunit [options] <directory>

So move the --filter argument before the test file that you want as mentioned in @Alex and @Ferid Mövsümov answers. And you should only have the test that you want run.

2
  • I don't think this addresses the original question...I had the same problem of needing to run just one of the tests in a file containing many unit tests, and the two other answers using filters worked for me
    – jfoo
    Jan 29, 2015 at 0:27
  • @jfoo One of the problems with the OP's commands was that the --filter option was after the file name. The other two answers had the correct answer but did not point out why the filter was being applied.
    – Schleis
    Jan 29, 2015 at 20:20
2

If you're using an XML configuration file, you can add the following inside the phpunit tag:

<groups>
  <include>
    <group>nameToInclude</group>
  </include>
  <exclude>
    <group>nameToExclude</group>
  </exclude>
</groups>

See https://phpunit.de/manual/current/en/appendixes.configuration.html

1

I am late to the party though. But as personal I hate to write the whole line.

Instead, I use the following shortcuts in the .bash_profile file make sure to source .bash_profile the file after adding any new alias else it won't work.

alias pa="php artisan"
alias pu="vendor/bin/phpunit" 
alias puf="vendor/bin/phpunit --filter"

Usage:

puf function_name

puf filename

If you use Visual Studio Code you can use the following package to make your tests breeze.

Package Name: Better PHPUnit
Link: https://marketplace.visualstudio.com/items?itemName=calebporzio.better-phpunit

You can then set the keybinding in the settings. I use Command + T binding in my MAC.

Now once you place your cursor on any function and then use the key binding then it will automatically run that single test.

If you need to run the whole class then place the cursor on top of the class and then use the key binding.

If you have any other things then always tweek with the Terminal

Happy Coding!

0

You must use --filter to run a single test method php phpunit --filter "/::testMethod( .*)?$/" ClassTest ClassTest.php

The above filter will run testMethod alone.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.