SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
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)” ;

Contenu connexe

Tendances

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
 

Tendances (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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 

Dernier (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 

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)” ;