0
Perl Testing 101
Craig Treptow, August, 2013
Unit Tests
Goal
Isolate each part of a program and show that
the individual parts are correct.
Unit Tests
Benefits
● Find problems earlier
● Facilitate code changes
○ Help ensure changes did not affect other behavior
...
Unit Tests
Reality
● They do not “prove” software is correct
● They can be
○ wrong
○ hard to set up

● They do
○ increase ...
Perl Testing
What is test code?
It’s just Perl code:
#!/usr/bin/perl -w

print "1..1n";

print 1 + 1 == 2 ? "ok 1n" : "not...
Perl Testing
1..1
ok 1

This says:
● I’m going to run 1 test
● The test passed
Perl Testing
Writing that kind of code would get tedious.
So, start with Test::Simple:
#!/usr/bin/perl -w

use Test::Simpl...
Perl Testing
Want more tests?
#!/usr/bin/perl -w

use Test::Simple tests => 2;
ok( 1 + 1 == 2 );
ok( 2 + 2 == 5 );

Run th...
Perl Testing
Before we go on:
a few conventions
● Test files are kept in folders called ‘t’
● Test files are named <someth...
Perl Testing
Test::Simple
● Only provides ok()
● Can be awkward
○ Can’t tell what you got for a result
○ Only True/False (...
Perl Testing
Test::More
● ok(), is(), isnt()
● like(), unlike()
● cmp_ok(), can_ok(), isa_ok()
● use_ok(), require_ok()
● ...
Upcoming SlideShare
Loading in...5
×

Perl testing 101

189

Published on

Made for $work to explain unit tests to old time COBOLers.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
189
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Perl testing 101"

  1. 1. Perl Testing 101 Craig Treptow, August, 2013
  2. 2. Unit Tests Goal Isolate each part of a program and show that the individual parts are correct.
  3. 3. Unit Tests 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
  4. 4. Unit Tests 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”
  5. 5. Perl Testing What is test code? It’s just Perl code: #!/usr/bin/perl -w print "1..1n"; print 1 + 1 == 2 ? "ok 1n" : "not ok 1n"; Since 1 + 1 is 2, it prints: 1..1 ok 1
  6. 6. Perl Testing 1..1 ok 1 This says: ● I’m going to run 1 test ● The test passed
  7. 7. Perl Testing 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.
  8. 8. Perl Testing 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.
  9. 9. Perl Testing Before we go on: a few conventions ● Test files are kept in folders called ‘t’ ● Test files are named <something>.t ○ 00-<test prerequisites>.t ○ 10-<module name>.t ○ 20-<module name>.t There aren’t “rules”, it’s Perl!!
  10. 10. Perl Testing 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...
  11. 11. Perl Testing 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×