Development

LimeTestingFramework

You must first sign up to be able to contribute.

New Lime Testing Framework

Overview

On January 17, 2006 a sample chapter from the upcoming book The Definitive Guide to Symfony was posted to the official symfony blog (direct link to pdf).

This page is to serve as a source of additional information, documentation, tips, tricks, and examples pertaining to the new Lime testing framework that are not present in the book.

Cheat Sheet

See downloadable lime cheat sheet at the bottom of this page.

Available Methods

The following (unofficial) documentation is a list of the methods available and their usage. Since lime is still in beta, these can change meaning or be completely dropped before final release.

Most methods will have a $message argument. Text here will be printed out if the file is called directly via php.

ok()

ok($exp, $message = '')

Tests $exp for true. Test will fail if $exp, when casted to a boolean value, is not true. This should still work if $exp results in a string or any number that isn't 0, as 0 will be treated as false. If you need to test for 0, use is() instead.

$some_var = 'hello';
$t->ok(isset($some_var), '$some_var should exist'); //test passes
unset($some_var)
$t->ok(isset($some_var), '$some_var should exist'); //test fails

is()

is($exp1, $exp2, $message = '')

Compares $exp1 to $exp2. Test will fail if $exp1 and $exp2 do not match. Types are not taken into effect (something stored as numeric can be compared to a string of numbers)

$t->is(42, "42", 'The meaning of life, the universe, and everything, is 42'); //test passes
$t->is(1, TRUE, '1 is the same as true');                                     //test passes

isnt()

isnt($exp1, $exp2, $message = '')

The opposite of is(), test will fail if $exp1 and $exp2 DO match.

$t->isnt(6*9, 42, 'The question can not be "what do you get if multiply six by nine"'); //test passes
$t->isnt(base_convert(6*9, 10, 13), 42, 'But it can if the numbers are base 13');       //test fails

Editorial note: Yeah I'm starting to get carried away with these "Hitch Hiker's Guide to the Galaxy" references so let me explain that last test. 6*9, in decimal (base 10) equals 54. But 6*9 in base 13 does indeed equal 42. base_convert() converts the result of 6*9 from base 10 to base 13. Example code is supposed to be very basic but couldn't help myself here. -todd

like()

like($exp, $regex, $message = '')

Searches $exp using the regular expression $regex. Test passes if $regex is found in $exp.

$text = "The quick brown fox jumped over the lazy dog.";
$t->like($text, '/quick/', '$text contains "quick"'); //test passes
$t->like($text, '/QUICK/', '$text contains "QUICK"'); //test fails
$t->like($text, '/QUICK/i', '$text contains "quick" without regards to case'); //test passes

unlike()

unlike($exp, $regex, $message = '')

Searches $exp using the regular expression $regex. Test passes if $regex IS NOT found in $exp.

$text = "The quick brown fox jumped over the lazy dog.";
$t->unlike($text, '/green fox/', 'there is no green fox'); //test passes
$t->unlike($text, '/lazy dog/', 'dogs are not lazy');      //test fails

cmp_ok()

cmp_ok($exp1, $op, $exp2, $message = '')

Compares $exp1 to $exp2 using the operator $op. Test passes if the result is true.

$t->cmp_ok(5, '>', 3, '5 is greater than 3');  //test passes
$t->cmp_ok(5, '<', 3, '5 is less than 3');     //test fails
$t->cmp_ok('hello', '==', strtolower('HELLO'), 'strtolower() makes a string lowercase');  //test passes

can_ok()

can_ok($object, $methods, $message = '')

Checks the object $object for the method(s) $methods. $methods can be an array containing multiple methods. Test will fail if 1 or more of the methods specified in $methods does not exist.

A simple autoloader was added in v1.0beta2 (see r2875) which allows you to autoload classes, just like in an actual project.

$testSfConfig = new sfConfig();
$t->can_ok($testSfConfig, 'add', 'sfConfig() contains the add method'); //test passes
$t->can_ok($testSfConfig, array('add', 'get'), 'sfConfig() contains add and get methods'); //test passes
$t->can_ok($testSfConfig, array('add', 'get', 'create'), 'sfConfig() contains add, get, and create methods'); //test fails

isa_ok()

isa_ok($var, $class, $message = '')

This tests that variable $var is the class $class. An interesting side note is when $var is not an object, then this will test that $var is the type specified by $class. I believe this was done only for error reporting on failure of detecting a class name. A more efficient way of checking data type is to use the ok() function. $t->ok(is_numeric(5), '5 is a number'); is more efficient than using isa_ok().

$testSfConfig = new sfConfig();
$t->isa_ok($testSfConfig, 'sfConfig', '$testSfConfig is the class sfConfig');       //test passes
$testSfConfig->example = 'some text';
$t->isa_ok($testSfConfig->example, 'sfConfig', '$testSfConfig->example is in the class sfConfig'); //test fails
$t->isa_ok($testSfConfig->example, 'string', '$testSfConfig->example is a string'); //test passes

is_deeply()

is_deeply($exp1, $exp2, $message = '')

Intended for arrays, but works with anything. This will check that $exp1 exactly matches $exp2 in data type and value. If an array is being compared, it will that the values in both arrays are exact. It even handles multidimensional arrays (arrays within an array). If any value doesn't match, test will fail.

$baseArray = array('red','blue',array(1,2));
$t->is_deeply($baseArray, array('red','blue',array(1,2)), 'comparing arrays');      //test passes
$t->is_deeply($baseArray, array('red','blue',1,2), 'comparing arrays');             //test fails
$t->is_deeply($baseArray, array('red','blue',array(2,1)), 'must be in same order'); //test fails

diag(), comment(), and todo()

diag($message)
comment($message)
todo($message = '')

Although they function slightly different they all basically do the same thing, output text. diag() is used in all error messages that get printed out when a test fails. comment() does the exact same thing, but just displays slightly different if color isn't being used. If using color they both look exactly the same. Typically you would use diag() to say each function name before you write the tests for it. comment() is used for anything else you wanted mention. Neither of these increment the count of tests that are run. If you have five tests, two comments, and a diag message, it will still say that 5 tests were run.

todo() is the most different. It acts very similar. This means that it does count as a test. The message will be prefixed "# TODO ". Also note that the message for todo() is optional but is required for diag() and comment().

These message will show up if you provide the testname as an argument.

skip()

skip($message = '', $nb_tests = 1)

Will skip the next $nb_tests number of tests. The skip() method itself doesn't increase the test count. Depending on the number of tests you say to skip, it will go through and instead of display the text associated with the test, it will display "# SKIP $message" for each test that is skipped. So if you skip 5 tests, you will see the skip message 5 times.

include_ok()

include_ok($file, $message = '')

Makes an attempt to include the file $file. Test will fail if it is unable to.

$t->include_ok('/some/nonexistent/file', 'Include a fake file'); // test fails
$t->include_ok('/existing/file', 'Include a real file');         // test passes

Documentation

Source

Code Examples

Relevant API Docs

Attachments