LMS LMS - 5 months ago 134
PHP Question

Docker - What is the easiest way to launch phpunit testsuite

Is there any good way (faster) to launch phpunit on docker? Here what I used to do :

docker-compose build
docker-compose up
docker ps
docker exec <container_id> phpunit --configuration /myproject/src/Tests/phpunit.xml --testsuite testAllSuites


Note: I don't want to use Volume to sync files, so right now everytime I have to rebuild the project .. and I am trying to figure out how to launch test on a specific container without specifing the containe_id .. is it possible to launch it using the build name instead ?

LMS LMS
Answer

I just figure out an easiest way to launch phpunit tests on my php-fpm container, here the command :

docker exec $(docker ps -n=-1 -q --filter name=my_php_container_name --format="{{.ID}}") phpunit --configuration /myproject/src/Tests/phpunit.xml --testsuite testAllSuites

so now whenever I change anything on my code here what I do :

docker-compose build
docker-compose up
docker exec $(docker ps -n=-1 -q --filter name=my_php_container_name --format="{{.ID}}") phpunit --configuration /myproject/src/Tests/phpunit.xml --testsuite testAllSuites

So this means I don't have to display all docker containers (docker ps) to get the id and update the command manually to launch phpunit on the new container id.

Runing this command wil return only the container_id :

docker ps -n=-1 -q --filter name=my_php_container_name --format="{{.ID}}"

here some explanations :

-n=-1 : show n last ceated containers

-q : only display numeric IDs

for more details about options here the docker documentation

Note: Just for instance, if we move phpunit.xml file just under /myproject/ root the phpunit command can be even smaller and if we run phpunit it will automatically load phpunit.xml and run all tests under your /Tests/... folder and the command will be :

docker exec $(docker ps -n=-1 -q --filter name=my_php_container_name --format="{{.ID}}") phpunit

Hope this can help someone ...

Comments