SlideShare a Scribd company logo
1 of 33
Download to read offline
Writing Perl Test

Boyce Thompson Institute for Plant
            Research
           Tower Road
  Ithaca, New York 14853-1801
             U.S.A.

             by
 Aureliano Bombarely Gomez
Writing Perl Test:
   1.Why should Perl Code have test?

   2. How to write a simple test.

   3. Using Test::Simple.

   4. Expanding horizonts: Test::More and Test::Exception
Writing Perl Test:
   1.Why should Perl Code have test?

   2. How to write a simple test.

   3. Using Test::Simple.

   4. Expanding horizonts: Test::More and Test::Exception
1.Why should Perl Code have test?


   3 Good Reasons to Write Test:

     ●   Funcionality:
            Code does the things that should do.

     ●   Integrability:
             Code use/interact with other modules in the way
             that they should.

     ●   Maintenance:
            If you change something, you need to know that it is
            working.
1.Why should Perl Code have test?



  Documentation:

    ●   CPAN:
        http://search.cpan.org/dist/Test-Simple/lib/Test/Tutorial.pod.

    ●   Perldocs:
        perldoc Test::Simple, Test::More and Test::Exception

    ●   Books:
        Perl Testing: A Developer's Notebook.
1.Why should Perl Code have test?

 Testing, a code writing mode.

  ●   When you should a test for a piece of code ?

       ALWAYS

  ●   How many test should have a piece of code ?

       A MINIMUM OF ONE PER FUNCTION.
       A MAXIMUM AS A MANY AS YOU FEEL CONFORTABLE.

  ●   How you write the test script ?

       IN PARALLEL WITH YOUR CODE.
1.Why should Perl Code have test?




   A scientific point of view for CODE TESTING:

   “Test are for code the same than positive and negative
   controls for experiments, you never will be sure of your
   results without them”.
1.Why should Perl Code have test?




   Writing Test, two approaches:

     1) Integrated with the script.
        1.1) Test files and output comparisson.
        1.2) Option that enables the Test variables.

     2) A separated test script: MyModuleName.t
Writing Perl Test:
   1.Why should Perl Code have test?

   2. How to write a simple test.

   3. Using Test::Simple.

   4. Expanding horizonts: Test::More and Test::Exception
2. How to write a simple test.

    User Case: Sam format flags are in bitwise format
           http://samtools.sourceforge.net/samtools.shtml
2. How to write a simple test.

    User Case: Sam format flags are in bitwise format
           http://samtools.sourceforge.net/samtools.shtml
2. How to write a simple test.

    User Case: Sam format flags are in bitwise format
           http://samtools.sourceforge.net/samtools.shtml

     Bitwise:
     00000000001 => 0x0001 => 2^0      =1         => PAIRED
     00000000010 => 0x0002 => 2^1      =2         => PAIR MAPPED
     00000000100 => 0x0004 => 2^2      =4         => READ UNMAPPED
     00000001000 => 0x0008 => 2^3      =8         => MATE UNMAPPED
     00000010000 => 0x0001 => 2^4      = 16       => READ REVERSE
     00000100000 => 0x0002 => 2^5      = 32       => MATE REVERSE
     00001000000 => 0x0004 => 2^6      = 64       => FIRST IN PAIR
     00010000000 => 0x0008 => 2^7      = 128      => SECOND IN PAIR
     00100000000 => 0x0001 => 2^8      = 256      => ALIGN NOT PRIM.
     01000000000 => 0x0002 => 2^9      = 512      => QUALITY FAILS
     10000000000 => 0x0004 => 2^10     = 1024     => PCR DUPLICATE
2. How to write a simple test.

    Example:
      99  = 64 + 32 + 2 + 1

     Bitwise:
     00000000001 => 0x0001 => 2^0    =1       => PAIRED
     00000000010 => 0x0002 => 2^1    =2       => PAIR MAPPED
     00000000100 => 0x0004 => 2^2    =4       => READ UNMAPPED
     00000001000 => 0x0008 => 2^3    =8       => MATE UNMAPPED
     00000010000 => 0x0001 => 2^4    = 16     => READ REVERSE
     00000100000 => 0x0002 => 2^5    = 32     => MATE REVERSE
     00001000000 => 0x0004 => 2^6    = 64     => FIRST IN PAIR
     00010000000 => 0x0008 => 2^7    = 128    => SECOND IN PAIR
     00100000000 => 0x0001 => 2^8    = 256    => ALIGN NOT PRIM.
     01000000000 => 0x0002 => 2^9    = 512    => QUALITY FAILS
     10000000000 => 0x0004 => 2^10   = 1024   => PCR DUPLICATE
