Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on...

56
Unit Testing Testing Unit Testing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

Transcript of Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on...

Page 1: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Unit Testing

Testing

Unit Testing

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Page 2: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Studying impact of climate change on agriculture

Testing Unit Testing

Page 3: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Studying impact of climate change on agriculture

Have aerial photos of farms from 1980–83

Testing Unit Testing

Page 4: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Studying impact of climate change on agriculture

Have aerial photos of farms from 1980–83

Want to compare with photos from 2007–presentWant to compare with photos from 2007–present

Testing Unit Testing

Page 5: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Studying impact of climate change on agriculture

Have aerial photos of farms from 1980–83

Want to compare with photos from 2007–presentWant to compare with photos from 2007–present

First step is to find regions where fields overlap

Testing Unit Testing

Page 6: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Luckily, these fields are in Saskatchewan...

Testing Unit Testing

Page 7: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Luckily, these fields are in Saskatchewan...

Testing Unit Testing

...where fields are rectangles

Page 8: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

A student has written a function that finds

the overlap between two rectangles

Testing Unit Testing

Page 9: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

A student has written a function that finds

the overlap between two rectangles

We want to test it before using itWe want to test it before using it

Testing Unit Testing

Page 10: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

A student has written a function that finds

the overlap between two rectangles

We want to test it before using itWe want to test it before using it

We're also planning to try to speed it up...

Testing Unit Testing

Page 11: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

A student has written a function that finds

the overlap between two rectangles

We want to test it before using itWe want to test it before using it

We're also planning to try to speed it up...

...and want tests to make sure we don't break it

Testing Unit Testing

Page 12: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

A student has written a function that finds

the overlap between two rectangles

We want to test it before using itWe want to test it before using it

We're also planning to try to speed it up...

...and want tests to make sure we don't break it

Use Python's Nose library

Testing Unit Testing

Nose

Page 13: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Each test is a function

Testing Unit Testing

Nose

Page 14: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Each test is a function

– Whose name begins with test_

Testing Unit Testing

Nose

Page 15: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Each test is a function

– Whose name begins with test_

Group related tests in filesGroup related tests in files

Testing Unit Testing

Nose

Page 16: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Each test is a function

– Whose name begins with test_

Group related tests in filesGroup related tests in files

– Whose names begin with test_

Testing Unit Testing

Nose

Page 17: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Each test is a function

– Whose name begins with test_

Group related tests in filesGroup related tests in files

– Whose names begin with test_

Run the command nosetests

Testing Unit Testing

Nose

Page 18: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Each test is a function

– Whose name begins with test_

Group related tests in filesGroup related tests in files

– Whose names begin with test_

Run the command nosetests

– Which automatically search the current

directory and sub-directories for tests

Testing Unit Testing

directory and sub-directories for tests

Nose

Page 19: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Simple example: testing dna_starts_with

Testing Unit Testing

Page 20: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Simple example: testing dna_starts_with

def test_starts_with_itself():

dna = 'actgt'dna = 'actgt'

assert dna_starts_with(dna, dna)

def test_starts_with_single_base_pair():

assert dna_starts_with('actg', 'a')

Testing Unit Testing

def does_not_start_with_single_base_pair():

assert not dna_starts_with('ttct', 'a')

Page 21: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Simple example: testing dna_starts_with

def test_starts_with_itself():

dna = 'actgt'Give tests

dna = 'actgt'

assert dna_starts_with(dna, dna)

def test_starts_with_single_base_pair():

assert dna_starts_with('actg', 'a')

meaningful

names

Testing Unit Testing

def does_not_start_with_single_base_pair():

assert not dna_starts_with('ttct', 'a')

Page 22: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Simple example: testing dna_starts_with

def test_starts_with_itself():

dna = 'actgt'Use

dna = 'actgt'

assert dna_starts_with(dna, dna)

def test_starts_with_single_base_pair():

assert dna_starts_with('actg', 'a')

assert

to check

results

Testing Unit Testing

def does_not_start_with_single_base_pair():

assert not dna_starts_with('ttct', 'a')

Page 23: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Simple example: testing dna_starts_with

