PHPUnit testing individually in Symfony

I would like to run separate tests of individual modules using PHPUnit, but I have certain classes that are separate from the tests, because I use the symfony structure, and I group the tests and classes in different folders.

I would like to run tests individually as follows:

php phpunit.phar MyTest.php 

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

It is not a problem to run all the tests together, thanks to phpunit.xml , but when I want to run them individually, it is a problem.

How can i fix this?

+4
source share
3 answers

You must tell phpunit where you have the phpunit.xml configuration phpunit.xml (because it needs to know, for example, the autoloader). If you have a symfony 2 structure by default, it will be in the application directory, so just run your test (I assume you are in the root path of the project):

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

change

All tests with names corresponding to the pattern will be executed above: /.*concreteTestPattern.*/

+9
source

You should use the --filter argument on your PHPUnit command line. This will only run tests that match the given pattern. If you pass only the full name of the test you want to run, phpunit should only run this test.

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

+3
source

PHPUnit can be configured to run using a configuration file.

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

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

If you use VCS, such as Git, you must add the new app / phpunit.xml file to the VCS ignore list.

You will also notice that the bootstrap file located in app / bootstrap.php.cache is specified in the configuration. This file is used by PHPUnit to configure the test environment.

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

$ phpunit -c app

+1
source

Source: https://habr.com/ru/post/1484462/


All Articles