Day 4: Testing


Perl authors have a long tradition of shipping test cases with their modules, and in Perl 6 we plan to continue with that nice tradition.

And testing is very easy. The traditional perlish way is to print data in the Test Anything Protocol. But you don’t have to do that yourself, you can use a module for that.

Assume you have written a nice factorial function

 sub fac(Int $n) {
     [*] 1..$n

Currently it doesn’t matter for us how that function works – we want to find out if it does. So let’s test it:

 use v6;

 sub fac(Int $n) {
     [*] 1..$n

 use Test;
 plan 6;

 is fac(0), 1,  'fac(0) works';
 is fac(1), 1,  'fac(1) works';
 is fac(2), 2,  'fac(2) works';
 is fac(3), 6,  'fac(3) works';
 is fac(4), 24, 'fac(4) works';

 dies_ok { fac('oh noes i am a string') }, 'Can only call it with ints';

And let’s run it:

 $ perl6
 ok 1 - fac(0) works
 ok 2 - fac(1) works
 ok 3 - fac(2) works
 ok 4 - fac(3) works
 ok 5 - fac(4) works
 ok 6 - Can only call it with ints

In detail: use Test; loads the testing module, plan 6; declares that we plan to run six tests. Then five lines of the pattern is $got, $expected, $description follow. is() does string comparison, but since integers always stringify the same way, that’s fine.

Finally with dies_ok { $some_code }, $description we test that calling the function with a non-integer argument is a fatal error.

The output contains the test plan 1..6, followed by one line for each test. That starts with ok (or not ok if the test failed), the test number, space, dash, space and test description.

If you run more tests, you don’t want to look through every test output carefully, but you want a summary. The prove command from Perl 5 gives you such a summary:

 prove --exec perl6 .. ok
 All tests successful.
 Files=1, Tests=6, 11 wallclock secs ( 0.02 usr  0.00 sys + 10.26 cusr  0.17 csys = 10.45 CPU)
 Result: PASS

You can also put all your test files in a directory, let’s call it t/, and run prove recursively on all .t files in that dir:

 prove --exec perl6 -r t

Putting that line in your Makefile is also nice, so that you can just type
make test to run the tests.

Tags: ,

8 Responses to “Day 4: Testing”

  1. bened Says:

    Thanks, good one!

    What makes the script so slow? Is it the module-loading? Will the performance likely still be this bad in Rakudo*?

  2. Moritz Says:

    Rakudo is just generally slow, because the focus is on features and correctness first, and speed later.

    That said I hope that there will be some improvements before Rakudo Star is released, and if the parrot developers manage to build a good JIT system on top of LLVM until then it might be quite a bit faster.

    • hydra35 Says:

      To what extent Rakudo is slow? If Rakudo Start is intended to be a usable release, it must not be, say 1X~2X slower than perl 5?

  3. День 4: Тестирование | Ky6uk's Blog Says:

    […] Оригинальная статья. VN:F [1.7.8_1020]один момент…Rating: 0.0/10 (0 votes […]

  4. vamped Says:

    I had to use: $ prove –perl perl6

    –exec was not a spec’ed option

    • carl Says:

      Well, it is, but only in newer versions of Test::Harness. At least 3.10 has the ‘–exec’ option. You might want to download a newer version from CPAN.

  5. Perl 6 Advent Calendar на русском | Ky6uk's Blog Says:

    […] Perl 6 Advent Calendar на русском 8 Декабрь 2009 Ky6uk Написать комментарий К комментариям Разработчики проекта Rakudo, реализации языка Perl 6 на базе виртуальной машины Parrot, объявили о проведении предрождественского марафона, в рамках которого с 1 по 26 декабря в блоге каждый день будут публиковаться практические уроки, демонстрирующие те или иные возможности Perl 6. В первую неделю декабря в рамках цикла статей было рассказано об установке Rakudo, использованию циклов, операторах форматирования, статических типах, мета-операторах и приемах тестирования. […]

  6. 扶凯 » [Perl6]测试 Says:

    […] 本文为译文原作者 By Moritz: […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 50 other followers

%d bloggers like this: