PHPUnit Testing: WordPress Unable to Connect to MySQL Database

I decided to use Semaphore CI to run automated test for a small WordPress plugin I made over the weekend.

I ran into a problem where WordPress was unable to connect to the database each time phpunit command is ran.

So i double checked to ensure the MySQL user and password credentials were correct and indeed they were. What then could be the problem?

After a lot of head banging, the solution was replacing localhost with 127.0.0.1 as the database host (DB_HOST).