2. How to write a simple test.

    Example:
      145    = 128 + 16 + 1

     Bitwise:
     00000000001 => 0x0001 => 2^0    =1       => PAIRED
     00000000010 => 0x0002 => 2^1    =2       => PAIR MAPPED
     00000000100 => 0x0004 => 2^2    =4       => READ UNMAPPED
     00000001000 => 0x0008 => 2^3    =8       => MATE UNMAPPED
     00000010000 => 0x0001 => 2^4    = 16     => READ REVERSE
     00000100000 => 0x0002 => 2^5    = 32     => MATE REVERSE
     00001000000 => 0x0004 => 2^6    = 64     => FIRST IN PAIR
     00010000000 => 0x0008 => 2^7    = 128    => SECOND IN PAIR
     00100000000 => 0x0001 => 2^8    = 256    => ALIGN NOT PRIM.
     01000000000 => 0x0002 => 2^9    = 512    => QUALITY FAILS
     10000000000 => 0x0004 => 2^10   = 1024   => PCR DUPLICATE
2. How to write a simple test.


    package Sam::Flags;                    ## ACCESSORS
                                           sub set_flags {
    use strict;                                my $self = shift;
    use warnings;                              my $flags = shift;

    ## CONSTRUCTOR                             if (defined $flags) {
                                                     if ($flags !~ m/^d+$/) {
    sub new {                                          die(“ERROR: $flags isnt an integer.”);
        my $class = shift;                           }
        my $flags = shift;                           if ($flags > 2047) {
                                                       die(“ERROR: Max. flag int.= 2047”);
        my $self = bless( { } , $class);             }
                                               }
        $self->set_flags($flags);              $self->{flags} = $flags;
        return $self;                      }
    }                                      sub get_flags {
                                               my $self = shift;
                                               return $self->{flags};
                                           }
2. How to write a simple test.




                  ## SYNOPSIS

                  ## use Sam::Flags;

                  ## my $samflag = Sam::Flags->new();

                  ## $samflag->set_flag($flag);
                  ## my $flag = $samflag->get_flag();
2. How to write a simple test.


                  ## TEST: sam_flags.t
                  use strict;
                  use warnigs;
                  use Sam::Flags;

                  my $samflag = Sam::Flags->new();

                  ## Define the variable to test
                  my $flag = 64;

                  ## Use the functions
                  $samflag->set_flags($flag);
                  my $get_flag = $samflag->get_flags();

                  ## Check the result
                  if ($flag == $get_flag) {
                        print STDERR “TEST OK.n”;
                  }
                  else {
                        print STDERR “TEST FAIL.n”; }
Writing Perl Test:
   1.Why should Perl Code have test?

   2. How to write a simple test.

   3. Using Test::Simple.

   4. Expanding horizonts: Test::More and Test::Exception
3. Using Test::Simple

  Test::Simple: A perl module to write test.
  http://search.cpan.org/~mschwern/Test-Simple-0.98/lib/Test/Simple.pm




                                                      Plan:
                                                      Specify the test number




                                                      ok function:
                                                      Check a variable
3. Using Test::Simple



               ## TEST: sam_flags.t
               use strict;
               use warnings;
               use Sam::Flags;
               use Test::Simple tests => 1;

               my $samflag = Sam::Flags->new();

               ## Define the variable to test
               my $flag = 64;

               ## Use the functions
               $samflag->set_flags($flag);
               my $get_flag = $samflag->get_flags();

               ## Check the result
               ok($flag == $get_flag, “ACCESSOR TEST OKAY”);
3. Using Test::Simple


  ## FUNCTION

  sub flag2descriptions {                        my $flag = $self->get_flags();
      my $self = shift;
                                                 my @descriptions = ();
      my %flags = (
         1         =>       'PAIRED',            my @sfl = sort {$b <=> $a} keys %flags;
         2         =>       'PAIR MAPPED',
         4         =>       'READ UNMAPPED',     foreach my $fl (@sfl) {
         8         =>       'MATE UNMAPPED',          if ($fl <= $flag $$ $flag > 0) {
         16        =>       'READ REVERSE',                 push @descriptions, $flags{$fl};
         32        =>       'MATE REVERSE',                 $flag -= $fl;
         64        =>       'FIRST IN PAIR',          }
         128       =>       'SECOND IN PAIR',    }
         256       =>       'ALIGN NOT PRIM',
         512       =>       'QUALITY FAILS',     return @descriptions;
         1024      =>       'PCR DUPLICATE', }
      );
