Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run single test method with phpunit?

Tags:

php

phpunit

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.

like image 446
Alex Avatar asked Sep 29 '14 07:09

Alex


People also ask

How do I run a specific test in PHPUnit?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

Can we run single test method with PHPUnit?

Sometimes when you're trying to nail down a failing test you need a quick way to run just the thing thats broken, instead of your whole suite or a whole test case. Fortunately it's pretty simple with the --filter flag provided by PHPUnit.

Which method is used to create a mock with PHPUnit?

PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.

What is a PHPUnit test?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.


2 Answers

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 
like image 186
Alex Avatar answered Sep 25 '22 22:09

Alex


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() { } 
like image 40
iamtankist Avatar answered Sep 22 '22 22:09

iamtankist