Let’s learn about Integration testing today. Integration testing is a method of testing by passing in the real dependencies and thus testing out the integration between two or more objects.
Please check out Part 4, if you have not read it yet: https://hackernoon.com/php-test-driven-development-part-4-enter-the-mock-106b4fdedd00
Taking the same previous example. We’ll do an integration test of the Math class. This will test both the Math class and also the Calculate class. This is because Math class calls Calculate class while we are testing it.
If you would also like to check out the accompanying YouTube video, here’s the link: **P.S watch till the end to see me dance ** 😄
First let’s add “Integration” test suite to our phpunit.xml file.
Since this is the first time we’re writing an integration test, let’s go ahead and create a new folder called “Integration” inside the “tests” folder.
Finally let’s make a MathTest.php file inside the “Integration” folder, with the following content:
Here, on line 2, we use the “Tests\Integration” namespace. We’re following the same pattern here as the Unit tests. All integration tests go under the integration namespace.
On line number 6, we’re extending the default Laravel TestCase class. In Unit tests we just extended the default PHPUnit TestCase class as that was enough for us. But to write integration tests, we’ll need to provide in real dependencies to the class we’re testing. We could provide them one by one, but that’ll get really tedious for a lot of integration tests. This is where Laravel’s TestCase class comes in handy. It has an instance of “App” class which is a Inversion of Control(IoC) container provided by Laravel. This helps us resolve dependencies automatically. Read more about the Laravel’s IoC container here: https://laravel.com/docs/5.7/container
Line number 10 is important too, because if you do not run the parent setUp method, the Laravel’s IoC container and helper methods wont work. This is required if you’re extending the Laravel’s default TestCase class.
We can see the IoC in action at line 12. $this->app->make() is what we’re using to instantiate the App\Math class we have. Notice we do not provide any dependencies to it. The Calculate class is automatically injection by the IoC container.
The rest starting from line 15 is the same as the unit test. All that is different here is that we do not use any Mockery assertions. We have no use of Mockery assertions because we are passing in real dependencies.
Similarly you can write more integration tests to cover up more cases. We can utilize the same tests that we wrote for Unit tests here too removing the Mockery assertion parts.
We can run this Integration class “MathTest” by providing the absolute or it’s relative path to phpunit. For example:
As you can see, we have 3 tests and 6 assertions that passed.
Congratulations! You now know how to write Integration tests :).
In the next part, we’ll learn how to write Functional tests. I hope you enjoyed the article. Please remember to give some claps 👏 to this article. Also please follow me for more articles like this.
See you on the next one. Cheers 😉
My other series on medium: