Best practice for unit tests that rely on internet access?
david at cantrell.org.uk
Tue May 3 14:09:10 BST 2011
On Fri, Apr 29, 2011 at 11:15:58AM +1000, Toby Wintermute wrote:
> What is the best practice for writing unit tests that rely on internet access?
> ie. You have a CPAN module which is all about talking to a web
> service.. So you have unit tests that do just that.
> What if the system testing it doesn't allow HTTP connections out? The
> tests will fail..
Skip 'em if you can't even connect - or if the user doesn't provide an
API key or whatever that the tests need.
> a) Prompt the user for whether to skip the tests? (I hate this
> option.. I dislike interactivity in installers)
Interactivity is fine, provided it has sensible defaults.
> b) Only run the tests if specifically set by AUTHOR_TEST or something,
> otherwise mock the server? (I dislike this.. means few good tests run
> for users)
> c) Try to test if there's a working connection, and silently skip the
> tests if not? (Risks skipping tests if the connect fails for other
> reasons than no outbound HTTP allowed)
You could always check whether outbound HTTP is allowed by connecting to
somewhere entirely different. Try penthouse or thepiratebay -
somewhere which is highly unlikely to have been explicitly whitelisted,
so is a good test of whether Generic Web Stuff can be expected to work.
David Cantrell | A machine for turning tea into grumpiness
It wouldn't hurt to think like a serial killer every so often.
Purely for purposes of prevention, of course.
More information about the london.pm