Документация
HTML CSS PHP PERL другое

Section 17.2.  A Simple Test Script

 
Previous
Table of Contents
Next

17.2. A Simple Test Script

Before we go on, let's write some Perl code. We just told you about creating the tests before you write the code, so maybe we should follow our own advice. We'll get to the details later, but for now we start with the Test::More module,[Section 17.2.  A Simple Test Script] which comes with the standard Perl distribution.

[Section 17.2.  A Simple Test Script] There is also a Test::Simple module, but most people use its successor, Test::More, so we'll stick to that.

To start our testing, we write a simple Perl script. All of our tests are going to be scripts, so we can use the stuff you already know about Perl. We load Test::More and tell it how many tests we want to run. After that, we use the convenience functions from Test::More, which we'll go through later. In short, most of them compare their first argument, which is the result we got, to the second argument, which is what we expected.

#!/usr/bin/perl

use Test::More tests => 4;

ok( 1, '1 is true' );

is( 2 + 2, 4, 'The sum is 4' );

is( 2 * 3, 5, 'The product is 5' );

isnt( 2 ** 3, 6, "The result isn't 6" );

like( 'Alpaca Book', qr/alpaca/i, 'I found an alpaca!' );

When we run this simple script, we get this output. When the received and expected values are the same, Test::More outputs a line starting with ok. When those values don't match, it outputs a line starting with not ok. It also outputs some diagnostic information that tells us what the test expected and what we actually gave it.

1..4
ok 1 - 1 is true
ok 2 - The sum is 4
not ok 3 - The product is 5
#   Failed test 'The product is 5'
#   in /Users/brian/Desktop/test at line 9.
#          got: '6'
#     expected: '5'
# Looks like you planned 4 tests but ran 1 extra.
# Looks like you failed 1 test of 5 run.
ok 4 - The result isn't 6
ok 5 - I found an alpaca!

Later on, we'll see how Perl takes all of this output to create a nice report for us. We won't have to look at a lot of output to find the important bits once Test::Harness goes through it for us.


Previous
Table of Contents
Next
© 2000- NIV