PHPUnit Testing in the Browser

PHPUnit Test Report

Introduction

Over the last couple weeks, I’ve been learning PHPUnit. PHPUnit is a unit testing framework for PHP, based on the JUnit framework for JAVA. It was created by Sebastian Bergmann. I’ll be the first to say that it is a wonderful library and it greatly simplifies Unit Testing in PHP. That being said, PHPUnit is not pretty. You mainly interact with it through the terminal. Now, I like pretty things. The web is pretty. Web development is pretty (with Textmate at least). Why can’t unit testing be pretty? A few days ago, I set out to resolve this issue.

PHPUnit Test Report

PHPUnit Test Report is designed to make unit testing fun. It discovers all files in your test folder that have the suffix “TestCase.php”. From there it loads and runs any classes within those files that have the suffix “TestCase”. Following standard PHPUnit procedures, it will run all methods that has the prefix “test”. For the case above, it is loading the class “TagTestCase” (from file TagTestCase.php). It will then proceed to run through all its methods that have “test” in front of them – so it runs testStylesheet, testScript, testRemoveScript, etc.


The result colors represent the following:
  • Red – Failed (or error)
  • Green – Pass
  • Yellow – Skipped
  • Grey – Incomplete

Let’s learn about some of PHPUnit Test Report’s features.

Sandbox that PHP

PHPUnit Test Report sandboxes your PHP. If you have an error in your code, the report will give you information about the error.

Talk to me PHPUnit

One of the most frustrating aspects of Unit Testing with PHPUnit was that I was unable to print variables within the tests to help me debug my application. PHPUnit Test Report handles this nicely:

Link PHPUnit Test Report to your Tests

I made it really easy to start using PHPUnit Test Report immediately. You can either go to PHPUnit Test Report’s index in the browser and add the path to your tests:

https://localhost/PHPUnit-Test-Report/?path=PATH_TO_TEST_FILES

Or you can add an index in your tests directory (tests/index.php) and include PHPUnit Test Report’s index:

include '../../PHPUnit-Test-Report/index.php';

Start using PHPUnit Test Report today

Excited yet? I’m releasing PHPUnit Test Report today. PHPUnit 3.4.13 comes with the report. It should be self-contained, but let me know if you have any issues. As far as the continual development of this project, I started learning PHPUnit about a week ago, so I have a lot to learn about it. As I learn more and continue to test my applications, I’ll add more stuff. I’ll try to help as best I can with issues you run into but, I can’t guarantee that I’ll be able to help. Feel free to get under the hood and hack something up. It’s pretty basic code.

Download PHPUnit Test Report

Click the link below to go to PHPUnit Test Report’s project page on GitHub.

PHPUnit Test Report Project Page

Your Turn.

The project is now hosted on GitHub. I’d love to see any contributions on your part – someone with some real PHPUnit experience could really do some damage with this application! Happy testing!

Comments

Ryan McCue    •   https://ryanmccue.info/

Tuesday, June 29th, 2010

Whoa, this looks awesome. I've avoided PHPUnit previously because it lacked the ability to have a web interface, but I'm definitely going to migrate my existing tests now. Definitely a killer feature.

Matt Mueller

Wednesday, June 30th, 2010

Thanks Ryan! Yah, PHPUnit from the terminal made my head spin. I've been using the Unit Report extensively over the last two days and its really making my testing easier. By the way - I updated the report today - I fixed some of the sandbox issues, and you can now stop the test suite by using exit() in any test. Cheers!

KRavEN

Saturday, July 31st, 2010

Need to have it ignore .svn folders and anything in them

ncorpse    •   https://moshcode.info

Monday, August 23rd, 2010

Hey Matt. You really made my evening. This is exactly what I was looking for. I got it working in 20 minutes. You may should mention that TestClasses need to have TestCase in the filename and that the directory of your code should not be renamed. But I'm not leaving a comment to critice... Thank you very much for sharing this with us!

Asher Snyder    •   https://www.noloh.com

Saturday, September 18th, 2010

This is a great start! What's the code licensed under? I was hoping it would be on Github so that I could fork it and make some modifications. For example, it you could select xml, json files generated from PHPUnit, rather then always running tests. This way you could automate tests daily and still view the history visually.

dave

Friday, September 24th, 2010

Hello! think's for this, but it's not so easy to use in a framework (codeigniter, yii...) it would be nice to adapt it to controller/action (with a specific controller/action to run it?), the direct access to an index can't work or better, to make it at a nice library we could use to make our view pages (so we have only to import your class and use your api to display tests) :)

Jean-Philippe

Tuesday, November 9th, 2010

