5

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?

2
  • phpunit has a filter option which you can use. phpunit.de/manual/current/en/… What do you mean by running individually? Commented Jun 4, 2013 at 17:27
  • I mean running just 1 test, not all the test in all the files defined in the xml Commented Jun 6, 2013 at 10:23

3 Answers 3

11

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.*/

Sign up to request clarification or add additional context in comments.

1 Comment

Thats running all the test in that file. I just want to run one
3

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>

Comments

1

PHPUnit can be set to execute using a configuration file.

In our Symfony2 project this file is located at app/phpunit.xml.dist.

As this file is suffixed with .dist, you need to copy its contents into a file called app/phpunit.xml.

If you are using a VCS such as Git, you should add the new app/phpunit.xml file to the VCS ignore list.

You will also notice the configuration is specifying the bootstrap file located at app/bootstrap.php.cache. This file is used by PHPUnit to get the testing environment setup.

We can execute this test by running the following command from the root directory of the project. The -c option specifies that PHPUnit should load its configuration from the app directory.

$ phpunit -c app

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.