CL Simplex

Laravel: Introduction to Integration Testing

Laravel: Introduction to Integration Testing

We make use of unit tests, and integration tests within our projects to help catch bugs before code goes live. This mostly helps with “regression errors.” Regression errors are bugs in new code that breaks old things that use to work. Introducing testing has allowed us to focus our work on feature development and spend much less time correcting and putting out fires. Laravel’s built in testing capabilities make it easy to spot the smoke.

Quality Code: Start Small

Too many new processes at once and the project goes up in smoke. We will start with a small, simple test for our project. We have customized our phpunit.xml file, but the standard one included with stock Laravel projects will do nicely. We will customize the example test to introduce meaningful testing to a stock project.

Laravel 5 Integration Test Example

The ExampleTest.php stock Laravel comes with is cute, but ultimately not very useful once we start working on the project itself. Changing the homepage is often the first change. We will change the test to be more useful - it will give us important information now and in the future.

We will replace line 17 in ExampleTest.php

->see('Laravel'); with ->assertResponseOk();

Let’s examine what we’ve done. We now have a test that checks the response code of the homepage. Checking the homepage is not exactly a big deal - but that’s the beauty of it. This new integration test is a small change no one will complain about. Additionally, you now can test from the command line to see if package dependencies, or code changes crash your homepage. This means you can now use continuous integration tools to do the testing for you!

You can see the final result (while simple) on our github page.

Next Steps For Integration Tests

This is only the beginning. Beginning with page availability (does visiting this page crash the application?) you can slowly build more advanced tests (if I create a dummy calendar entry, does it properly appear in the calendar?) that allow you to know more about your code and frankly - programmers are paid to know things.

Navigation

Tap or click on these posts to navigate to the next or previous posts.

Post Series

This post is part of a larger series. Tap or click on a post to view more in this series.