Next | Automated Testing | 16 |
A lot of the code in a normal program is for error checking and error handling
Much of this can be omitted in test programs
Consider
unless (open F, "<", $db_file) { die "Couldn't open file '$db_file': $!; aborting"; } my @matches; while (<F>) { chomp; my ($u) = split /:/; push @matches, $_ if $u eq $username; } close F; my ($rec, @extra) = @matches; if (@extra) { die "Multiple records for user '$username' in file '$db_file'\n"; } elsif (not defined $rec) { die "User '$username' not found in file '$db_file'\n"; } my (@field) = split /:/, $rec; unless (@field == 5) { die "Malformed record for user '$username' in file '$db_file':\n\t$rec\n"; } # Now do something with $field[3] ...
In Test World, this looks more like this:
my ($rec) = qx{ grep '^netmon:' $db_file }; is((split(/:/, $rec))[3], "expired", "netmon user was expired");
Next | Copyright © 2004 Mark Dominus |