Starting to get addicted to unit testing…

During the last months, I started paying more attention to unit testing and writing tests in my symfony apps. But not until this awesome post from Bernhard Schussek, I really understood how you can make your testing life easier and get tests done better, faster and be more efficient.

It also helped me to understand, to which granularity you can and should break down your tests as well as your methods in the lib classes.

So this really is a BIG FAT recommendation on reading the article!!

What I learned from it and what I am doing ad enjoying right now, when creating new functionality:

  • If I have a complex method to write, I break it down into the smaller parts that need to be done step by step. For this, I write the logic with methods that are not even exisiting yet.
  • Next, I write the unit TESTS for these non-exisiting methods, filled with test cases that make sense in the current context of the problem and help to make sure the method works
  • Next, create an empty stub of the new method, so you can start running the test without a crash – but of course it will fail
  • Add functionality to the method to make sure you finally pass all tests.

This really makes the whole process a lot of fun because it really is rewarding to see the “All tests successful.” or “# Looks like everything went fine.” at the end.

And you can continue with your next part of the more complex functions with a good feeling of confidence, that in the end it will all work fine 😀

How to configure symfony unit tests to use an sqlite memory database

Since Bernard Schussecks presentation at “symfony Day Cologne”, talking about lime and using sqlite memory tables for unit tests, I was wondering HOW TO DO IT

Well, there was not much talk about the how… of course this must be known to everybody – such an easy task. (Find yourself some <irony> tags to put around this).

Well, at least I did not have a clue how to do it and did not find too much information on it in the web…

So, spending some more time with testing and some rare tutorials on it, I finally made it work!!

And for you and me and everybod else, I’ll note it down for reference…

Assumptions/Prerequisits:

  • We are using symfony and doctrine
  • In test/fixtures we have a/some yml files with test data

First, we need to set up the test connection to use sqlite:MEMORY:

In your config/database.yml add

test:
  sqlitetest:
    class: sfDoctrineDatabase
    param:
      classname: DebugPDO
      dsn: 'sqlite::memory:'

(yes, ::)

Now, lets assume (accoding to the Jobeet tutorial Day 8 ) we have a

test/bootstrap/Doctrine.php

Now, the important part is to not only LOAD the data but first create the tables:

  include(dirname(__FILE__).'/unit.php');
  $configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);
  new sfDatabaseManager($configuration);
  Doctrine::createTablesFromModels(dirname(__FILE__).'/../../lib/model');
  Doctrine::loadData(sfConfig::get('sf_test_dir').'/fixtures');

Now, in our test files we can simply call

include(dirname(__FILE__).'/../../bootstrap/Doctrine.php');
$t = new lime_test(4);  //change it with the number of tests you have 

everytime
Took me quite some time to figure it out – really simpe – if you know how…

Mein erster Unit Test

Original Date: 2009-09-08

Peinlich, oder??

Da entwickel ich seit Jahren so vor mich hin, aber heut hab ich zum ersten mal bewusst einen Unit Test für symfony mit lime geschrieben. Und das krasse: DAS MACHT SOGAR SPASS.

Es ist echt toll zu sehen, wie schön das ist, wenn ne Funktion das tut was sie soll bzw. kann auch echt hilfreich sein, um Fehler zu finden – wenn etwas nicht so is, wie’s sein sollte.

Albern, oder? Und doch so klar?

Aber ich vermute, ich bin nicht der einzige da draußen, der sich für einen tollen Programmierer hält, aber auch noch nie ordentlich was mit “richtigem” Testen gemacht hat… probieren – und wenn’s tut, tut’s… Denkste…

Wie dem auch sei, ich bin zufrieden. Hab erst mal nur eine Klasse getestet, aber is ja immer so: Erst das Prinzip verstehen, verbessern, und dann geht’s richtig los.

So, und mit dem neugeweonnenen Selbstvertrauen in meine Funktion für den Karo-Chat 2.0 hab ich den jetzt sogar auf das ECHTE Chatfile losgelassen. Und siehe da – es klappt!

Der neue Chat kann jetzt richtig schön AJAXen, neue Nachrichten faden ein, alte raus – herrlich…

Mal sehen, was als nächstes kommt. Vermutlich sollte ich mal eine ganze Ladung an statischen Testdaten für die Karo-Datenbank zusammenstellen, um da auchne zuverlässige basis für Tests zu haben?

Jaa, das sollte mal her.

Ach, nebenbei hab ich den von Quabla gemeldeten Bug gefixt: Bei Spielen “mit Checkpoints” aber keinen Checkpoints auf der Karte konnte man gleich ins Ziel – soll ja nich, geht jetzt auch nich mehr…