dracoblue.net

Assertions and No-Test-Framework in PHP

When I was writing the tests for

Craur, I wanted to try a new way (no phpunit this time) to write tests in php. Since php already has a assert($assertion) method, I imagined that it might be a good start to test without an extra test framework. A simple

assert(false)

raised a warning. Nice! So I made the following approach:

tests/
    my_test_file.php
    another_test.php

Whenever I want to launch the tests, I just go into the tests folder and launch php $file_name for all of them. If the exit code is different to 0 -> the test failed. It's a pitty, bit

assert($assertion) does not raise an exception or something like this. Great for other use cases, but in mine this was really an issue. So I wrote this small wrapper script bootstrap_for_test.php. It registers a new AssertionException class for all kind of errors and as ASSERT_CALLBACK! Now even a notice or a warning make the test fail. Great! Now a simple test looks like this:

<?php
include('./../bootstrap_for_test.php');
assert(2 == 3);

To have the tests run at once, I wrote a little

run_test.sh script. It runs every test and returns the script with an exit code different to 0, if one of the tests fails.

All integrated with Travis CI and Craur is tested!

In open source, php by DracoBlue @ 05 Mar 2012

comments powered by Disqus

Recent Files

Advertisement

Recent Dev-Articles

About

Blogroll