Blogs / Jean-Luc Chassaing / Unit testing tips and tricks

Unit testing tips and tricks

Sunday 21 November 2010 2:19:21 pm

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

By : Jean-Luc Chassaing

Ok, I 've been taken by other problems but it's time for me to give you the last post and yet the most important part of three, dedicated to unit testing on eZ Publish.

In the two previous posts, I've detailed how to get a unit testing environment and in the second one I gave you some clues to begin writing your first tests.

the two previous posts on the subject are :

It's time for me to give you a few time saving clues. Don't think you'll find here all about unit testing. My goal is to help go true some problems I've had to face and save your time by the way.

I'd like to come back to the suite.php script of my previous post.

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 unction suite()
    {
        return new self();    
    }
}

So imaging you've written your own extension and as you make some special things out of ez, your written a few classes and you want to perform unit testing on them. So you write your test cases and register them in a test suite. As you make real special thing you need to have access to the database. And to do so, you've used the ezpDatabaseTestSuite and ezpDatabaseTestCase for the suite class and the test cases.

Let's see how we'll run the test :

php test/runtests.php -D mysql://user@localhost/mydb extension/myextension

ok so here we tell runtests wich database to use to perform the tests. Here is my firs advise. Just use an empty database for your tests. When you starts performing a test sequence using a database connection, one of the first things that is done is to set up the database injecting the defaut system schema and database scripts. Just for memory does scripts are located in kernel/sql/...

So that leads us back to the suit.php script just above. In fact it would be good to have your own database (with your bran new classes) injected in the tests dedicated database. So here's my second advise use those two lines :

$this->insertDefaultData= false;
$this->sqlFiles= array('my_schema.sql','my_data.sql');

the first line just tells that we don't want the default database to be loaded and so we give our specific sql scripts.

So now things are rather good. You can play all your Unit test as long as you want and get them performed on your database.

Let's keep going on the datase stuff. know you have some tests written and there's a new one to come that need a specific table. And in fact you don't want to rebuild new schema and data sql exports.

In this case, you can add the next line in your test case script :

ezpTestDatabaseHelper::insertSqlData( $this->sharedFixture, array('extension/gcusermanager/sql/mysql/schema.sql') );

Just take care to add a blank line at the end of all your sql scripts. And by the way I'd like to thanks Bertrand for his posts here who where helpful.

An other thing very useful when writing tests is to have data provider functions. If you want them to work you'll have to take care of how you write your test and suite class constructors.

so for your testCase it should look like something like that :

public function __construct($name = NULL, array $data = array(), $dataName = '')
{


parent::__construct($name,$data,$dataName);

...

Well .. it look like this post suffers of a lack of structure. However, I hope it will help does of you who, as I did, want to perform unit test on their scripts.

Blog Post Discussion

Unit testing tips and tricks

eZ debug

Timing: Jan 18 2025 00:06:07
Script start
Timing: Jan 18 2025 00:06:07
Module start 'content'
Timing: Jan 18 2025 00:06:07
Module end 'content'
Timing: Jan 18 2025 00:06:08
Script end

Main resources:

Total runtime0.1535 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0069 592.0078180.8359
Module start 'content' 0.00690.0048 772.843899.3281
Module end 'content' 0.01170.1417 872.1719531.8672
Script end 0.1535  1,404.0391 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00362.3145200.0002
Check MTime0.00150.9538200.0001
Mysql Total
Database connection0.00110.747110.0011
Mysqli_queries0.109171.07981410.0008
Looping result0.00130.83551390.0000
Template Total0.141592.210.1415
Template load0.00060.385010.0006
Template processing0.140991.781810.1409
Override
Cache load0.00040.266510.0004
Sytem overhead
Fetch class attribute can translate value0.00090.562810.0009
XML
Image XML parsing0.00030.202510.0003
General
dbfile0.00332.1366200.0002
String conversion0.00000.002630.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