def test_starts_with_itself():

dna = 'actgt'Use

dna = 'actgt'

assert dna_starts_with(dna, dna)

def test_starts_with_single_base_pair():

assert dna_starts_with('actg', 'a')

variables

for fixtures

to prevent

typing

Testing Unit Testing

def does_not_start_with_single_base_pair():

assert not dna_starts_with('ttct', 'a')

typing

mistaeks

Page 24: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Simple example: testing dna_starts_with

def test_starts_with_itself():

dna = 'actgt'dna = 'actgt'

assert dna_starts_with(dna, dna)

def test_starts_with_single_base_pair():

assert dna_starts_with('actg', 'a')

Testing Unit Testing

def does_not_start_with_single_base_pair():

assert not dna_starts_with('ttct', 'a') Test lots

of cases

Page 25: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

"Test lots of cases"

Testing Unit Testing

Page 26: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

"Test lots of cases"

How many?

Testing Unit Testing

Page 27: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

"Test lots of cases"

How many?

Testing Unit Testing

Page 28: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

"Test lots of cases"

How many?

How to choose cost-effective tests?How to choose cost-effective tests?

Testing Unit Testing

Page 29: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

"Test lots of cases"

How many?

How to choose cost-effective tests?How to choose cost-effective tests?

If we test dna_starts_with('atc', 'a'),

we're unlikely to learn much from testing

dna_starts_with('ttc', 't')

Testing Unit Testing

Page 30: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

"Test lots of cases"

How many?

How to choose cost-effective tests?How to choose cost-effective tests?

If we test dna_starts_with('atc', 'a'),

we're unlikely to learn much from testing

dna_starts_with('ttc', 't')

So choose tests that are as different from each

Testing Unit Testing

So choose tests that are as different from each

other as possible

Page 31: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

"Test lots of cases"

How many?

How to choose cost-effective tests?How to choose cost-effective tests?

If we test dna_starts_with('atc', 'a'),

we're unlikely to learn much from testing

dna_starts_with('ttc', 't')

So choose tests that are as different from each

Testing Unit Testing

So choose tests that are as different from each

other as possible

Look for boundary cases

Page 32: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Apply this to overlapping rectangles

Testing Unit Testing

Page 33: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Apply this to overlapping rectangles

A "normal" case

Testing Unit Testing

Page 34: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Apply this to overlapping rectangles

A "normal" case

What else would be useful?

Testing Unit Testing

Page 35: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Apply this to overlapping rectangles

A "normal" case

What else would be useful?

Testing Unit Testing

Page 36: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Testing Unit Testing

Page 37: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Testing Unit Testing

Page 38: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Testing Unit Testing

Page 39: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Testing Unit Testing

Page 40: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

?

Testing Unit Testing

Page 41: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

?

Testing Unit Testing

?

Page 42: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

? Tests help us define

what "correct"

actually means

Testing Unit Testing

?

actually means

Page 43: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Turn this into code

Testing Unit Testing

Page 44: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Turn this into code

def test_touch_on_corner():

one = ((0, 0), (1, 1))one = ((0, 0), (1, 1))

two = ((1, 1), (2, 2))

assert overlap(one, two) == None

Testing Unit Testing

Page 45: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

Turn this into code

def test_touch_on_corner():

one = ((0, 0), (1, 1))one = ((0, 0), (1, 1))

two = ((1, 1), (2, 2))

assert overlap(one, two) == None

An unambiguous, runnable answer to our

question about touching on corners

Testing Unit Testing

question about touching on corners

Page 46: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

def test_unit_with_itself():

unit = ((0, 0), (1, 1))unit = ((0, 0), (1, 1))

assert overlap(unit, unit) == unit

Testing Unit Testing

Page 47: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

def test_unit_with_itself():

unit = ((0, 0), (1, 1))unit = ((0, 0), (1, 1))

assert overlap(unit, unit) == unit

Wasn't actually in the set of test cases

we came up with earlier

Testing Unit Testing

we came up with earlier

Page 48: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

def test_partial_overlap():

red = ((0, 3), (2, 5))red = ((0, 3), (2, 5))

