Bastian's Blog

Bastian's Blog

Info | HTML | Text | RSS

PHPUnit - Some lesser known assertions

It just came into my mind: Could this be part one of a series? Probably. Let's see how it turns out. As I already pointed out in the abstract, people often do not know their tools. Even or because they are using them each and every day and do get caught in the same old pace they do everytime. I think it's time to change that.

For today let's focus on some Assertions which make my life much easier writing unit tests.


In general an assertion is an expectation of the developer on what your system under test (SUT) will return and how it will react in different situations. As of the part of unit testing an assertion is e.g. the content of a variable, the return value of the tested method. As you can imagine there are infinited ways to verify this behavior and PHPUnit comes with a handy number of assertion to help you being successful in this task.

Ok let's be honest, in every case we always do the boring comparison between a returned and an expected value. But insstead of always using the same old assertEquals() we are going to explore some more special assertions. So, let's start with a set of string comparison methods.

XML to the rescue

In many places XML is used to communicate and exchange data e.g. between to machines by using webservices. To verify that the returned value of the method is as expected, PHPUnit provides several assertions to do this:

Meanwhile the first 4 methods rely on the equal structure and nodevalues the last one (assertEqualXMLStructure()) only testifies the correctness of the XML structure. This comes in very handy when testing webservices with changing content whithin an integration test.

Reading invisible attribute

Sometime methods to be tested simple do not return anything, but alter class attributes in any way. This is the time where people start to question the unsability and reliability of (unit) tests. They start building proxy-classes to raise the visibility of these class-attributes (since it is possible to raise the visibility from protected to public), or even worse change the visibility of the attribute in the production code and simply ignore the benefits of proper set visibilities as in a part of a decent OOP strategy. Here is a releaving message for everyone using these smells in his sources: User one of the assertAttribute*() methods to read a specific protected or private class attribute. Also readAttribute() is able to alter the visibility of an attribute to read its value.


As you might expect every method I named earlier has also a negative sibling. To gain the reversed assertion just add a Not to the method name at the correct position (e.g. AssertAttributeNot*()).

So much about some findings I made using PHPUnit extensively in the early past. Let's see I think I will write about Mock- and Proxy-Objects soonish.

If you like this article, I'd be very pleased if you'd flattr me.



Add new comment

Fields with bold names are mandatory.