PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Be More Asssertive: Getting to Know PHPUnit's Assertions

Note: This article was originally published at Planet PHP on 20 January 2011.
Planet PHP

In my previous article, I took you through some of the basics of unit testing with PHPUnit. I introduced you to what it means to test your code and the real benefits it can for you and your code. This time I want to dive in a little deeper and get to know PHPUnit in a bit more detail.

Marking Tests Skipped or Incomplete

PHPUnit includes two methods that, if used correctly, can make your testing life a little simpler. The two methods, markTestSkipped and markTestIncomplete, allow your tests to have different results than just passing or failing. markTestSkipped gives you a bit of an aoouta if a problem is encountered in the course of running your tests.

Say you're testing to see if a database record exists after the execution of a given method. In a perfect world, you'd always be able to connect to the database. What happens if you can't, though? Well, your test would most certainly fail, since it couldn't find the row. But that failing test might lead you to believe there was a problem with your code, even though there might not be. With the help of markTestSkipped, you can simply skip over that particular test in the current test run. Here's an example:

createObject(); try { $db = new Database(); $this-assertTrue($db-rowExists()); } catch (DatabseException $e) { $this-markTestSkipped('This test was skipped because there was a database problem'); } } ?

There are three possible outcomes here. First, of course, if everything works as intended, the test will pass. It's also possible that the assertTrue could fail, if the rowExists method returns false.

But finally, and most interestingly, the database object could throw a DatabaseException type of exception. Our try/catch block will catch the exception, which indicates that the database is unavailable, and therefore that the whole point of the test is moot. Instead of failing, it just skips the test. The aoskippeda result will still show up in all of your testing runs, so you'll still be made aware that the issue will need to be resolved eventually.

The markTestIncomplete method provides us with similar functionality, but with a different twist. It's intended to give you a way to mark a test as, big surprise, incomplete. What this means is that if you begin writing a test, and intend to come back to it later to finish it, you can make it clear in the test results that it's not a failure of the application code, but rather an incomplete test. You can even define a custom message so you'll know why a test was skipped:

markTestIncomplete("There aren't enough hours in the day to have my tests go green"); $trueVariable = true; $this-assertTrue($trueVariable); } ?

It's important to note that these two methods are a convenience only, and shouldn't be an excuse for you to write sloppy tests. Your application is only as good as the tests that support it, so be sure to revisit any incomplete or skipped tests once you're done working on the primary functionality you're testing.

Assertions, Assertions, Assertions

In the last article, I reviewed a number of often-used assertions provided by PHPUnit. Now, we'll review them and put them into practice in some sample code to show you how they're used.

Let's start out with the sample class we'll be testing:

1, 'second key' = 2);private $testString = "I do love me some strings"; public function __construct() { } public function addValues($valueOne,$valueTwo) { return $valueOne+$valueTwo; } public function getTestString() { return $this-testString; } } ?

Let's also set up our test class. This will be the container we drop the rest of the tests into so they can run against our Testable class:

_testable = new Testable(); } public function tearDown() { $this-_testable = null; } /** test methods will go here */ } ?

We're using the setUp and tearDown methods to prepare

Truncated by Planet PHP, read more at the original (another 12015 bytes)