Launch Success: The new blog launched successfully! Thanks for everyone's feedback!

Running Tests from The Commandline in CakePHP

Posted by Tim Koschützki, 2 days, 11 hours ago - in PHP & CakePHP » Testing, Debugging & Refactoring

Hey folks,

I have contributed some new cool feature for you to the cake branch. A testsuite shell that allows you to run your tests from the commandline. It allows you to run all of the following:

  • All core testcases at once
  • All core test groups
  • Each core test case individually
  • All application-specific testcases at once
  • All application-specific test groups
  • Each core test case individually

It also supports plugins, which means you can run plugin cases and groups.

How does it work?

Actually it's very simple. You just call

./cake testsuite category test_type file

Category can either be "core", "app" or the name of a plugin.

The Test Type can be "case", "group" or "all".

The file is the name of the file with the folder prefix, but without the test.php / group.php suffix. Examples are: models/my_model, controllers/my_controller, etc.

Can you show me examples of valid calls to the testshell?

Yes, of course! Although I am going to directly copy this from the help screen from the testshell. Why should I reinvent the wheel? : P

./cake testsuite app all
./cake testsuite core all
./cake testsuite app case behaviors/debuggable
./cake testsuite app case models/my_model
./cake testsuite app case controllers/my_controller
./cake testsuite core case file
./cake testsuite core case router
./cake testsuite core case set
./cake testsuite app group mygroup
./cake testsuite core group acl
./cake testsuite core group socket
./cake testsuite bugs case models/bug  // for the plugin 'bugs' and its test case 'bug'
./cake testsuite bugs group bug  // for the plugin bugs and its test group 'bug'
./cake testsuite bugs_me case models/bug  // for the plugin 'bugs_me' and its test case 'bug'
./cake testsuite bugs_me group bug  // for the plugin bugs_me and its test group 'bug'

What does the output look like?

Welcome to CakePHP v1.2.0.6311 beta Console
---------------------------------------------------------------
App : app
Path: /Applications/MAMP/htdocs/cakephp/branch/app
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
Running core case router
Individual test case: libs/router.test.php
1/1 test cases complete: 190 passes.

Macintosh-6:branch dakangelbge$ 
elcome to CakePHP v1.2.0.6311 beta Console
---------------------------------------------------------------
App : app
Path: /Applications/MAMP/htdocs/cakephp/branch/app
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
Running app all
All App Tests
2/2 test cases complete: 6 passes.

Macintosh-6:branch dakangelbge$

Got any questions?

PS: There is some hidden feature in it, which I am not going to reveal until another thing got written about first. ; ) Anybody who knows already please do not spoil the fun. : P

5 Comments

Patrick on May 08, 2008:

Thank you very much! I immediately have to try this out…

Tim Koschützki on May 08, 2008:

Please let me know how you like it, Patrick.

Mariano Iglesias 20 hours, 58 minutes ago:

This is particularly useful to make sure the CakePHP code you share with others stay compatible on other PHP versions. For example, I'm running Apache 2.2, to which there's no PHP4 module, and at the same time I need to make sure stuff like Bindable is still working on PHP4.

So I run the web based version of the test suite on my normal web server (PHP 5.2.6 over Apache 2.2) and when I want to run the tests on PHP4 I use the command line, priorly modifying the path to PHP.

Travis 10 hours, 54 minutes ago:

Heya Tim, you left out one of the diffs between our Geoff-inspired work -- returning an indication of pass/fail to the invoking environment.

Tim Koschützki 9 hours, 32 minutes ago:

Travis: Good thinking yeah. I think I will add that.

Add a comment