ZenMagick unit testing
With ZenMagick 0.9.2 there is now an easy way to do automated testing.
The new zm_tests plugin comes with a number of tests that make sure that things do not get out of hand and to keep regressions low.
All testing is done in your browser and for those of you using IE, here is a screenshot of what you are missing out on
(I will try to improve the layout across browsers, but it’s a very low priority as it is working fine for me
Edit: Actually, fixing the validation errors and generally making the page look a bit cooler helped a lot! This means the current trunk version does look good in IE, too.
Tests are grouped based based on the directory the code is found in (one level only) and it is easy to select and run single tests, a particular group or all.
Looking at the image you can also see the group ‘@other’. Tests in that group do not come with the plugin but are tests written for other plugins.
The way it works, plugins can detect if the testing plugin is installed. If so, they are able to programmatically register new tests. That means everyone can use the testing framework to easily run their own tests.
So far the most important tests are possibly the price calculation tests. Those actually depend on the demo store data being available in the database, as at least attribute prices are compared to static data. I might be able to change that in the future, but that will involve some more digging into zen-cart code.
Unit tests are written using the great SimpleTests library. The main reasons I picked that particular one were:
- Easy to compress into a single file
- Web page testing out of the box
- Comes with a basic HTML report module
To be honest, other unit testing projects might provide the same, but after a few trials simple tests just fitted best into ZenMagick.
Expect more tests coming - I made a point of writing tests for most of the changes for 0.9.2. There will be more, in particular I plan to convert the included JMeter configuration into unit tests. There are a couple advantages in doing that:
- Single interface for all testing
- No other software required other than a browser
- Tests can incorporate brower actions plus database verification at the same time
I apologize if I do not use some of simple test’s unique features like autorun, but it seems a lot nicer this way
Finally, a simple example of how to write a new test:
- Create a new file with a class extending
UnitTestCase:
class TestSimpleResultList extends UnitTestCase { ... } - Add a method that starts with ‘test‘:
public function testSomething() { $this->assertTrue(1 == 1); } - Drop that new file in zenmagick/plugins/request/zm_tests/tests/misc
To use code to add a test (for another plugin), the following could be used in the plugins init() method::
if (null != ($tests = ZMPlugins::instance()->getPluginForId('zm_tests'))) {
// add class path only now to avoid errors due to missing UnitTestCase
ZMLoader::instance()->addPath($this->getPluginDir().'tests/');
$tests->addTest('TestZMTokens');
}

