PHPUnit has been blowing away its competition for more than 5 years now. With the use of the right PHPUnit assertions, you can get the most benefit out of PHPUnit. In this post, we will look into some popular and very useful PHPUnit assertions you should be using day to day in your PHPUnit tests. Let’s get cracking! Show
Table of contentsPHPUnit Assertions to know aboutCurrently, PHPUnit is at version 9.5. As per the latest docs there are 60 assert methods you can use in your PHPUnit tests.
Analyzing PHPUnit Assertions usage in a small, medium, and large projectTo know which PHPUnit assertions are used most in the projects I have worked with in the past years I ran the following command:
I will be taking three projects, one small, the second one medium, and the third one is a large one. The size is not only the code but also the number of tests and assertions in the projects.
Small Project with PHPUnit AssertionsThe small project built with Symfony as a REST API has only 104 tests and 306 PHPUnit assertions. The above command when executed on this project’s tests folder gave the following output:
After that, 1 is used 51 times in this relatively small project. This means almost 1 out of 4 PHPUnit assertions for this project is 0.Medium project for PHPUnit Assertions countThe medium project built with Symfony which is a REST API has 221 tests and 472 PHPUnit assertions. The above command when run on its tests directory looks like below:
The numbers of asserts don’t add up to 221 because there are a lot of PHPunit data providers used in these tests. Still, 1 and 0 make up almost 16% of the assertions used in this project. Recently we switched the code coverage driver to be PCov instead of Xdebug. It made the test coverage much faster. If you are interested, I had also blogged about unit testing in Laravel in the past.Big project for counting PHPUnit AssertionsNext up is the big-sized project, it has both APIs and user interface. It has unit tests as well as some integration tests. This project has a whopping 5824 tests and 13975 PHPUnit assertions. When we run the above PHPUnit assertions counter command on this project’s tests folder it yields:
That accounts for 22.5% of all asserts in the test suite. Next in line is 8 which is used 942 times, making it get 6.74% of the pie. Let’s have a look at some more PHPUnit assertions that are very helpful but not that obvious.Anatomy of PHPUnit assertionsAssertions in PHPUnit follow a pattern, for instance, 0 takes 3 arguments. The first one is 0, the second one is 1, and the third one which is optional but important is 2.
For instance the following test:
Will result in:
Notice the 3, these types of messages will be helpful when you debug which tests did not pass and how to fix them.Some PHPUnit assertions you must start using todayIn the past, I have used many PHPUnit assertions among the 60 odd available ones. Below are some of them which have been particularly useful: assertJsonStringEqualsJsonStringThis PHPUnit assertion is very handy when testing APIs’ responses. You can use this 4 assertion to check if the received JSON response matches what you were expecting it to be. To match the JSON string, you could do something like:
The next one is about Regex matching. assertMatchesRegularExpression or assertRegExpAs the name sounds the assert will match for the regular expression. I have found it helpful to test error messages that begin with something or have a defined pattern. It was called 5 in PHPUnit . Below is an example which checks for string 6 so 40 and any digit, the test below will as the test string is 7:
The next one is about strings. assertStringContainsStringThis is also a very handy assertion to check if a given string has another string. If you want to ignore the letter case you can use 8. The 9 and 0 PHPUnit assertions are also very useful. Below is a quick example of the string contains the string:
Consequently, we have the ever-popular assert Equals PHPUnit assertion with a twist. assetEqualsWithDeltaAnother useful PHPUnit assertion is 1. When you have a test where some variance is expected this assertion is super useful. Below is an example:
Subsequently, we have a PHPUnit assertion that is related to PHP arrays. assertArrayHasKeyThe 2 assertion in PHPUnit is also very helpful. If you are using arrays are return types then you can use this assertion to check if the returned array has a certain key before checking the value. Below is an example of this practical PHPUnit assertion:
You can view all the 5 PHPUnit assertions’ examples in this gist. ConclusionYou can find a list of PHPUnit assertions in this list too.
|