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