Test-Simple-0.98 > Test::Simple

名前

Test::Simple - Basic utilities for writing tests.

Test::Simple - テストを書くための基本的なユーティリティ

概要

  use Test::Simple tests => 1;

  ok( $foo eq $bar, 'foo is bar' );

説明

** If you are unfamiliar with testing read Test::Tutorial first! **

** テストに馴染がないなら、まず、Test::Tutorial を読んで下さい! **

This is an extremely simple, extremely basic module for writing tests suitable for CPAN modules and other pursuits. If you wish to do more complicated testing, use the Test::More module (a drop-in replacement for this one).

これは、CPAN モジュールや、他の目的のためにテストを書くのに、 極めて簡単な、極めて基本的なモジュールです。 もし、より複雑なテストを望むなら、 (置き換え可能な実装である)Test::More モジュールを使って下さい。

The basic unit of Perl testing is the ok. For each thing you want to test your program will print out an "ok" or "not ok" to indicate pass or fail. You do this with the ok() function (see below).

Perl のテストの基本的なユニットは、okです。 テストしたいものそれぞれについて、 プログラムは、「ok」か、「not ok」を出力し、合格したか、失敗したかを示します。 ok() 関数で、これを行います(下を見てください)。

The only other constraint is you must pre-declare how many tests you plan to run. This is in case something goes horribly wrong during the test and your test program aborts, or skips a test or whatever. You do this like so:

唯一の他の強制は、行おうと計画しているテストが、いくつであるかを先に 宣言しなければならないことです。 この宣言は、何かがテストの間に恐ろしい問題になり、テストのプログラムが 中断するか、テストをスキップするか、何かそういったものが起こる場合のために あります。 このようにします:

    use Test::Simple tests => 23;

You must have a plan.

計画が必要です。

ok
  ok( $foo eq $bar, $name );
  ok( $foo eq $bar );

ok() is given an expression (in this case $foo eq $bar). If it's true, the test passed. If it's false, it didn't. That's about it.

ok() には式が与えられています(この場合、$foo eq $bar)。 与えられた式が、真なら、テストは合格です。 与えられた式が、偽なら、 合格ではありません。 だいたいそれくらいです。

ok() prints out either "ok" or "not ok" along with a test number (it keeps track of that for you).

ok() は、テストの番号と一緒に、「ok」か「not ok」のどちらかを出力します (後を追い続けます)。

  # This produces "ok 1 - Hell not yet frozen over" (or not ok)
  ok( get_temperature($hell) > 0, 'Hell not yet frozen over' );

If you provide a $name, that will be printed along with the "ok/not ok" to make it easier to find your test when if fails (just search for the name). It also makes it easier for the next guy to understand what your test is for. It's highly recommended you use test names.

もし、$nameを引数に与えるなら、$nameは、「ok/not ok」と一緒に出力され、 テストが失敗した場合に、テストを見つけるのが簡単になります(ただ名前だけで 探せます)。 また、こうしておけば、他の人が、あなたのテストが何のためにあるのかを 理解するのが簡単になります。 テストの名前を用いることは、強く推奨されます。

All tests are run in scalar context. So this:

全てのテストは、スカラーコンテキストで動きます。つまり、これは:

    ok( @stuff, 'I have some stuff' );

will do what you mean (fail if stuff is empty)

あなたの意図するように動きます(stuffが空なら失敗します)。