Regarding the SVN issue (mentionned by KRavEN), a quick fix is to edit the PHPUnit-Test-Report/PHPUnit.php file line 40 from: if(strstr($name, 'TestCase.php') !== false) { to: if(strstr($name, 'TestCase.php') !== false && strstr($name, '.svn') == false) {

meze

Friday, November 12th, 2010

XML configuration files aren't supported?

Shaked

Thursday, January 13th, 2011

Hey, very nice work! I`ve started to use your system, but I`m using PHPUnit 3.5.5 (Soon will change to 3.5.6 - its already out to production). When loading your /PHPUnit/ everything works fine, but I`m getting some unexpected errors from my tests because PHPUnit 3.4.13 missing some assertions that 3.5.5 has. I will try to move and read about what you wrote, but if you have a quick answer it will be very helpful. Thank you very much for you effort, this system will be very useful when PHP developers will use more & more PHPUnit.

Shaked

Thursday, January 13th, 2011

Hey, after making some debugging I found out that when using newer versions of PHPUnit you need to replace few lines. Goto ./PHPUnit.php, line #12 & line #13 You will see this : set_include_path(dirname(__FILE__)); require_once('PHPUnit/Framework.php'); replace with: require_once('PHPUnit/Autoload.php'); //Newer versions needs to require Autoload.php and not Framework.php Found here: https://code.google.com/p/yii/issues/detail?id=1641 I deleted this line: set_include_path(dirname(__FILE__)); because I`m including PHPUnit on my PATH already. there is no need to replicate your include twice, if you use this I guess you will see some errors (I didn't check it) Hope you will enjoy this system like me. Matt - thanks again for great system! P.S there are some problems with the comment system. I`m using Chrome Beta....

Matt Mueller

Thursday, January 13th, 2011

@Shaked - Thanks! I bundled PHPUnit because I couldn't figure out this issue at the time when I first wrote it. I'll take a look at integrating these changes!

Theodore R. Smith    •   https://www.phpexperts.pro/

Sunday, January 23rd, 2011

I would like the following code added to the repo as soon as possible. Requiring, or even supporting, full paths is very dangerous. Seriously. You should only always support relativistic paths to the base of the project. So add this to Main/index.php: 19 $path = str_replace('../', '', $_GET['path']); // Protect against unauthorized file access. 20 $_GET['path'] = realpath(dirname(__FILE__) . '/../../' . $path); Also, in the same file, I have this, that enables TestDox output and Code Coverage output, as well: 36 ?> 37 38 41 42 <iframe src="/" width="100%" height="100%"> 43 <?php

NSinopoli    •   https://github.com/NSinopoli/VisualPHPUnit

Monday, March 21st, 2011

Hi Matt, Thanks for releasing your code! I found your project after searching for a front-end for PHPUnit on StackOverflow. I ended up forking it and making a bunch of modifications - the most notable of which, I think, is the ability to log the unit test results (both visually and with JSON). Check it out here: https://github.com/NSinopoli/VisualPHPUnit Thanks again!

Matt Mueller

Tuesday, March 22nd, 2011

Wow NSinopoli. Your fork looks fantastic! Thanks for taking the time to bring this project to the next level! I'm going to take a look at your fork and test it out as soon as I can, then I'll either integrate it into the main project or just redirect to your project page. I also like the name "Visual PHPUnit" much better :-D.

NSinopoli

Monday, April 4th, 2011

Hi Matt - thanks for the kind words. I've another project in mind if you'd be interested in collaborating. I'd like to build a nice logging front-end (as an alternative to going through painfully verbose log files). I'm really impressed with your html/css skills and would love to have you cook up something akin to what you did with the PHPUnit project. I doubt it would take very long - shoot me an email and we can discuss the details! Thanks.

JKrems    •   https://hybrissimo.com

Tuesday, June 14th, 2011

Working on a yii-project and made a few minor changes to your script (for example *Test.php for filenames and running a bootstrap.php if present in the test directory). Now it "just works" when you set the path to your "/protected/tests/"-directory. :) If someone could use it: https://github.com/JKrems/yii-report

Nick Turner    •   https://www.nickturner.co.uk/libs/PHPUnit_Html/demo

Monday, July 18th, 2011

Thanks Matt, Looking at your effort gave me the incentive to go off and write my own version. Although the source is completely different I based the HTML style on yours and gave you credit in the report footer and in the README files. Instead of using the JSON listener and then deciphering its output, my version inserts a proper HTML printer object which derived from the base PHPUnit framework printer interface. It also uses the normal 'runner' object used by the command line version so that most command line options are available via URL parameters. Once again to use it you just drop the example 'index.php' file into your tests directory and point the browser at it. If the PHPUnit and PHPUnit_Html projects are not in the PHP include path then you need to either add them there or edit the 'index.php' file to add the paths to the projects. It also has the option to view the source code of each test in the report. You can see a demo of the output generated at https://www.nickturner.co.uk/libs/PHPUnit_Html/demo/. For more details read the README file at https://github.com/nickturner/PHPUnit_Html/ Thanks for the inspiration and hope you don't mind me reusing some of your ideas. Nick

Greenbar: Browser-Based TDD for Python | Yet Another Programming Blog    •   https://www.davehking.com/wordpress/?p=355

Sunday, August 21st, 2011

[...] PHPUnit Test Report (PHP) [...]

Sebastian Bergmann    •   https://sebastian-bergmann.de/

Thursday, August 25th, 2011

Looks nice, good job! However, I do not like the fact that you are bundling PHPUnit in your GitHub repository. It would be nice to just have a separate PHPUnit_WebUI package that can be installed in addition to PHPUnit. Please contact me via email in case you want to discuss this further.

Matt Mueller

Thursday, August 25th, 2011

It's the man himself! Haha, I don't like it either... The *only* reason I bundled it was because I couldn't get it working on the latest version of PHPUnit (at the time). It was a bad hack, but it worked for my purposes. This project needs an update, now that I'm working with PHP & PHPUnit again, I plan on dusting off the cobwebs.