3. Using Test::Simple



        ## TEST: sam_flags.t

        use strict;
        use warnings;
        use Sam::Flags;
        use Test::Simple tests => 2;

        My $flag = 64;
        my $samflag = Sam::Flags->new($flag);

        ok($flag == $samflag->get_flags, “ACCESSOR TEST OKAY”);

        my @expdesc1 = ('FIRST IN PAIR');

        ok (join(sort($samflag->flag2descriptions())) eq join(sort(@expdec1)),
            “FLAG2DESCRIPTION TEST OKAY”);
3. Using Test::Simple




                   ## OUTPUT

                   1..2
                   ok 1 - ACCESSOR TEST OKAY
                   ok 2 - FLAG2DESCRIPTION TEST OKAY
Writing Perl Test:
   1.Why should Perl Code have test?

   2. How to write a simple test.

   3. Using Test::Simple.

   4. Expanding horizonts: Test::More and Test::Exception
4. Expanding horizonts: Test::More

 Test::More: Another perl module to write test.
  http://search.cpan.org/~mschwern/Test-Simple-0.98/lib/Test/More.pm




                                                     Plan:
                                                     Specify the test number

                                                      require_ok
                                                      Test modules

                                                      ok function:
                                                      is function
                                                      like function
4. Expanding horizonts: Test::More

 Test::More: Another perl module to write test.

 Functions:
   is($got, $expected, $message)
                  => compare variables with 'eq' function

    like($got, '/expected/i', $message)
                    => compare using a regex

    unlike($got, '/expected/', $message)
                   => compare using !~ and a regex

    cmp_ok($got, $operator, $expected, $message)
                => compare variables using an specific op.
4. Expanding horizonts: Test::More



         ## TEST: sam_flags.t

         use strict;
         use warnings;
         use Sam::Flags;
         use Test::More tests => 2;

         My $flag = 64;
         my $samflag = Sam::Flags->new($flag);

         is($flag, $samflag->get_flags, “ACCESSOR TEST OKAY”);

         my @expdesc1 = ('FIRST IN PAIR');

         like(join(sort($samflag->flag2descriptions())), '/FIRST IN PAIR/i'),
              “FLAG2DESCRIPTION TEST OKAY”);
4. Expanding horizonts: Test::More

 Test::More: Another perl module to write test.
  Functions:
    can_ok($module, @methods)
               => check that a module can use some methods

     isa_ok($object, $class, $object_name)
                   => check if an object is the right class

     use_ok($module_name)
                => check that the module can be loaded

     diag($diagnostic_message)
                     => print a diagnostic message
4. Expanding horizonts: Test::More

      ## TEST: sam_flags.t

      use strict;
      use warnings;
      use Test::More tests => 5;

      BEGIN { use_ok('Sam::Flags'); };

      can_ok('Sam::Flags', qw/new get_flags set_flags flag2descriptions/);

      my $flag = 64;
      my $samflag = Sam::Flags->new($flag);
      isa_ok($samflag, 'Sam::Flags');

      is($flag, $samflag->get_flags, “ACCESSOR TEST OKAY”)
                or diag(“Accessor test failed”);

      my @expdesc1 = ('FIRST IN PAIR');

      like(join(sort($samflag->flag2descriptions())), '/FIRST IN PAIR/i'),
           “FLAG2DESCRIPTION TEST OKAY”) or diag(“Flag2description failed”);
4. Expanding horizonts: Test::More

   Test::Exception: Test exception based code.
  http://search.cpan.org/~adie/Test-Exception-0.31/lib/Test/Exception.pm




                                                       Plan:
                                                       Specify the test number

                                                        throws_ok function:
4. Expanding horizonts: Test::More

   Test::Exception: Test exception based code.


   Functions:
     throws_ok(BLOCK REGEX/CLASS, $description)
                => check if a block is throw with a regex or
                  a class.

      dies_ok(BLOCK, $description)
                  => check if a block dies

      lives_ok(BLOCK, $description)
                  => check if a block does not die