Test::Simple will start by printing number of tests run in the form "1..M" (so "1..5" means you're going to run 5 tests). This strange format lets Test::Harness know how many tests you plan on running in case something goes horribly wrong.

Test::Simple は、「1..M」と言う形式で、テストの動く番号をプリントすることで、 始まります(つまり、「1..5」は、5つのテストを動かすという意味です)。 この、変わったフォーマットは、何かが恐ろしく間違っているような場合に、 実行中の計画がいくつあるかを、Test::Harnessに知らせます。

If all your tests passed, Test::Simple will exit with zero (which is normal). If anything failed it will exit with how many failed. If you run less (or more) tests than you planned, the missing (or extras) will be considered failures. If no tests were ever run Test::Simple will throw a warning and exit with 255. If the test died, even after having successfully completed all its tests, it will still be considered a failure and will exit with 255.

もし、全てのテストが合格すれば、Test::Simpleは、0で、終えます (これは普通です)。 何かが間違っていれば、間違ったか数で終了します。 計画したのより、少ない(または、多くの)テストが実行された場合、 見あたらないもの(か余分なもの)があるため、計画は失敗したとみなされます。 もし、テストが実行されなければ、Test::Simpleは、警告を投げて、 255 で終えます。 全てのテストがちゃんと完全にされた後でも、テストが die すれば、 それがまだ失敗であると考えられ、255 で終えます。

So the exit codes are...

つまり、終了コードは…

    0                   all tests successful
    255                 test died or all passed but wrong # of tests run
    any other number    how many failed (including missing or extras)
    0                   全てのテストが成功
    255                 テストが死んだか全部通ったが何かおかしい # テストの実行について
    any other number    いくつ失敗したか(足りないもの、余計なものを含む)

If you fail more than 254 tests, it will be reported as 254.

254 以上のテストが失敗した場合は、254 として、報告されます。

This module is by no means trying to be a complete testing system. It's just to get you started. Once you're off the ground its recommended you look at Test::More.

このモジュールは、決して、完全なテストシステムであろうとはしません。 このモジュールは、ただ、あなたにテストを始めさせる切っ掛けにすぎません。 一旦テストを始めたならば、Test::More を見ることをお奨めします。

Here's an example of a simple .t file for the fictional Film module.

架空の Film モジュールのための簡単な、.t ファイルの例をあげます。

    use Test::Simple tests => 5;

    use Film;  # What you're testing.

    my $btaste = Film->new({ Title    => 'Bad Taste',
                             Director => 'Peter Jackson',
                             Rating   => 'R',
                             NumExplodingSheep => 1
                           });
    ok( defined($btaste) && ref $btaste eq 'Film',     'new() works' );

    ok( $btaste->Title      eq 'Bad Taste',     'Title() get'    );
    ok( $btaste->Director   eq 'Peter Jackson', 'Director() get' );
    ok( $btaste->Rating     eq 'R',             'Rating() get'   );
    ok( $btaste->NumExplodingSheep == 1,        'NumExplodingSheep() get' );

It will produce output like this:

これは、このように出力します:

    1..5
    ok 1 - new() works
    ok 2 - Title() get
    ok 3 - Director() get
    not ok 4 - Rating() get
    #   Failed test 'Rating() get'
    #   in t/film.t at line 14.
    ok 5 - NumExplodingSheep() get
    # Looks like you failed 1 tests of 5

Indicating the Film::Rating() method is broken.

Film::Rating() メソッドが壊れている事を示しています。

警告

Test::Simple will only report a maximum of 254 failures in its exit code. If this is a problem, you probably have a huge test script. Split it into multiple files. (Otherwise blame the Unix folks for using an unsigned short integer as the exit status).

Test::Simple は、その終了コードで、最大で 254 の失敗を報告するだけです。 このことが問題になるのなら、おそらく、莫大なテストスクリプトなのでしょう。 そのテストスクリプトを複数のファイルに分割して下さい。 (もしくは、終了状態として、符号なしの短い整数を使う UNIX の人々を 非難して下さい)。

Because VMS's exit codes are much, much different than the rest of the universe, and perl does horrible mangling to them that gets in my way, it works like this on VMS.

VMS の終了コードはそれ以外の世界とは、もっと、もっと違っており、 自分のやり方を得ている Perl は恐ろしくその終了コードを押しつぶすので、 VMS では、次のように動きます。

    0     SS$_NORMAL        all tests successful
    4     SS$_ABORT         something went wrong
    0     SS$_NORMAL        全てのテストは成功
    4     SS$_ABORT         何かが間違っています

Unfortunately, I can't differentiate any further.

残念なことに、これ以上切り分けできません。

注意

Test::Simple is explicitly tested all the way back to perl 5.6.0.

Test::Simple は、perl 5.6.0 まで、はっきりと テストされます。

Test::Simple is thread-safe in perl 5.8.1 and up.

Test::Simple は、perl 5.8.1 以上で、スレッドセーフです。

経緯

This module was conceived while talking with Tony Bowden in his kitchen one night about the problems I was having writing some really complicated feature into the new Testing module. He observed that the main problem is not dealing with these edge cases but that people hate to write tests at all. What was needed was a dead simple module that took all the hard work out of testing and was really, really easy to learn. Paul Johnson simultaneously had this idea (unfortunately, he wasn't in Tony's kitchen). This is it.

このモジュールは、ある夜、Tony Bowden と一緒に、彼の家のキッチンで、 私の抱えていた新しいテストのモジュールに本当に複雑な特徴を書くという 問題について話している途中に、思いつきました。 彼は、主な問題は、これらのエッジケースを処理することではなく、 人々がテストを書くのを嫌っている事につきると見ていました。 必要とされているのは、テストから、全てのきつい仕事を取り出した、実に、 本当に、学びやすい、全く簡単なモジュールでした。 Paul Johnson は、同時にこのアイディアを持っていました(が、不幸なことに、 彼は、Tony のキッチンにいなかった)。 これこそが、そのまったく簡単なテストのモジュールです。

SEE ALSO

Test::More

More testing functions! Once you outgrow Test::Simple, look at Test::More. Test::Simple is 100% forward compatible with Test::More (i.e. you can just use Test::More instead of Test::Simple in your programs and things will still work).

テストの機能がより多い! Test::Simple を卒業したら、Test::More を見てください。 Test::Simple は、Test::More と、100% 前方互換性があります。 (たとえば、プログラムで、Test::Simple の代わりに、Test::More を 使うことが出来ますし、それはそのまま動きます)。

Look in Test::More's SEE ALSO for more testing modules.

さらなるテストモジュールについては Test::More の SEE ALSO を見てください。

作者

Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern <schwern@pobox.com>, wardrobe by Calvin Klein.

コピーライト

Copyright 2001-2008 by Michael G Schwern <schwern@pobox.com>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html