How to test your plugins?

Published on and tagged with cakephp  plugin  testing

I just uploaded a new version of my test suite to the downloads section. This version allows you to test your CakePHP plugins. In contrast to the “normal” tests which are placed in app/tests, plugin tests are placed in app/plugins/[plugin name]/tests. The used folder structure for the tests folder is almost identical to the one used for app/tests:

app/
    plugins/
        pluginname/
            tests/
                components
                controllers
                fixtures
                helpers
                models

Only the groups folder is missing, as I am not (yet) convinced it is needed.

You can write the plugin tests in the same way you write your other tests (see http://cakebaker.42dh.com/2006/12/18/testing-with-cakephp-12-a-preview/).

You can execute the plugin test via command line with:

php bake2.php test application-alias plugins  // executes all plugin tests
php bake2.php test application-alias plugin pluginname  // executes all tests of the specified plugin

Or if you use the web interface you can call:

http://example.com/tests/plugins  // executes all plugin tests
http://example.com/tests/plugin/pluginname  // executes all tests of the specified plugin

Feedback is welcome. Happy testing :)

8 comments baked

  • hydra12

    Thank you, Thank You, THANK YOU!!!! I’ll test this right now!

  • hydra12

    OK, I have a plugin called auth with a model called auth_login. auth_login has a function called authenticate that takes a username and a password.

    In transportation/plugins/auth/tests/models, I have this:

    auth_login_test.php:

    AuthLogin = new AuthLogin();
    }

    function testAuthenticate()
    {
    $this->assertTrue($this->AuthLogin->authenticate(‘mbuckner’,’Wh0me’));
    $this->assertTrue($this->AuthLogin->authenticate(‘hs-mbuckner’,’hunter’));
    $this->assertFalse($this->AuthLogin->authenticate(‘mbuckner’,’Hunt3r’));
    $this->assertFalse($this->AuthLogin->authenticate(‘es-mbuckner’,’test’));
    }

    }

    ?>

    When I run localhost/transportation/tests, it shows that auth has tests. When I click the link, I get a new page that says this:

    Tests for plugin auth
    0/0 test cases complete: 0 passes, 0 fails and 0 exceptions.

    What am I doing wrong?

    Thanks for the help!
    hydra12

  • hydra12

    OK, some of my code got cut off. Here it is again:

    AuthLogin = new AuthLogin();
    }

    function testAuthenticate()
    {
    $this->assertTrue($this->AuthLogin->authenticate(‘mbuckner’,’Wh0me’));
    $this->assertTrue($this->AuthLogin->authenticate(‘hs-mbuckner’,’hunter’));
    $this->assertFalse($this->AuthLogin->authenticate(‘mbuckner’,’Hunt3r’));
    $this->assertFalse($this->AuthLogin->authenticate(‘es-mbuckner’,’test’));
    }

    }

    ?>

  • hydra12

    I don’t know what’s up with my code getting cut off. Sorry. I posted a snippet at CakeForge. http://cakeforge.org/snippet/detail.php?type=snippet&id=178

  • hydra12

    It’s just not my day. Here’s the correct link: http://cakeforge.org/snippet/detail.php?type=snippet&id=179

  • hydra12

    OK, last time today (I hope). I got plugin testing to work, but I had to hack the cake_group_test.php to do it. test_task.php’s __addPluginModelTests function calls cake_group_test.php with an absolute path to the model test. cake_group_test’s addTestFile function is expecting a filename, not a file path. I changed the function to this, and it worked:

    if (file_exists(APP.’tests’.DS.$testFile)) {
    parent::addTestFile(APP.’tests’.DS.$testFile);
    }
    elseif (file_exists($testFile)) {
    parent::addTestFile($testFile);
    }

    hydra12

  • cakebaker

    @hydra12: Thanks for your testing. It was my bad, I forgot to update cake_group_test.php in the folder from which I generate the package :| I am sorry. I uploaded a new version.

    Btw: You could also send an email instead of putting the code as snippet to cakeforge.

  • hydra12

    Thanks! It’s working great now :-)

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License