Practical Test-driven Development Presentation
.pdfTesting and reporting on complex data
Testing Utilities
•Not all tests can easily be performed with a simple binary expression.
•Sometimes the data being tested is large enough the presenting differences between what was received and what was expected is challenging.
•Test::Differences and Test::Deep can help simplify testing of large and/or complex data structures.
141
Testing and reporting on complex data
Testing Utilities
use Test::Differences; eq_or_diff( $got, $expected, $test_name )
•Test::Differences picks up where Test::More’s ‘is_deeply’ leaves off by showing side-by-side differences in multiline strings or complex data structures.
•This module is only really reliable on Perl’s > 5.8
142
Testing and reporting on complex data
Testing Utilities
“scalar_is_deeply.t”
3 use Test::More qw(no_plan);
4
5 my $expected = 'This is a multiline
6 string of text
7 that is not the easiest thing to
8 display.';
9
10 my $got = $expected;
11 substr($got, 12, 1) = 'i';
12
13 is $got, $expected, 'The are who we thought they were';
143
Testing and reporting on complex data
Testing Utilities
--(0)> prove scalar_is_deeply.t
is_deeply....
#Failed test 'The are who we thought they were'
#at is_deeply.t line 13.
# got: 'This is a muitiline
#string of text
#that is not the easiest thing to
#display.'
#expected: 'This is a multiline
#string of text
#that is not the easiest thing to
#display.'
#Looks like you failed 1 test of 1. is_deeply....dubious
Test returned status 1 (wstat 256, 0x100) DIED. FAILED test 1
Failed 1/1 tests, 0.00% okay
Failed Test Stat Wstat Total Fail Failed List of Failed
-------------------------------------------------------------------------------
is_deeply.t |
1 |
256 |
1 |
1 100.00% 1 |
Failed 1/1 test scripts, 0.00% okay. 1/1 subtests failed, 0.00% okay.
144
Testing and reporting on complex data
Testing Utilities
“scalar_test_differences.t”
3 use Test::More qw(no_plan);
4 use Test::Differences;
5
6 my $expected = 'This is a multiline
7 string of text
8 that is not the easiest thing to
9 display.';
10
11 my $got = $expected;
12 substr($got, 12, 1) = 'i';
13
14 eq_or_diff $got, $expected,
'They are who we thought they were';
145
Testing and reporting on complex data
Testing Utilities
--(0)> prove scalar_test_differences.t test_differences....NOK 1
#Failed test 'They are who we thought they were'
#at test_differences.t line 14.
# + |
---+---------------------------------- |
|
+---------------------------------- |
+ |
# | Ln|Got |
|
|Expected |
| |
|
# +--- |
+---------------------------------- |
|
+---------------------------------- |
+ |
# * |
1|This is a muitiline |
|This is a multiline |
* |
|
# | |
2|string of text |
|string of text |
| |
|
# | |
3|that is not the easiest thing to |
|that is not the easiest thing to |
| |
|
# | |
4|display. |
|
|display. |
| |
# +--- |
+---------------------------------- |
|
+---------------------------------- |
+ |
# Looks like you failed 1 test of 1. |
|
|
||
test_differences.... |
dubious |
|
|
|
|
Test returned status 1 (wstat 256, 0x100) |
|
||
DIED. FAILED test 1 |
|
|
|
|
|
Failed 1/1 tests, 0.00% okay |
|
|
|
Failed Test |
Stat Wstat Total Fail |
Failed List of Failed |
|
-------------------------------------------------------------------------------
test_differences.t |
1 256 |
1 |
1 |
100.00% |
1 |
Failed 1/1 test scripts, 0.00% okay. 1/1 |
subtests |
failed, 0.00% okay. |
146
Testing and reporting on complex data
Testing Utilities
“ref_is_deeply.t”
1 use warnings;
2 use strict;
3 use Test::More qw(no_plan);
4
5 my $expected = { name => 'Josh',
pets => [qw( ella ginger )] }; 6 my $got = bless {%$expected}, 'Person';
7 $got->{name} = 'Heather';
8
9 is $got, $expected, 'Structures are different';
147
Testing and reporting on complex data
Testing Utilities
--(0)> prove ref_is_deeply.t
ref_is_deeply....
#Failed test 'Structures are different'
#at ref_is_deeply.t line 9.
# got: 'Person=HASH(0x183beb8)'
#expected: 'HASH(0x183be40)'
#Looks like you failed 1 test of 1. ref_is_deeply....dubious
Test returned status 1 (wstat 256, 0x100) DIED. FAILED test 1
Failed 1/1 tests, 0.00% okay
Failed Test Stat Wstat Total Fail Failed List of Failed
-----------------------------------------------------------------------
ref_is_deeply.t |
1 256 |
1 |
1 100.00% 1 |
Failed 1/1 test |
scripts, 0.00% |
okay. 1/1 subtests failed, 0.00% okay. |
148
Testing and reporting on complex data
Testing Utilities
“ref_test_differences.t”
1 use warnings;
2 use strict;
3 use Test::More qw(no_plan);
4 use Test::Differences;
5
6 my $expected = { name => 'Josh',
pets => [qw( ella ginger )] }; 7 my $got = bless {%$expected}, 'Person';
8 $got->{name} = 'Heather';
9
10 eq_or_diff $got, $expected, 'Structures are different';
149
Testing and reporting on complex data
Testing Utilities
--(0)> perl ref_test_differences.t not ok 1 - Structures are different
#Failed test 'Structures are different'
#at ref_test_differences.t line 10.
# +---- |
+ |
---------------------- |
+------------------- |
|
+ |
# | Elt|Got |
|Expected |
| |
|||
# +---- |
+---------------------- |
|
+------------------- |
|
+ |
# * |
0|bless( { |
|{ |
|
* |
|
# * |
1| |
name => 'Heather', |
| |
name => 'Josh', |
* |
# | |
2| |
pets => [ |
| |
pets => [ |
| |
# | |
3| |
'ella', |
| |
'ella', |
| |
# | |
4| |
'ginger' |
| |
'ginger' |
| |
# | |
5| |
] |
| |
] |
| |
# * |
6|}, 'Person' ) |
|} |
|
* |
|
# +---- |
+---------------------- |
|
+------------------- |
|
+ |
1..1 |
|
|
|
|
|
# Looks like you failed 1 test of 1.
150