4. Expanding horizonts: Test::More

        ## TEST: sam_flags.t

        use strict;
        use warnings;
        use Test::More tests => 5;
        Use Test::Exception;

        BEGIN { use_ok('Sam::Flags'); };

        can_ok('Sam::Flags', qw/new get_flags set_flags flag2descriptions/);

        my $flag = 64;
        my $samflag = Sam::Flags->new($flag);
        isa_ok($samflag, 'Sam::Flags');

        is($flag, $samflag->get_flags, “ACCESSOR TEST OKAY”)
                  or diag(“Accessor test failed”);

        throws_ok { $samflag->set_flags('NoInteger')} qr/ERROR: NoInteger/,
            “SET_FLAGS with wrong argument dies” ;
4. Expanding horizonts: Test::More




           ## TEST: sam_flags.t


           dies_ok { $samflag->set_flags(30000)},
               “SET_FLAGS with wrong argument dies (high integer)” ;

More Related Content

What's hot

Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPIPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPGuilherme Blanco
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1兎 伊藤
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11Combell NV
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
Cli the other sapi pbc11
Cli the other sapi pbc11Cli the other sapi pbc11
Cli the other sapi pbc11Combell NV
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHPWim Godden
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016Britta Alex
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021Ayesh Karunaratne
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6 brian d foy
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 

What's hot (20)

Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Workshop unittesting
Workshop unittestingWorkshop unittesting
Workshop unittesting
 
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPIPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Cli the other sapi pbc11
Cli the other sapi pbc11Cli the other sapi pbc11
Cli the other sapi pbc11
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 

Viewers also liked (6)

Introduction2R
Introduction2RIntroduction2R
Introduction2R
 
BasicGraphsWithR
BasicGraphsWithRBasicGraphsWithR
BasicGraphsWithR
 
BasicLinux
BasicLinuxBasicLinux
BasicLinux
 
GoTermsAnalysisWithR
GoTermsAnalysisWithRGoTermsAnalysisWithR
GoTermsAnalysisWithR
 
Genome Assembly
Genome AssemblyGenome Assembly
Genome Assembly
 
RNAseq Analysis
RNAseq AnalysisRNAseq Analysis
RNAseq Analysis
 

Similar to Write Perl Tests: Guide to Test::Simple and Beyond

O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...Rodolfo Carvalho
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby MetaprogrammingThaichor Seng
 
Test and API-driven development of CakePHP Behaviors
Test and API-driven development of CakePHP BehaviorsTest and API-driven development of CakePHP Behaviors
Test and API-driven development of CakePHP BehaviorsPierre MARTIN
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.Workhorse Computing
 
Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"Fwdays
 
Test-driven Development no Rails
Test-driven Development no RailsTest-driven Development no Rails
Test-driven Development no Railselliando dias
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Nikita Popov
 
10 Catalyst Tips
10 Catalyst Tips10 Catalyst Tips
10 Catalyst TipsJay Shirley
 
Dirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP ExtensionDirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP ExtensionAdam Trachtenberg
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormMichelangelo van Dam
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::ManagerJay Shirley
 
Ruby closures, how are they possible?
Ruby closures, how are they possible?Ruby closures, how are they possible?
Ruby closures, how are they possible?Carlos Alonso Pérez
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 

Similar to Write Perl Tests: Guide to Test::Simple and Beyond (20)

My Development Story
My Development StoryMy Development Story
My Development Story
 
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
 
Test and API-driven development of CakePHP Behaviors
Test and API-driven development of CakePHP BehaviorsTest and API-driven development of CakePHP Behaviors
Test and API-driven development of CakePHP Behaviors
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.
 
Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
Test-driven Development no Rails
Test-driven Development no RailsTest-driven Development no Rails
Test-driven Development no Rails
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)
 
10 Catalyst Tips
10 Catalyst Tips10 Catalyst Tips
10 Catalyst Tips
 
Dirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP ExtensionDirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP Extension
 
Metadata-driven Testing
Metadata-driven TestingMetadata-driven Testing
Metadata-driven Testing
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
 
