Databases and Unit Tests


In this video you will learn how to use a database in conjunction with your Codeception Unit test suite.

This is a bit of a contentious issue. Unit testing should be rapid, and talking to a database is usually quite a slow process as far as computers are concerned.

As such, there are two schools of thought on unit testing code that interacts with the database.

The first school will tell you to mock everything. Mocking allows you to trick your code into believing that it received a given response when a query was made. As you're specifying the response, you can make the response a good one or a bad one, and then make sure your code behaves appropriately in either situation.

The second school will tell you that mocking a database call is extra work and not really testing the system as it would work in real life. I disagree with this argument somewhat, but it is easier to use a real database when testing. And you do gain the added benefit of seeing genuine returned results for your queries. There are downsides to this - speed, reliance on fixtures, and so forth.

However, regardless of which school of thought you subscribe to, it is useful to know how to use a database with unit tests. And so by the end of this video you will have found out just how easy it is to get a working MySQL database for use with your Codeception Unit test suite.

Code For This Course

Get the code for this course.

Code For This Video

Get the code for this video.

Episodes