Calculator test
Let's do that. Write a script using the sum() function of the module and printing the results.
examples/test-simple/tests/t01.pl
use strict; use warnings; use FindBin qw($Bin); use lib "$Bin/../lib"; use MySimpleCalc qw(sum); print sum(1, 1), "\n"; print sum(2, 2), "\n"; print sum(2, 2, 2), "\n";
In this script we use the $Bin variable of the FindBin module to let perl find the MyTools.pm file.
If you run this script, the output will look like this:
2 4 4
There was an error on the last line. 2 + 2 + 2 should be 6 and not 4
While this is a very simple example, it is easy to overlook the simple detail, not noticing that one of the results was indeed incorrect.
Of course this is a simple computation and anyone should know what is the expected result, but what if we are testing something more complex?
Do you know what should be the result? Will you compute it every time manually to check if that's the correct answer?
At this point we are not interested in fixing the bug in MyTools.pm. We are interested in a robust way to write tests for it.
We'll use this example but we have not solved our biggest problems yet:
We cannot expect our test engineers looking at the results to know the valid result of each line.