Practical Test-driven Development Presentation
.pdfSetting up your tests with mock objects
Testing Utilities
“extends.t”
3 use Test::More qw(no_plan);
4 use Test::MockObject::Extends;
5
6 use DateTime;
7
8 my $dt = DateTime->new(
year => 2007, month => 12, day => 14 );
9
10 $dt = Test::MockObject::Extends->new($dt); 11 $dt->set_always( year => 2006 );
12
13 is( $dt->year, 2006 );
14 is( $dt->month, 12 );
171
Exercise Four
•Mock the scenario of getting temperatures to convert out of a database.
•Extend so that no matter what time a file was really modified, it shows a time in the future.
172
Determining Test Coverage
Testing Utilities
•Often Test-Driven Development leads to only “Happy Path” tests, with edge cases missed or ignored.
•Perl proves an excellent coverage toolkit.
•Test coverage shows you what pieces of your code were exercised by tests and what pieces were missed.
•Coverage is shown deeper than just the statement level, with branch, conditional, statement, and POD coverage illustrated.
•Coverage can not only help you write more and better tests, but also help you find and remove unused code.
173
Determining Test Coverage
Testing Utilities
--(0)> perl -MDevel::Cover -Ilib t/TestPercentOwnership.pm
Devel::Cover 0.64: Collecting coverage data for branch, condition, pod, statement, subroutine and time. Selecting packages matching:
Ignoring packages matching: /Devel/Cover[./]
Ignoring packages in:
.
/opt/local/lib/perl5/5.8.8 /opt/local/lib/perl5/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/site_perl/5.8.8 /opt/local/lib/perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/lib/perl5/vendor_perl/5.8.8/darwin-2level
ok 1 - use PercentOwnership;
...
ok 20 - two equal sized units 1..20
Devel::Cover: Writing coverage database to /Users/joshua/Documents/clients/stonehenge/Test Driven Development Course/src/oo_testing/11/cover_db/runs/1208141988.5526.47211
---------------------------- |
------ ------ ------ ------ ------ ------ ------ |
||||||
File |
stmt |
bran |
cond |
sub |
pod |
time |
total |
---------------------------- |
------ ------ ------ ------ ------ ------ ------ |
||||||
lib/PercentOwnership.pm |
100.0 |
100.0 |
n/a |
100.0 |
0.0 |
0.6 |
90.6 |
t/TestPercentOwnership.pm |
100.0 |
50.0 |
n/a |
100.0 |
n/a |
99.4 |
99.0 |
Total |
100.0 |
75.0 |
n/a |
100.0 |
0.0 |
100.0 |
97.0 |
---------------------------- |
------ ------ ------ ------ ------ ------ ------ |
174
Determining Test Coverage
Testing Utilities
•Types of Coverage
•Statement - which statements were executed?
•Branch - were all cases that could be branched to achieved?
•Conditional - were all logical combinations expressed?
•Subroutine - were all subroutines executed?
•POD - was there POD for all public subroutines?
175
Determining Test Coverage
Testing Utilities
Statement Coverage
•Which statements in the program were executed?
•A statement isn’t necessarily a single line of code.
•A single line of code isn’t limited to one statement.
•Statement coverage is one of the easiest metrics to achieve 100% coverage on.
•Some edge cases can be very difficult to create, especially those related to catastrophic system failure.
176
Determining Test Coverage
Testing Utilities
Branch Coverage
•Every logical branch that program execution can take is exercised.
•Branches include invisible branches, like missing ‘else’ cases on an ‘if’ statement.
•100% branch coverage implies 100% statement coverage.
177
Determining Test Coverage
Testing Utilities
Conditional Coverage
•In each conditional, was every combination of boolean operators tested?
•Full conditional coverage requires more tests than branch coverage because conditional coverage ensures that every combination of boolean operators is used for each conditional while branch coverage just ensures that every possible piece of code is executed at least once around a branch.
•100% conditional coverage implies 100% branch coverage.
178
Determining Test Coverage
Testing Utilities
Subroutine Coverage
•A higher-level of coverage that summarizes at the subroutine level.
•If statement coverage is at 100%, subroutine coverage will be 100%.
•Subroutine-level coverage helps you quickly find unused (or just untested) subroutines.
179
Determining Test Coverage
Testing Utilities
“extends.t”
POD Coverage
•Not really checking to see if any real code is covered with tests.
•Uses Pod::Coverage to make sure that your documentation is comprehensive.
180