Blogs / Jean-Luc Chassaing / writing and performing tests

writing and performing tests

Sunday 14 November 2010 1:19:17 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

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...


				            

Blog Post Discussion

writing and performing tests

eZ debug

Timing: Jan 17 2025 23:35:10
Script start
Timing: Jan 17 2025 23:35:10
Module start 'content'
Timing: Jan 17 2025 23:35:10
Module end 'content'
Timing: Jan 17 2025 23:35:11
Script end

Main resources:

Total runtime0.1457 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0063 587.7344180.8359
Module start 'content' 0.00640.0058 768.570399.3281
Module end 'content' 0.01220.1335 867.8984532.0547
Script end 0.1457  1,399.9531 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00332.2984200.0002
Check MTime0.00130.9123200.0001
Mysql Total
Database connection0.00060.443310.0006
Mysqli_queries0.101769.78021410.0007
Looping result0.00140.95701390.0000
Template Total0.133191.410.1331
Template load0.00090.616210.0009
Template processing0.132290.733110.1322
Override
Cache load0.00060.425310.0006
Sytem overhead
Fetch class attribute can translate value0.00100.717010.0010
XML
Image XML parsing0.00030.218910.0003
General
dbfile0.00251.7242200.0001
String conversion0.00000.007530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs