Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit testing individually in Symfony

Tags:

phpunit

I would like to run some unit Tests individually with PHPUnit, but I have certain classes separated from the Tests, since I am using the symfony framework, and I group the Tests and the Classes in different folders.

I would like to run the Tests individually like this:

php phpunit.phar MyTest.php

The problem is that the test file uses the classes from the controllers, and phpunit doesnt seem to be able to import the needed classes for the test.

This is not a problem to run all the tests together, thanks to phpunit.xml but when I want to run them individualy, its a problem.

How could I fix this?

like image 935
Enrique Moreno OB Avatar asked Dec 08 '22 15:12

Enrique Moreno OB


2 Answers

You have to point phpunit where you have your phpunit.xml config file (because it must know the autoloader for example). If you have default symfony 2 structure it will be in app directory, so just run your test like that (I assume that you are in project root path):

phpunit -c app/ --filter="concreteTestPattern" src/Acme/DemoBundle/Tests/MyTest.php

edit:

Above will run all tests which names match to the pattern: /.*concreteTestPattern.*/

like image 101
Cyprian Avatar answered Mar 06 '23 03:03

Cyprian


You would use the --filter argument in your PHPUnit command string. This will only run tests that match the pattern given. If you pass only the complete name of the test that you want run, phpunit should only run that test.

If you have a data provider associated with the test and only want to run one test case, you can also filter that by using --filter <testName>::<testcase name>

like image 28
Schleis Avatar answered Mar 06 '23 05:03

Schleis