Ruby closures, how are they possible?
Ruby closures, how are they possible?Ruby closures, how are they possible?
Ruby closures, how are they possible?
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Specs2
Specs2Specs2
Specs2
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Write Perl Tests: Guide to Test::Simple and Beyond

  • 1. Writing Perl Test Boyce Thompson Institute for Plant Research Tower Road Ithaca, New York 14853-1801 U.S.A. by Aureliano Bombarely Gomez
  • 2. Writing Perl Test: 1.Why should Perl Code have test? 2. How to write a simple test. 3. Using Test::Simple. 4. Expanding horizonts: Test::More and Test::Exception
  • 3. Writing Perl Test: 1.Why should Perl Code have test? 2. How to write a simple test. 3. Using Test::Simple. 4. Expanding horizonts: Test::More and Test::Exception
  • 4. 1.Why should Perl Code have test? 3 Good Reasons to Write Test: ● Funcionality: Code does the things that should do. ● Integrability: Code use/interact with other modules in the way that they should. ● Maintenance: If you change something, you need to know that it is working.
  • 5. 1.Why should Perl Code have test? Documentation: ● CPAN: http://search.cpan.org/dist/Test-Simple/lib/Test/Tutorial.pod. ● Perldocs: perldoc Test::Simple, Test::More and Test::Exception ● Books: Perl Testing: A Developer's Notebook.
  • 6. 1.Why should Perl Code have test? Testing, a code writing mode. ● When you should a test for a piece of code ? ALWAYS ● How many test should have a piece of code ? A MINIMUM OF ONE PER FUNCTION. A MAXIMUM AS A MANY AS YOU FEEL CONFORTABLE. ● How you write the test script ? IN PARALLEL WITH YOUR CODE.
  • 7. 1.Why should Perl Code have test? A scientific point of view for CODE TESTING: “Test are for code the same than positive and negative controls for experiments, you never will be sure of your results without them”.
  • 8. 1.Why should Perl Code have test? Writing Test, two approaches: 1) Integrated with the script. 1.1) Test files and output comparisson. 1.2) Option that enables the Test variables. 2) A separated test script: MyModuleName.t
  • 9. Writing Perl Test: 1.Why should Perl Code have test? 2. How to write a simple test. 3. Using Test::Simple. 4. Expanding horizonts: Test::More and Test::Exception
  • 10. 2. How to write a simple test. User Case: Sam format flags are in bitwise format http://samtools.sourceforge.net/samtools.shtml
  • 11. 2. How to write a simple test. User Case: Sam format flags are in bitwise format http://samtools.sourceforge.net/samtools.shtml
  • 12. 2. How to write a simple test. User Case: Sam format flags are in bitwise format http://samtools.sourceforge.net/samtools.shtml Bitwise: 00000000001 => 0x0001 => 2^0 =1 => PAIRED 00000000010 => 0x0002 => 2^1 =2 => PAIR MAPPED 00000000100 => 0x0004 => 2^2 =4 => READ UNMAPPED 00000001000 => 0x0008 => 2^3 =8 => MATE UNMAPPED 00000010000 => 0x0001 => 2^4 = 16 => READ REVERSE 00000100000 => 0x0002 => 2^5 = 32 => MATE REVERSE 00001000000 => 0x0004 => 2^6 = 64 => FIRST IN PAIR 00010000000 => 0x0008 => 2^7 = 128 => SECOND IN PAIR 00100000000 => 0x0001 => 2^8 = 256 => ALIGN NOT PRIM. 01000000000 => 0x0002 => 2^9 = 512 => QUALITY FAILS 10000000000 => 0x0004 => 2^10 = 1024 => PCR DUPLICATE
  • 13. 2. How to write a simple test. Example: 99 = 64 + 32 + 2 + 1 Bitwise: 00000000001 => 0x0001 => 2^0 =1 => PAIRED 00000000010 => 0x0002 => 2^1 =2 => PAIR MAPPED 00000000100 => 0x0004 => 2^2 =4 => READ UNMAPPED 00000001000 => 0x0008 => 2^3 =8 => MATE UNMAPPED 00000010000 => 0x0001 => 2^4 = 16 => READ REVERSE 00000100000 => 0x0002 => 2^5 = 32 => MATE REVERSE 00001000000 => 0x0004 => 2^6 = 64 => FIRST IN PAIR 00010000000 => 0x0008 => 2^7 = 128 => SECOND IN PAIR 00100000000 => 0x0001 => 2^8 = 256 => ALIGN NOT PRIM. 01000000000 => 0x0002 => 2^9 = 512 => QUALITY FAILS 10000000000 => 0x0004 => 2^10 = 1024 => PCR DUPLICATE
  • 14. 2. How to write a simple test. Example: 145 = 128 + 16 + 1 Bitwise: 00000000001 => 0x0001 => 2^0 =1 => PAIRED 00000000010 => 0x0002 => 2^1 =2 => PAIR MAPPED 00000000100 => 0x0004 => 2^2 =4 => READ UNMAPPED 00000001000 => 0x0008 => 2^3 =8 => MATE UNMAPPED 00000010000 => 0x0001 => 2^4 = 16 => READ REVERSE 00000100000 => 0x0002 => 2^5 = 32 => MATE REVERSE 00001000000 => 0x0004 => 2^6 = 64 => FIRST IN PAIR 00010000000 => 0x0008 => 2^7 = 128 => SECOND IN PAIR 00100000000 => 0x0001 => 2^8 = 256 => ALIGN NOT PRIM. 01000000000 => 0x0002 => 2^9 = 512 => QUALITY FAILS 10000000000 => 0x0004 => 2^10 = 1024 => PCR DUPLICATE
  • 15. 2. How to write a simple test. package Sam::Flags; ## ACCESSORS sub set_flags { use strict; my $self = shift; use warnings; my $flags = shift; ## CONSTRUCTOR if (defined $flags) { if ($flags !~ m/^d+$/) { sub new { die(“ERROR: $flags isnt an integer.”); my $class = shift; } my $flags = shift; if ($flags > 2047) { die(“ERROR: Max. flag int.= 2047”); my $self = bless( { } , $class); } } $self->set_flags($flags); $self->{flags} = $flags; return $self; } } sub get_flags { my $self = shift; return $self->{flags}; }
  • 16. 2. How to write a simple test. ## SYNOPSIS ## use Sam::Flags; ## my $samflag = Sam::Flags->new(); ## $samflag->set_flag($flag); ## my $flag = $samflag->get_flag();
  • 17. 2. How to write a simple test. ## TEST: sam_flags.t use strict; use warnigs; use Sam::Flags; my $samflag = Sam::Flags->new(); ## Define the variable to test my $flag = 64; ## Use the functions $samflag->set_flags($flag); my $get_flag = $samflag->get_flags(); ## Check the result if ($flag == $get_flag) { print STDERR “TEST OK.n”; } else { print STDERR “TEST FAIL.n”; }
  • 18. Writing Perl Test: 1.Why should Perl Code have test? 2. How to write a simple test. 3. Using Test::Simple. 4. Expanding horizonts: Test::More and Test::Exception
  • 19. 3. Using Test::Simple Test::Simple: A perl module to write test. http://search.cpan.org/~mschwern/Test-Simple-0.98/lib/Test/Simple.pm Plan: Specify the test number ok function: Check a variable
  • 20. 3. Using Test::Simple ## TEST: sam_flags.t use strict; use warnings; use Sam::Flags; use Test::Simple tests => 1; my $samflag = Sam::Flags->new(); ## Define the variable to test my $flag = 64; ## Use the functions $samflag->set_flags($flag); my $get_flag = $samflag->get_flags(); ## Check the result ok($flag == $get_flag, “ACCESSOR TEST OKAY”);
  • 21. 3. Using Test::Simple ## FUNCTION sub flag2descriptions { my $flag = $self->get_flags(); my $self = shift; my @descriptions = (); my %flags = ( 1 => 'PAIRED', my @sfl = sort {$b <=> $a} keys %flags; 2 => 'PAIR MAPPED', 4 => 'READ UNMAPPED', foreach my $fl (@sfl) { 8 => 'MATE UNMAPPED', if ($fl <= $flag $$ $flag > 0) { 16 => 'READ REVERSE', push @descriptions, $flags{$fl}; 32 => 'MATE REVERSE', $flag -= $fl; 64 => 'FIRST IN PAIR', } 128 => 'SECOND IN PAIR', } 256 => 'ALIGN NOT PRIM', 512 => 'QUALITY FAILS', return @descriptions; 1024 => 'PCR DUPLICATE', } );
  • 22. 3. Using Test::Simple ## TEST: sam_flags.t use strict; use warnings; use Sam::Flags; use Test::Simple tests => 2; My $flag = 64; my $samflag = Sam::Flags->new($flag); ok($flag == $samflag->get_flags, “ACCESSOR TEST OKAY”); my @expdesc1 = ('FIRST IN PAIR'); ok (join(sort($samflag->flag2descriptions())) eq join(sort(@expdec1)), “FLAG2DESCRIPTION TEST OKAY”);
  • 23. 3. Using Test::Simple ## OUTPUT 1..2 ok 1 - ACCESSOR TEST OKAY ok 2 - FLAG2DESCRIPTION TEST OKAY
  • 24. Writing Perl Test: 1.Why should Perl Code have test? 2. How to write a simple test. 3. Using Test::Simple. 4. Expanding horizonts: Test::More and Test::Exception
  • 25. 4. Expanding horizonts: Test::More Test::More: Another perl module to write test. http://search.cpan.org/~mschwern/Test-Simple-0.98/lib/Test/More.pm Plan: Specify the test number require_ok Test modules ok function: is function like function
  • 26. 4. Expanding horizonts: Test::More Test::More: Another perl module to write test. Functions: is($got, $expected, $message) => compare variables with 'eq' function like($got, '/expected/i', $message) => compare using a regex unlike($got, '/expected/', $message) => compare using !~ and a regex cmp_ok($got, $operator, $expected, $message) => compare variables using an specific op.
  • 27. 4. Expanding horizonts: Test::More ## TEST: sam_flags.t use strict; use warnings; use Sam::Flags; use Test::More tests => 2; My $flag = 64; my $samflag = Sam::Flags->new($flag); is($flag, $samflag->get_flags, “ACCESSOR TEST OKAY”); my @expdesc1 = ('FIRST IN PAIR'); like(join(sort($samflag->flag2descriptions())), '/FIRST IN PAIR/i'), “FLAG2DESCRIPTION TEST OKAY”);
  • 28. 4. Expanding horizonts: Test::More Test::More: Another perl module to write test. Functions: can_ok($module, @methods) => check that a module can use some methods isa_ok($object, $class, $object_name) => check if an object is the right class use_ok($module_name) => check that the module can be loaded diag($diagnostic_message) => print a diagnostic message
  • 29. 4. Expanding horizonts: Test::More ## TEST: sam_flags.t use strict; use warnings; use Test::More tests => 5; BEGIN { use_ok('Sam::Flags'); }; can_ok('Sam::Flags', qw/new get_flags set_flags flag2descriptions/); my $flag = 64; my $samflag = Sam::Flags->new($flag); isa_ok($samflag, 'Sam::Flags'); is($flag, $samflag->get_flags, “ACCESSOR TEST OKAY”) or diag(“Accessor test failed”); my @expdesc1 = ('FIRST IN PAIR'); like(join(sort($samflag->flag2descriptions())), '/FIRST IN PAIR/i'), “FLAG2DESCRIPTION TEST OKAY”) or diag(“Flag2description failed”);
  • 30. 4. Expanding horizonts: Test::More Test::Exception: Test exception based code. http://search.cpan.org/~adie/Test-Exception-0.31/lib/Test/Exception.pm Plan: Specify the test number throws_ok function:
  • 31. 4. Expanding horizonts: Test::More Test::Exception: Test exception based code. Functions: throws_ok(BLOCK REGEX/CLASS, $description) => check if a block is throw with a regex or a class. dies_ok(BLOCK, $description) => check if a block dies lives_ok(BLOCK, $description) => check if a block does not die
  • 32. 4. Expanding horizonts: Test::More ## TEST: sam_flags.t use strict; use warnings; use Test::More tests => 5; Use Test::Exception; BEGIN { use_ok('Sam::Flags'); }; can_ok('Sam::Flags', qw/new get_flags set_flags flag2descriptions/); my $flag = 64; my $samflag = Sam::Flags->new($flag); isa_ok($samflag, 'Sam::Flags'); is($flag, $samflag->get_flags, “ACCESSOR TEST OKAY”) or diag(“Accessor test failed”); throws_ok { $samflag->set_flags('NoInteger')} qr/ERROR: NoInteger/, “SET_FLAGS with wrong argument dies” ;
  • 33. 4. Expanding horizonts: Test::More ## TEST: sam_flags.t dies_ok { $samflag->set_flags(30000)}, “SET_FLAGS with wrong argument dies (high integer)” ;