blue = ((1, 0), (2, 4))

assert overlap(red, blue) == ((1, 3), (2, 4))

Testing Unit Testing

Page 49: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

def test_partial_overlap():

red = ((0, 3), (2, 5))red = ((0, 3), (2, 5))

blue = ((1, 0), (2, 4))

assert overlap(red, blue) == ((1, 3), (2, 4))

This test actually turned up a bug

Testing Unit Testing

Page 50: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

def overlap(red, blue):

'''Return overlap between two rectangles, or None.'''

((red_lo_x, red_lo_y), (red_hi_x, red_hi_y)) = red

((blue_lo_x, blue_lo_y), (blue_hi_x, blue_hi_y)) = blue((blue_lo_x, blue_lo_y), (blue_hi_x, blue_hi_y)) = blue

if (red_lo_x >= blue_hi_x) or (red_hi_x <= blue_lo_x) or \

(red_lo_y >= blue_hi_x) or (red_hi_y <= blue_lo_y):

return None

lo_x = max(red_lo_x, blue_lo_x)

lo_y = max(red_lo_y, blue_lo_y)

hi_x = min(red_hi_x, blue_hi_x)

Testing Unit Testing

hi_x = min(red_hi_x, blue_hi_x)

hi_y = min(red_hi_y, blue_hi_y)

return ((lo_x, lo_y), (hi_x, hi_y))

Page 51: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

def overlap(red, blue):

'''Return overlap between two rectangles, or None.'''

((red_lo_x, red_lo_y), (red_hi_x, red_hi_y)) = red

((blue_lo_x, blue_lo_y), (blue_hi_x, blue_hi_y)) = blue((blue_lo_x, blue_lo_y), (blue_hi_x, blue_hi_y)) = blue

if (red_lo_x >= blue_hi_x) or (red_hi_x <= blue_lo_x) or \

(red_lo_y >= blue_hi_x) or (red_hi_y <= blue_lo_y):

return None

lo_x = max(red_lo_x, blue_lo_x)

lo_y = max(red_lo_y, blue_lo_y)

hi_x = min(red_hi_x, blue_hi_x)

Testing Unit Testing

hi_x = min(red_hi_x, blue_hi_x)

hi_y = min(red_hi_y, blue_hi_y)

return ((lo_x, lo_y), (hi_x, hi_y))

Page 52: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

def overlap(red, blue):

'''Return overlap between two rectangles, or None.'''

((red_lo_x, red_lo_y), (red_hi_x, red_hi_y)) = red

((blue_lo_x, blue_lo_y), (blue_hi_x, blue_hi_y)) = blue((blue_lo_x, blue_lo_y), (blue_hi_x, blue_hi_y)) = blue

if (red_lo_x >= blue_hi_x) or (red_hi_x <= blue_lo_x) or \

(red_lo_y >= blue_hi_x) or (red_hi_y <= blue_lo_y):

return None

lo_x = max(red_lo_x, blue_lo_x)

lo_y = max(red_lo_y, blue_lo_y)

hi_x = min(red_hi_x, blue_hi_x)

Oops

Testing Unit Testing

hi_x = min(red_hi_x, blue_hi_x)

hi_y = min(red_hi_y, blue_hi_y)

return ((lo_x, lo_y), (hi_x, hi_y))

Page 53: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

You should spend your time choosing test cases

and defining their answers

Testing Unit Testing

Page 54: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

You should spend your time choosing test cases

and defining their answers

Nose (and its kin) are there to handle everythingNose (and its kin) are there to handle everything

that you shouldn't re-think each time

Testing Unit Testing

Page 55: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

You should spend your time choosing test cases

and defining their answers

Nose (and its kin) are there to handle everythingNose (and its kin) are there to handle everything

that you shouldn't re-think each time

"The tool shapes the hand"

Testing Unit Testing

Page 56: Testing - v4.software-carpentry.org · Testing Unit Testing. Studying impact of climate change on agriculture Have aerial photos of farms from 1980–83 Want to compare with photos

August 2010

created by

Greg Wilson

August 2010

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.