In this final part of a three-part series on unit testing, we discuss the use of graphical interfaces, unit testing in a web environment, and more. The article is excerpted from chapter six of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).
Because PHP is a Web-oriented language, you might want an HTML-based user interface for running your unit tests. PHPUnit comes bundled with this ability, using PHPUnit_WebUI_TestRunner::run(). This is in fact a nearly identical framework to TextUI; it simply uses its own listener to handle generate HTML-beautified output.
Hopefully, in the future some of the PHP Integrated Development Environments (IDEs; programming GUIs) will expand their feature sets to include integrated support for unit testing (as do many of the Java IDEs). Also, as with PHP-GTK (a PHP interface to the GTK graphics library API that allows for Windows and X11 GUI development in PHP), we can always hope for a PHP-GTK front end for PHPUnit. In fact, there is a stub for PHPUnit_GtkUI_TestRunner in the PEAR repository, but at this time it is incomplete.
There are three major times when you can write tests: before implementation, during implementation, and after implementation. Kent Beck, author of JUnit and renowned Extreme Programming guru, advocates to "never write a line of functional code without a broken test case." What this quote means is that before you implement anything—including new code—you should predefine some sort of call interface for the code and write a test that validates the functionality that you think it should have. Because there is no code to test, the test will naturally fail, but the point is that you have gone through the exercise of determining how the code should look to an end user, and you have thought about the type of input and output it should receive. As radical as this may sound at first, test-driven development (TDD) has a number of benefits:
Encourages good design—You fully design your class/function APIs before you begin coding because you actually write code to use the APIs before they exist.
Discourages attempts to write tests to match your code—You should do TDD instead of writing code to match your tests. This helps keep your testing efforts honest.
Helps constrain the scope of code—Features that are not tested do not need to be implemented
Improves focus—With failing tests in place, development efforts are naturally directed to making those tests complete successfully.
Sets milestones—When all your tests run successfully, your code is complete.
The test-first methodology takes a bit of getting used to and is a bit difficult to apply in some situations, but it goes well with ensuring good design and solid requirements specifications. By writing tests that implement project requirements, you not only get higher-quality code, but you also minimize the chance of overlooking a feature in the specification.
The Flesch Score Calculator
Rudolf Flesch is a linguist who studied the comprehensibility of languages, English in particular. Flesch's work on what constitutes readable text and how children learn (and don't learn) languages inspired Theodor Seuss Geisel (Dr. Seuss) to write a unique series of children's book, starting with The Cat in the Hat. In his 1943 doctoral thesis from Columbia University, Flesch describes a readability index that analyzes text to determine its level of complexity. The Flesch index is still widely used to rank the readability of text.
The test works like this:
Count the number of words in the document.
Count the number of syllables in the document.
Count the number of sentences in the document.
The index is computed as follows:
Flesch score = 206.835 – 84.6 x (syllables/words) – 1.015 x (words/sentences)
The score represents the readability of the text. (The higher the score, the more readable.) These scores translate to grade levels as follows:
8th and 9th grades
Flesch calculates that Newsweek magazine has a mean readability score of 50; Seventeen magazine a mean score of 67; and the U.S. Internal Revenue Service tax code to have a score of –6. Readability indexes are used to ensure proper audience targeting (for example, to ensure that a 3rd-grade text book is not written at a 5th-grade level), by marketing companies to ensure that their materials are easily comprehensible, and by the government and large corporations to ensure that manuals are on level with their intended audiences.