Thursday, 5 February 2015

7. Skipping test cases if user input is 1

Note: Using PERL Module- fib.pm from post 1

Test Program(t.pl):
use strict;
use warnings;

use Test::More tests => 3;

use fib;
chomp(my $a=<>);

  SKIP: {
        skip('This is skipped',3 )
            if $a==1;


ok( fib(1) == 1 ,'tc1');
ok( fib(4) == 0 ,'tc4' );
ok( fib(8) == 1 ,'tc8' );
}

Output of t.pl
When input is 1:










When input is not 1:


Wednesday, 4 February 2015

6. Adding comments to test result

Note: Using PERL Module- fib.pm from post 1

Test Program(t.pl):
use strict;
use warnings;


use Test::More tests=>3;
use fib;

my @arr=(
 [1,1],
 [4,0],
 [8,1]
 );

foreach(@arr)
 {
 my $exp=pop @$_;
 my $act=fib(@$_);
 diag "Finding whether @$_ is fib";
 ok($exp==$act);
 }

Output of t.pl






5. Run test when number of test cases are not known

Note: Using PERL Module- fib.pm from post 1

Test Program(t.pl):
use strict;
use warnings;


use Test::Simple 'no_plan';
use fib;

my @arr=(
[1,1],
[4,0],
[8,1]
);

foreach(@arr)
{
my $exp=pop @$_;
my $act=fib(@$_);
ok($exp==$act);
}

Output of t.pl





4. Writing test cases in array

Note: Using PERL Module- fib.pm from post 1

Test Program(t.pl):
use strict;
use warnings;

use Test::Simple tests => 3;

use fib;

my @arr=(
[1,1],
[4,0],
[8,1]
);

foreach(@arr)
{
my $exp=pop @$_;
my $act=fib(@$_);
ok($exp==$act);
}

Output of t.pl




3: Adding comments/test name with tests

Note: Using PERL Module- fib.pm from post 1

Test Program(t.pl):
use strict;
use warnings;

use Test::Simple tests => 3;

use fib;

ok( fib(1) == 1 ,'1 is fib');
ok( fib(4) == 0 ,'4 is not fib');
ok( fib(8) == 1 ,'8 is fib');

Output of t.pl



2. Testing a Perl Module after issue is fixed

Now, suppose issue in last post was fixed.

Code(fib.pm):
use strict;
use warnings;

my @arr=(1,2,3,5,8);

sub fib{
my @a1=@_;
my $a=$a1[0];
my $c=0;
foreach(@arr)
{
if($_==$a)
{
$c++;
return 1;
last;
}
}
if($c==0)
{
return 0;
}
}


Test Program(t.pl):
use strict;
use warnings;

use Test::Simple tests => 10;

use fib;

ok( fib(1) == 1 );
ok( fib(2) == 1 );
ok( fib(3) == 1 );
ok( fib(4) == 0 );
ok( fib(5) == 1 );
ok( fib(6) == 0 );
ok( fib(7) == 0 );
ok( fib(8) == 1 );
ok( fib(9) == 0 );
ok( fib(10) == 0 );

Output of t.pl



1. Testing a Perl Module which have errors

Suppose there is a PERL Module- fib.pm

Function-  returns 1 if number is Fibonacci, otherwise return 0.

Range- 1 to 10

Location: C:\Perl\site\lib

Code(fib.pm):
use strict;
use warnings;

my @arr=(1,2,3,5);

sub fib{
my @a1=@_;
my $a=$a1[0];
my $c=0;
foreach(@arr)
{
if($_==$a)
{
$c++;
return 1;
last;
}
}
if($c==0)
{
return 0;
}
}

Clearly we have a bug here:- This program will output 0 for 8.

Test Program(t.pl):
use strict;
use warnings;

use Test::Simple tests => 10;

use fib;

ok( fib(1) == 1 );
ok( fib(2) == 1 );
ok( fib(3) == 1 );
ok( fib(4) == 0 );
ok( fib(5) == 1 );
ok( fib(6) == 0 );
ok( fib(7) == 0 );
ok( fib(8) == 1 );
ok( fib(9) == 0 );
ok( fib(10) == 0 );

Output of t.pl