2013年9月30日星期一

Chris Hartjes: Testing Listeners


In the latest post to his site, Chris Hartjes offers some advice about unit testing with listeners to help teach PHP developers the right way to test.



I had an idea to put together some kind of "PHP Testing Koans" site as a way to help PHP developers get better at learning how to actually write tests. Most developers who are introduced to testing get blocked at the point of actually writing a test. [...] So I started to brainstorm ways to make it happen. With some help from Joel Clermont I stumbled upon using test listeners for this.


He uses the built-in test listeners for PHPUnit to write a system that checks to ensure a certain test exists in a "Koan1Listener" class. This class implements the PHPUnit_Framework_TestListener interface and has several methods to catch events and handle issues thrown during execution.



The approach is simple: for each test class that gets executed, add the names of all the methods to an internal list. When the entire test suite is finished, we then check to see if the test names that we were expecting are in our list of methods we found. I am sure there is a more efficient way to do it, so let me know in the comments of a different approach.

Link: http://www.littlehart.net/atthekeyboard/2013/09/27/test-listeners/

没有评论:

发表评论