Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl Testing 101

Perl Testing 101

Made for work.

Craig Treptow

August 15, 2013
Tweet

More Decks by Craig Treptow

Other Decks in Programming

Transcript

  1. Perl Testing 101
    Craig Treptow, August, 2013

    View Slide

  2. Goal
    Isolate each part of a program and show that
    the individual parts are correct.
    Unit Tests

    View Slide

  3. Benefits
    ● Find problems earlier
    ● Facilitate code changes
    ○ Help ensure changes did not affect other behavior
    ● Makes integration testing easier
    ● Code becomes “self documenting”
    ○ Just look at the tests
    Unit Tests

    View Slide

  4. Reality
    ● They do not “prove” software is correct
    ● They can be
    ○ wrong
    ○ hard to set up
    ● They do
    ○ increase programmer confidence
    ○ need to be treated like “real code”
    Unit Tests

    View Slide

  5. What is test code?
    It’s just Perl code:
    #!/usr/bin/perl -w
    print "1..1\n";
    print 1 + 1 == 2 ? "ok 1\n" : "not ok 1\n";
    Since 1 + 1 is 2, it prints:
    1..1
    ok 1
    Perl Testing

    View Slide

  6. 1..1
    ok 1
    This says:
    ● I’m going to run 1 test
    ● The test passed
    Perl Testing

    View Slide

  7. Writing that kind of code would get tedious.
    So, start with Test::Simple:
    #!/usr/bin/perl -w
    use Test::Simple tests => 1;
    ok( 1 + 1 == 2 );
    That runs the same test as before.
    Perl Testing

    View Slide

  8. Want more tests?
    #!/usr/bin/perl -w
    use Test::Simple tests => 2;
    ok( 1 + 1 == 2 );
    ok( 2 + 2 == 5 );
    Run the test:
    perl simple.t
    Produces:
    1..2
    ok 1
    not ok 2
    # Failed test (test.pl at line 5)
    # Looks like you failed 1 tests of 2.
    Perl Testing

    View Slide

  9. Before we go on:
    a few conventions
    ● Test files are kept in folders called ‘t’
    ● Test files are named .t
    ○ 00-.t
    ○ 10-.t
    ○ 20-.t
    There aren’t “rules”, it’s Perl!!
    Perl Testing

    View Slide

  10. Test::Simple
    ● Only provides ok()
    ● Can be awkward
    ○ Can’t tell what you got for a result
    ○ Only True/False (ok/not ok)
    There is hope...
    Perl Testing

    View Slide

  11. Test::More
    ● ok(), is(), isnt()
    ● like(), unlike()
    ● cmp_ok(), can_ok(), isa_ok()
    ● use_ok(), require_ok()
    ● is_deeply()
    ● eq_array(), eq_hash(), eq_set
    Perl Testing

    View Slide