writing and performing tests

Sunday 14 November 2010 1:19:17 am

By : Jean-Luc Chassaing

In my last post, I described how to get a testing environment for your eZ Publish developments. This was in my previous post.

Now it's time to go further on write your first test and run it.

This is not a PHPUnit tutorial the goal is to give you a first step and some clues to avoid brainstorming gapes.

Ok so lets try and write our first test.

1 / First steps

1.1 / the folder structure

The statement is, you're coding your new extension called mynewextension. As your are making some special things out of eZ Publish, you've written your own class MyClass. So the folder structure of your extension should look like this :

mynewextension
|__ classes
|__ myclass.php

...

So first you'll have to create a tests folder where all your tests will be. Then replicate your code folder structure in your test folder. so things should look like :

mynewextension
|___ classes
| |__ myclass.php
|___ tests
|__ classes
| |__ myclass_test.php
|__ suite.php
...

So as you can see, I've added a tests folder with a classes folder where is the test case for my class : myclass_test.php and then in the tests folder there is a suite.php script.

1.2 / The scripts

So to write an run your first test, you need two files :

  • suite.php
  • myclass_test.php

1.2.1 / The suite

suite.php must extend ezpTestSuite or ezpDatabaseTestSuite if you need database access for your test which could be the case in many situations. So your suite.php script should look like this :

class MyFirstTestSuite extends ezpDatabaseTestSuite
{ 
    public function __construct()
    {
        parent::__construct();
        $this->insertDefaultData= false;
        $this->sqlFiles= array('my_schema.sql','my_data.sql');
        $this->setName( "My Own Test Suite" );
        $this->addTestSuite( 'MyClassTest' );
    }
    public static function suite()
    {
        return new self();    
    }
}

Ok so, the script above show a suite.php. I've made the choice to show you a suite script extending ezpDatabaseTestSuite because it will be the most interesting. I'll give you more details about databases below. At this point the important line is :

this->addTestSuite( 'MyClassTest' );

This is how to register your test case in the test suite.

1.2.2 / The test

On the same base as the suite, the test case will extend the ezpTestCase class or the ezpDatabaseTestCase. As you can gesse, we will use the second one in our example.

So our testCase will look like this :

class MyClassTest extends ezpDatabaseTestCase
{ 
  /**
  * @dataProvider provider
  */ 
   public function testAdd($a, $b, $c)
   {        
        $this->assertEquals($c, $a + $b);
   }
   public function provider()
   {        
        return array(array(0, 0, 0),
                     array(0, 1, 1),
                     array(1, 0, 1),
                     array(1, 1, 3));
    }
}

so that's it. I must be frank with you the test I wrote above is taken from the phpunit documentation.

Ok so now we have a suite and a testcase we can run them and sell how it works. I'll have some things to tell you.

Just before lets perform our first test. Create a database on your development server data will be dedicated to your test we'll say it's called testdb. know here's how to run the tests :

php tests/runtest.php -D mysql://dbuser@localhost/testdb extension/mynewextension

Ok I'll make a stop here... the second part will be in the next post...


				            
Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 29 2025 23:48:45
Script start
Timing: Jan 29 2025 23:48:45
Module start 'layout'
Timing: Jan 29 2025 23:48:45
Module start 'content'
Timing: Jan 29 2025 23:48:45
Module end 'content'
Timing: Jan 29 2025 23:48:45
Script end

Main resources:

Total runtime0.0878 sec
Peak memory usage4,096.0000 KB
Database Queries42

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0049 588.1563151.2109
Module start 'layout' 0.00490.0024 739.367236.6563
Module start 'content' 0.00740.0795 776.0234376.0391
Module end 'content' 0.08690.0009 1,152.062515.3906
Script end 0.0878  1,167.4531 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00303.4259150.0002
Check MTime0.00121.3906150.0001
Mysql Total
Database connection0.00070.805110.0007
Mysqli_queries0.034138.8130420.0008
Looping result0.00020.2628400.0000
Template Total0.059367.520.0296
Template load0.00252.829220.0012
Template processing0.056864.649220.0284
Template load and register function0.00010.113710.0001
states
state_id_array0.00091.000810.0009
state_identifier_array0.00060.727720.0003
Override
Cache load0.00171.8917470.0000
Sytem overhead
Fetch class attribute name0.00171.962830.0006
class_abstraction
Instantiating content class attribute0.00000.009030.0000
General
dbfile0.00161.7918100.0002
String conversion0.00000.007940.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplblog_entry/full.tplextension/community_design/design/suncana/override/templates/blog_entry/full.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
12content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/header.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/header.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezxmltags/li.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/li.tplEdit templateOverride template
1content/datatype/view/ezxmltags/ul.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/ul.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezkeyword.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezkeyword.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 30
 Number of unique templates used: 10

Time used to render debug report: 0.0001 secs