AssertSame vs AssertEqual in PHPUnit

Aug 15, 2017 php phpunit testing
This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

When you’re testing inside of your PHPUnit test methods, you will use many assert-based methods. Two that are seemingly very similar are assertSame() and assertEqual()

The difference between these is the same as the difference of === and == in PHP. One is equal and one is identical.

assertSame is the closest to identical comparison that you can use. So, when you have a choice, use assertSame() instead of equals. This will help catch type mismatches as well.

Go to All Posts