Mohamed Salem Lamiri
Mohamed Salem Lamiri

Reputation: 6077

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 ?

Upvotes: 2

Views: 4591

Answers (2)

Mohamed Salem Lamiri
Mohamed Salem Lamiri

Reputation: 6077

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

docker exec -it $(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 -it $(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 -it $(docker ps -n=-1 -q --filter name=my_php_container_name --format="{{.ID}}") phpunit

Hope this can help someone ...

Upvotes: 1

BMitch
BMitch

Reputation: 263597

No need to do a complicated lookup of the container id, here's an example of how you can do it within docker-compose:

$ cat docker-compose.yml 
version: '2'

volumes:
  testvol:
    driver: local

services:
  testapp:
    image: busybox
    entrypoint: "top"
    volumes:
      - testvol:/data

  testapp2:
    image: busybox
    entrypoint: "top"
    volumes:
      - testvol:/data

$ ../bin/docker-compose up -d
Creating test_testapp_1
Creating test_testapp2_1

$ ../bin/docker-compose ps
     Name         Command   State   Ports 
-----------------------------------------
test_testapp2_1   top       Up            
test_testapp_1    top       Up            

$ ../bin/docker-compose exec testapp ls -l
total 40
drwxr-xr-x    2 root     root         12288 Mar 18 16:39 bin
drwxr-xr-x    2 root     root          4096 Jun 11 11:07 data
drwxr-xr-x    5 root     root           360 Jun 13 11:50 dev
drwxr-xr-x    2 root     root          4096 Jun 13 11:50 etc
drwxr-xr-x    2 nobody   nogroup       4096 Mar 18 16:38 home
dr-xr-xr-x  224 root     root             0 Jun 13 11:50 proc
drwxr-xr-x    2 root     root          4096 Mar 18 16:38 root
dr-xr-xr-x   13 root     root             0 Jun 13 11:50 sys
drwxrwxrwt    2 root     root          4096 Mar 18 16:38 tmp
drwxr-xr-x    3 root     root          4096 Mar 18 16:39 usr
drwxr-xr-x    4 root     root          4096 Mar 18 16:39 var

You can also take advantage of the consistent container naming by docker-compose (it prepends the directory name and appends a number for scaling) to run the exec on the individual containers with docker exec:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
069d40e3b6c7        busybox             "top"                    2 minutes ago       Up 2 minutes                                 test_testapp2_1
60e6d34d3b5a        busybox             "top"                    2 minutes ago       Up 2 minutes                                 test_testapp_1

$ docker exec test_testapp_1 ls -l
total 40
drwxr-xr-x    2 root     root         12288 Mar 18 16:39 bin
drwxr-xr-x    2 root     root          4096 Jun 11 11:07 data
drwxr-xr-x    5 root     root           360 Jun 13 11:50 dev
drwxr-xr-x    2 root     root          4096 Jun 13 11:50 etc
drwxr-xr-x    2 nobody   nogroup       4096 Mar 18 16:38 home
dr-xr-xr-x  226 root     root             0 Jun 13 11:50 proc
drwxr-xr-x    2 root     root          4096 Mar 18 16:38 root
dr-xr-xr-x   13 root     root             0 Jun 13 11:50 sys
drwxrwxrwt    2 root     root          4096 Mar 18 16:38 tmp
drwxr-xr-x    3 root     root          4096 Mar 18 16:39 usr
drwxr-xr-x    4 root     root          4096 Mar 18 16:39 var

Upvotes: 1

Related Questions