ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


Testing C with Libtap
Pages: 1, 2, 3

Skipping Tests

Sometimes it is necessary to skip tests. For example, you might be testing an operation that only the root user can perform, or something that applies only to a particular platform. Using Test::More, you can create a block marked with the special label SKIP:



SKIP: { 
  skip 2, "because only root can foo()" 
    unless is_root(); 
  ok(foo(0), "root can foo(0)"); 
  ok(foo(1), "root can foo(1)"); 
}

With libtap you don't have this nice block structure, but the skip() function works similarly. It takes as its arguments the number of tests to skip and the string describing the reason to skip the tests in question. Here's the C version of the previous Perl example:

if (is_root()) { 
  ok(foo(0), "root can foo(0)"); 
  ok(foo(1), "root can foo(1)"); 
} 
else { 
  skip(2, "because only root can foo()"); 
}

Notice that you have to make sure to update the first argument passed to the skip() function in the !is_root() branch if the number of tests in the is_root() branch changes. This is easy to spot for simple cases like the above, but is harder for larger portions of code.

Libtap furnishes the skip_start() and skip_end macros, which provides a more Perl-ish way of skipping tests. If the first argument passed to skip_start() is true, libtap will skip all tests between it and the corresponding skip_end; that is, the code will compile but will not execute. You still have to make sure that the number passed as the second argument to skip_start() corresponds to the actual number of tests between it and skip_end, but at least you don't have to worry about two different branches of an if clause.

  skip_start(is_root(), 2, "because only root can foo()"); 
  ok(foo(0), "root can foo(0)"); 
  ok(foo(1), "root can foo(1)"); 
  skip_end; /* it's a macro: no parentheses */

Regardless of the method you use, when running as root, the output should look something like this:

1..2 
not ok 1 - root can foo(0) 
#     Failed test (skip.c:main() at line 12) 
ok 2 - root can foo(1) 
# Looks like you failed 1 tests of 2.

and something like this when running as a normal user:

1..2 
ok 1 # skip only root can foo() 
ok 2 # skip only root can foo()

TODO Tests

Occasionally you may want to incorporate a test that you expect to fail into your test suite. libtap supports TODO tests, which work well for this. They can be handy when you have a planned feature that you just don't have time to implement right now, but know how to test.

To write TODO tests in Perl, set the $TODO package variable to a true value. Test::More uses this value as the reason the tests are listed as not yet done.

ok(run(), "yay, it claims to start running!"); 
ok(is_running(), "it is running"); 
  
{ 
  local $TODO = "not sussed this part yet..."; 
  ok(stop(), "it appears to stop"); 
  ok(!is_running(), "it is not running"); 
}

In C it's almost the same, though you have to use a couple of function calls instead of a TODO block:

ok(run(), "yay, it claims to start running!"); 
ok(is_running(), "it is running"); 
  
todo_start("not sussed this part yet..."); 
ok(stop(), "it appears to stop"); 
ok(!is_running(), "it is not running"); 
todo_end();

Assume that the code you're testing reports that the program stopped when told to, but fails to actually stop. The output should look something like:

1..4 
ok 1 - yay, it claims to start running! 
ok 2 - it is running 
ok 3 - it appears to stop # TODO not done yet... 
not ok 4 - it is not running # TODO not done yet... 
#     Failed (TODO) test (todo.c:main() at line 17)

Pages: 1, 2, 3

Next Pagearrow





Sponsored by: