Home > Programming > Breaking up your tests

## Breaking up your tests

I thought I’d pass on a simple tip that was given to me by a colleague last year. When writing tests, it can help to break it into sections called “Given”, “When”, and “Then”, indicated by comments:

• “Given” – set up the criteria for your test
• “When” – execute the code for the test
• “Then” – inspect the results, and make assertions

Let’s say you were writing a unit test for the get() method of LWP::UserAgent (as an example, obviously you wouldn’t really do this, unless you are the maintainer of that module).

Assuming the use of Test::Class, the code might look like:

sub test_get : Tests(3) {
my $self = shift; # GIVEN my$ua = LWP::UserAgent->new;
$ua->timeout(10);$ua->agent('SomeAgentForTestingPurposes');

# WHEN
my $response =$ua->get('http://myurl.com/');

# THEN
isa_ok($response, 'HTTP::Response', "Returned object is a HTTP::Response"); is($response->is_success, 1, "Response was successful");
is(defined \$response->content, 1, "Something in the response's content");
}

This is a fairly simple test; in reality you’d probably have a lot more assertions in the “Then” section, and you’d probably be doing some mocking (making the ‘Given’ section longer). But for the purposes of this example, you can see how the three parts of the test are apparent at a glance. Also, it makes it clear that we’re only testing one thing in this test method (another good practice). Otherwise we’d end up with multiple Given/When/Then blocks.

So a simple tip maybe, but one I find I now use in all my unit testing.

Categories: Programming Tags: ,
1. June 6, 2009 at 7:15 pm

What is exactly the difference between “GIVEN” and “WHEN”? Just that
the “GIVEN” part lives longer than the “WHEN”?

\rho

• June 6, 2009 at 7:37 pm

‘Given’ has the things you need to do to setup the test, but are not actually part of the test. (That might not be too clear from the example I give)… to test the get() method, you need to instantiate a new LWP::UserAgent object, but you’re not actually testing new() in this test.

‘When’ has the execution of the actual code needed for the test. In most cases, if your code is well-factored, you’d expect this to be only one line, i.e. the call to the method you’re testing. So when you go back and look at the code, you can quickly see exactly what it is you’re testing by glancing at the ‘When’ section.

Hope that makes sense 🙂

2. July 5, 2016 at 11:51 am

Hello to every one, the contents present at this web
site are genuinely remarkable for people knowledge, well,
keep up the good work fellows.

3. February 11, 2017 at 4:36 pm

After checking out a handful of the articles on your website, I seriously
like your way of blogging. I added it to my bookmark webpage list and will be checking back soon. Take
a look at my web site too and let me know what you think.

4. March 28, 2017 at 1:57 am

I know this web site provides quality dependent posts and other information, is there any other site which presents such stuff in quality?

1. April 20, 2010 at 1:11 pm