Downloads

Google Ad

CakePHP Rake Plugin

Date: Thu, Apr 1st 2010, 21:47 Author: nick Views: 11431 Comments share

CakePHP Rake Test Plugin

Info:
Watch:
Get it:
  • Download Now
  • git clone git://github.com/webtechnick/CakePHP-Rake-Plugin rake
Testing is one of the most essential parts of any application, and CakePHP apps are no different. CakePHP ships with a very nice full featured testing suite with a little help from simpletest. If you're not fully testing your CakePHP app you're really missing out.

Pump some models

Testing is like going to the gym. The more you go, the more confident you feel. You may not want to get up off the couch to go, but once you're there, your endorphins kick in and you get into it quickly. At the end of the day, you'll always be glad you went. Testing your app is taking it to the gym. The more you test your app the more confident you'll be as a programmer. You'll have zero hesitation refactoring your code or trying something new if you've written good tests. You'll feel better about your app, and more than likely catch a few bugs before they become real bugs. I encourage everyone to write tests, lots and lots of tests.


Testsuite Shell needs viagra

The CakePHP test suite comes with a rather nice testsuite shell that you can use to test all, groups, or parts of your app. I really prefer the command line for testing, its fast, and easy to see the general state of your app at any given time. However, coming from a rails background, I really missed the constant feedback that a rake test would give me. Watching those little dots scroll across my terminal as I develop just feels like a great massage, with the climax of all tests passing -- the ultimate happy ending. Unfortunately I don't get the same satisfaction from the built in CakePHP testsuite shell. I would run a rather large batch of tests and just stare at my flaccid cursor sit there a good few minutes until finally I saw my 1000+ tests pass. I wanted my massage back!

make an appointment with CakePHP Rake Shell

In efforts to make the command line testsuite more appealing, I decided to create a CakePHP rake plugin that extends the testsuite shell, adds a few extra features, and most importantly -- gives us that happy ending.

Installing is just like installing any other CakePHP plugin; download or checkout the plugin and put it in your app/plugins/rake directory (bonus points for putting in your cake-core-install /plugin directory for use within all your apps).

Rake is used just like the testsuite shell; in fact, if you ask rake for help it will tell you its the testsuite shell, that's because it is..... just extended.

Examples:
  1. $ cake rake help
Outputs the same as cake testsuite help with the added option and description of 'verbose'.

Use rake just like you would the testsuite shell, but prepare yourself for the glorious dots to output on a per test basis, much like a progress bar.
  1. $ cake rake app case models/user
  2.  
  3.  
  4.  
  5. ------------------------------------------------------------
  6. CakePHP Test Shell
  7. ------------------------------------------------------------
  8. Running app case models/user
  9. .......................
  10. 1/1 test cases complete: 23 passes.
  11. Time taken by tests (in seconds): 1.36506414413
  12. Peak memory use: (in bytes): 14,349,472

You'll notice the only difference is that string if "."s in the output. Those "."s are outputted as the tests run and return as passes. It's hard to describe if you've never used Rails+Rake in a static format like a blog, but during the test the user is greeted with constant output to show what is going on in the tests. If there were failures, errors, exceptions, or skips, that output is buffered during the tests and outputted at the end like so:

  1. $ cake rake app case models/user
  2.  
  3.  
  4.  
  5. ------------------------------------------------------------
  6. CakePHP Test Shell
  7. ------------------------------------------------------------
  8. Running app case models/user
  9. ................f...f..
  10. FAIL->Expected false, got [Boolean: true] at [/home/wtn/dev/tests/cases/models/user.test.php line 130]
  11.   in testConfirmPasswordCheck
  12.   in UserTestCase
  13.   in /home/wtn/dev/tests/cases/models/user.test.php
  14.  
  15. FAIL->Expected false, got [Boolean: true] at [/home/wtn/dev/tests/cases/models/user.test.php line 157]
  16.   in testLocationExists
  17.   in UserTestCase
  18.   in /home/wtn/dev/tests/cases/models/user.test.php
  19.  
  20. 1/1 test cases complete: 21 passes, 2 fails.
  21. Time taken by tests (in seconds): 1.34627699852
  22. Peak memory use: (in bytes): 14,352,768

As you can see, we have "f"s in our once serene string of "."s. Those "f"s represent failing tests, and at the end of the tests the fails are then outputted for the user to see. The same is true for Errors, Exceptions, and Skips as well. Each error type is represented like so:
. = pass
f = failure
e = error
x = exception
s = skipped

The plugin also comes with an added option 'verbose'. This adds functionality that was missing in the TestSuiteShell that is present in the normal HTML testsuite -- the ability to see passed tests. You can append 'verbose' at the end of any rake command and any passed test is buffered and outputted at the end just like the others.
  1. $ cake rake app case models/user verbose
  2.  
  3.  
  4.  
  5. ------------------------------------------------------------
  6. CakePHP Test Shell
  7. ------------------------------------------------------------
  8. Running app case models/user
  9. ...
  10. PASS-> at [/home/wtn/dev/tests/cases/models/user.test.php line 50]
  11.   in testUserInstance
  12.   in UserTestCase
  13.   in /home/wtn/dev/tests/cases/models/user.test.php
  14.  
  15. PASS-> at [/home/wtn/dev/tests/cases/models/user.test.php line 58]
  16.   in testUserFind
  17.   in UserTestCase
  18.   in /home/wtn/dev/tests/cases/models/user.test.php
  19.  
  20. PASS->Equal expectation [Array: 1 items] at [/home/wtn/dev/tests/cases/models/user.test.php line 77]
  21.   in testUserFind
  22.   in UserTestCase
  23.   in /home/wtn/dev/tests/cases/models/user.test.php
  24.  
  25. 1/1 test cases complete: 3 passes.
  26. Time taken by tests (in seconds): 1.35410308838
  27. Peak memory use: (in bytes): 14,356,972

As the tests run, each pass is buffered and outputted for review at the end of all the tests. The verbose parameter works on any valid test command. Go on, give it a whirl!

Enjoy

It feels good to have my dots back, and it will feel good to you too. I hope you find this plugin as enjoyable as I do. Now get to the CakePHP gym! :)

Nick