Simple TDD with atoum

I will use atoum because I wrote about it some times ago, Unit test with atoum installed with composer. This post will present a ruby script which is compatible with folder and files structures. It’s a very simple script so it will be easy to adapt to PHPUnit if you need.

The loop mode in atoum is usefull but we need to press enter in the terminal all the time to run tests. It works of course very well but I think it’s better to monitor classes and execute appropriate tests automatically.

Install Watchr and create tdd.rb file

$ gem install watchr
$ cd ~/test-project
$ vi tdd.rb

Structure

.
├── bin
│   └── atoum -> ../vendor/mageekguy/atoum/bin/atoum
├── classes
│   └── helloWorld.php
├── composer.json
├── composer.lock
├── composer.phar
├── tdd.rb
├── tests
│   └── units
│       └── testHelloWorld.php
└── vendor
    ...

tdd.rb file

def clear_console
  puts "\e[H\e[2J"
end

def run_test(file)
    clear_console

    unless File.exist?(file)
        puts "#{file} does not exist"
        return
    end
 
    puts "Running: #{file}\n\n"
    result = `php bin/atoum -ulr -f #{file}`
    puts result
end

watch("classes/(.*).php") do |match|
    run_test %{tests/units/#{match[1]}.php}
end

watch("tests/units/(.*).php") do |match|
    run_test match[0]
end

clear_console
puts "Simple TDD with atoum !\n\n"

Execute tdd.rb

$ watchr ./tdd.rb

Edit classes/helloWorld.php or tests/units/testHelloWorld.php

Screenshots

Ready: 'TDD with atoum - monitor classes' Success: 'TDD with atoum - sucess in terminal' Failure: 'TDD with atoum - sucess in terminal'

Improvements

  • More flexible script (classes folder and tests folder)
  • Notification via Growl, Inotify, Dbus …
  • Try testy which is very complete

As you can see it’s a very simple way to work in TDD. Simple but work really good :)

Antoine Corcy

Developer / Copenhagen

Copenhagen, Denmark http://sbin.dk