Running Laravel tests with database in pipeline
Asked by Artis Smirnovs:

I have an issue running tests that requires DB connection. First part where pipeline is built works fine. Even \`php artisan migrate\` goes through.  
But Test step fails when test tries to add data to database with: \`could not find driver (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')\` . I tried changing host to 127.0.0.1 or mysql but it doesn't change anything.  

Most of my .yml file is based on example I found in bitbucket documentation so I am nt sure why its not working.  

Here is my bitbucket-pipelines.yml file:

```
# Template PHP Build

# This template allows you to validate your PHP application.
# The workflow allows running tests and code linting on the default branch.
image: php:8.2-fpm

pipelines:
  default:
    - step:
        script:
          - apt-get update && apt-get install -y unzip zip
          - apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
          - apt-get update && apt-get install -qy git curl libmcrypt-dev default-mysql-client
          - docker-php-ext-install pdo pdo_mysql
          - yes | pecl install mcrypt-1.0.6
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - COMPOSER_ALLOW_SUPERUSER=1 composer install
          - cp .env.pipeline .env
          - php artisan migrate --force
        artifacts:
          - storage/**
          - vendor/**
          - public/**
          - .env
        caches:
          - composer
        services:
          - mysql
    - parallel:
        - step:
            name: Tests
            script:
              - php artisan test
definitions:
  services:
    mysql:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: 'homestead'
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        MYSQL_USER: 'homestead'
        MYSQL_PASSWORD: 'secret'
```

And here is .env:  

```
APP_ENV=local
APP_KEY=ThisIsThe32CharacterKeySecureKey
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
```


Answers:
author: Theodora Boudale
body: Hi Artis and welcome to the community!

At the moment there is no mechanism for a Pipelines build to wait for service startup (we have a request in our issue tracker for the build to wait until all services are ready: <https://jira.atlassian.com/browse/BCLOUD-15244>). If the command that fails is the first one in that step, the mysql service may not be ready yet. You can try adding a wait command as the first command of the build (e.g. for 5, 10 or 30 seconds) and see if that helps.

Please also ensure that:

* you have added the service in the step definition
* you are using as host name 127.0.0.1 (avoid using localhost, as some clients will attempt to connect via a local "Unix socket", which will not work in Pipelines)

You can also try debugging the build locally with Docker (using the same images you have in your yml file) to ensure that the configuration in your code is correct and the build succeeds locally with Docker:

* <https://confluence.atlassian.com/bbkb/debug-pipelines-locally-with-docker-1167698072.html>

In step 3 of the guide, there is a section on how to test with build services.

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud