Wednesday, March 22, 2006
Unit Tests should test Units... so.. abandon private or what?
In Evil Unit Tests, Paul Wheaton says that you should be picking on little independent units - it's supposed to be "unit" testing isnt' it! Tests that involve lots of pieces of the puzzle are really 'functional' tests - JUnit or no JUnit.
He says that functional tests aren't really as useful - they have fingers in too many pies - they tend to break as a result of changes some distance away.
I think I'm writing tests at the whole range of granularities, but it hadn't occurred to me that coarse ones might really be "functional" rather than "unit".
I think the not-being-able-to-test-private-methods thing might encourage (force?) people to write overly coarse grained ones too.
See the bit at thevery bottom - "I took out the keyword 'private',. In order to achieve testability, he's had to promote a method to package access. I have been doing that too, but feeling guilty about it..
He says that functional tests aren't really as useful - they have fingers in too many pies - they tend to break as a result of changes some distance away.
I think I'm writing tests at the whole range of granularities, but it hadn't occurred to me that coarse ones might really be "functional" rather than "unit".
I think the not-being-able-to-test-private-methods thing might encourage (force?) people to write overly coarse grained ones too.
See the bit at thevery bottom - "I took out the keyword 'private',. In order to achieve testability, he's had to promote a method to package access. I have been doing that too, but feeling guilty about it..
Comments:
<< Home
RC: Bear in mind I am either a HTML/CSS person or a PHP person... probably not the best person to ask about testing /anything/ apart from "hit refresh, does it look better, yes, woohoo" or "hit refresh, does it look better, no, shit"
KP: hehehe I know *exactly* where you're coming from as in a different job I did front end work. (In fact, I am from the era of evil netscape 4.7...)
But, I've heard there is a way to unit-ish test html webapp stuff. It's a module that lets you make a request, then reads the document it gets back into some sort of DOMish thing, and lets you set up tests which ask questions like "is there a form called foo on the page" "how many pixels to the left is it"... etc Dave, what's the name of that thing you use?
But, I've heard there is a way to unit-ish test html webapp stuff. It's a module that lets you make a request, then reads the document it gets back into some sort of DOMish thing, and lets you set up tests which ask questions like "is there a form called foo on the page" "how many pixels to the left is it"... etc Dave, what's the name of that thing you use?
DS: Well, we use the Perl module WWW::Mechanize (http://search.cpan.org/~petdance/WWW-Mechanize-1.18/lib/WWW/Mechanize.pm)
to make real requests to the front end, including completing forms, clicking on buttons etc. and testing that the returned page contains the right content.
You can do fairly easily write a test which does a search on google for 'ingenta' and checks that ingenta is returned on the first page:
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
use Test::More tests => 2;
my $mech = WWW::Mechanize->new();
my $url = "http://www.google.co.uk";
$mech->get( $url );
# test we got the google search page
like( $mech->content, qr/pages from the UK/, "Successfully requested Google UK" );
# do a search
$mech->submit_form(
form_number => 1,
fields => {
q => "ingenta",
}
);
# check ingenta was returned
like( $mech->content, qr/On-line articles from 4500 journals/, "Google returned ingenta" );
Which gives:
perl mech.pl
1..2
ok 1 - Successfully requested Google UK
ok 2 - Google returned ingenta
Post a Comment
to make real requests to the front end, including completing forms, clicking on buttons etc. and testing that the returned page contains the right content.
You can do fairly easily write a test which does a search on google for 'ingenta' and checks that ingenta is returned on the first page:
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
use Test::More tests => 2;
my $mech = WWW::Mechanize->new();
my $url = "http://www.google.co.uk";
$mech->get( $url );
# test we got the google search page
like( $mech->content, qr/pages from the UK/, "Successfully requested Google UK" );
# do a search
$mech->submit_form(
form_number => 1,
fields => {
q => "ingenta",
}
);
# check ingenta was returned
like( $mech->content, qr/On-line articles from 4500 journals/, "Google returned ingenta" );
Which gives:
perl mech.pl
1..2
ok 1 - Successfully requested Google UK
ok 2 - Google returned ingenta
<< Home