Learning Perl Objects, References amp;amp; Modules [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Learning Perl Objects, References amp;amp; Modules [Electronic resources] - نسخه متنی

Randal L. Schwartz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید














14.2 Writing Tests with Test::Simple


The
Test::Simple module is included with the Perl
distribution, starting in Perl 5.8.[7]

[7] Older Perl
versions back to 5.004_03 can install the same module from the
CPAN.


Test::Simple automates
the boring task of writing "ok 1",
"ok 2", "ok
3", and so on, in your program.
Test::Simple exports one subroutine, called
(appropriately) ok. It's best
illustrated by example. For the earlier code, you can rewrite it as:

use Test::Simple tests => 4;
ok(1 + 2 = = 3, '1 + 2 = = 3');
ok(2 * 4 = = 8, '2 * 4 = = 8');
my $divide = 5 / 3;
ok(abs($divide - 1.666667) < 0.001, '5 / 3 = = (approx) 1.666667');
my $subtract = -3 + 3;
ok(($subtract eq "0" or $subtract eq "-0"), '-3 + 3 = = 0');

Ahh. So much simpler. The use not only pulls the
module in but also defines the number of tests. This generates the
1..4 header. Each ok test
evaluates its first argument. If the argument is true, it prints the
proper ok message. If not, it prints the proper
not ok message. For this
particular example, the output looks like:[8]

[8] Don't be misled when reading the mathematics
of the output. The first number and the dash on each
ok line are just labels; Perl
isn't telling you that 1
- 1 +
2 = =
3!


1..4
ok 1 - 1 + 2 = = 3
ok 2 - 2 * 4 = = 8
ok 3 - 5 / 3 = = (approx) 1.666667
ok 4 - -3 + 3 = = 0

The ok N messages are followed
with the labels given as the second parameters. This is great for
identifying each test, especially because the numbers 1 through 4
don't appear in the original test anymore. The test
harness ignores this information, unless you invoke
make test with
make test
TEST_VERBOSE=1, in which case, the information is
displayed for each test.

What if a test fails? If you change the first test to
1 + 2
= = 4, you get:

1..4
not ok 1 - 1 + 2 = = 4
# Failed test (1.t at line 4)
ok 2 - 2 * 4 = = 8
ok 3 - 5 / 3 = = (approx) 1.666667
ok 4 - -3 + 3 = = 0
# Looks like you failed 1 tests of 4.

The ok
1 became not
ok 1. But also notice the extra
message indicating the failed test, including its file and line
number. Messages preceded by a pound-sign comment marker are merely
comments, and are (mostly) ignored by the test harness.

For many people,
Test::Simple is simple enough to use for a wide
range of tests. However, as your Perl hackery evolves,
you'll want to step up to the next level of Perl
testing hackery as well, with Test::More.



/ 199