Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
..
Testing
Luis Pedro Coelho
Programming for Scientists
October 22, 2012
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (1 / 16) .
..
Testing
Luis Pedro Coelho
Programming for Scientists
October 22, 2012
..20
12-1
0-22
Testing
..
Defensive Programming
Defensive programming means writing code that will catch bugs early.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (2 / 16) .
..
Defensive Programming
Defensive programming means writing code that will catch bugs early.
..20
12-1
0-22
Testing
Defensive Programming
..
Remember the Homework?
de f tr im ( qs , thresh ) :. . .a s s e r t thresh >= 0 , ’ th r e sho ld should be p o s i t i v e ’
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (3 / 16) .
..
Remember the Homework?
de f tr im ( qs , thresh ) :. . .a s s e r t thre sh >= 0 , ’ th r e sho ld should be p o s i t i v e ’
..20
12-1
0-22
Testing
Remember the Homework?
..
Assert
a s s e r t <cond i t i on>, <e r r o r message>
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (4 / 16) .
..
Assert
a s s e r t <cond i t i on>, <e r r o r message>
..20
12-1
0-22
Testing
Assert
Catch errors earlyGive good error messages
..
Testing
Do you test your code?
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (5 / 16) .
..
Testing
Do you test your code?
..20
12-1
0-22
Testing
Testing
Of course you do, interactively, informally.Here we are only going to make this automatic.
..
Testing trim
import numpy as npfrom tr imfq import tr im
qs = np . array ( [ ] )tr im ( qs , 20 )
qs = np . array ( [ 20 , 20 ] )tr im ( qs , 20 )
These simple sort of tests are called smoke tests.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (6 / 16) .
..
Testing trim
import numpy as npfrom tr imfq import trim
qs = np . array ( [ ] )tr im ( qs , 20 )
qs = np . array ( [ 20 , 20 ] )tr im ( qs , 20 )
These simple sort of tests are called smoke tests.
..20
12-1
0-22
Testing
Testing trim
A “real” smoke test is when you put smoke through pipes and checkfor leaks
..
Testing trim
import numpy as npfrom tr imfq import tr im
qs = np . array ( [ ] )tr im ( qs , 20 )
qs = np . array ( [ 20 , 20 ] )tr im ( qs , 20 )
These simple sort of tests are called smoke tests.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (6 / 16) .
..
Testing trim
import numpy as npfrom tr imfq import trim
qs = np . array ( [ ] )tr im ( qs , 20 )
qs = np . array ( [ 20 , 20 ] )tr im ( qs , 20 )
These simple sort of tests are called smoke tests.
..20
12-1
0-22
Testing
Testing trim
A “real” smoke test is when you put smoke through pipes and checkfor leaks
..
Testing trim II
qs = np . array ( [ 10 , 10 , 10 , 20 , 20 , 20 , 20 , 10 ] )s , e = trim ( qs , 15 )a s s e r t np . a l l ( qs [ s : e ] >= 15 )a s s e r t s < e
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (7 / 16) .
..
Testing trim II
qs = np . array ( [ 10 , 10 , 10 , 20 , 20 , 20 , 20 , 10 ] )s , e = trim ( qs , 15 )a s s e r t np . a l l ( qs [ s : e ] >= 15 )a s s e r t s < e
..20
12-1
0-22
Testing
Testing trim II
..
Testing trim III
Where are errors likely to lurk?
At the edges?What if the whole string is above threshold?What if the whole string is below threshold?
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (8 / 16) .
..
Testing trim III
Where are errors likely to lurk?
At the edges?What if the whole string is above threshold?What if the whole string is below threshold?
..20
12-1
0-22
Testing
Testing trim III
..
Testing trim III
Where are errors likely to lurk?
At the edges?What if the whole string is above threshold?What if the whole string is below threshold?
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (8 / 16) .
..
Testing trim III
Where are errors likely to lurk?
At the edges?What if the whole string is above threshold?What if the whole string is below threshold?
..20
12-1
0-22
Testing
Testing trim III
..
Testing trim IV
s , e = trim (np . array ( [ 10 , 10 , 10 , 10 ] ) , 5 )a s s e r t s == 0a s s e r t e == 4
s , e = trim (np . array ( [ 10 , 10 , 10 , 10 ] ) , 15 )a s s e r t s == e # Note that we
# DO NOT care about# actua l va lue s
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (9 / 16) .
..
Testing trim IV
s , e = trim (np . array ( [ 10 , 10 , 10 , 10 ] ) , 5 )a s s e r t s == 0a s s e r t e == 4
s , e = trim (np . array ( [ 10 , 10 , 10 , 10 ] ) , 15 )a s s e r t s == e # Note that we
# DO NOT care about# actua l va lue s
..20
12-1
0-22
Testing
Testing trim IV
..
Testing trim V
s , e = trim (np . array ( [ 10 , 10 , 20 , 20 ] ) , 15 )a s s e r t s == 2a s s e r t e == 4
s , e = trim (np . array ( [ 20 , 20 , 10 , 10 ] ) , 15 )a s s e r t s == 0a s s e r t e == 2
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (10 / 16) .
..
Testing trim V
s , e = trim (np . array ( [ 10 , 10 , 20 , 20 ] ) , 15 )a s s e r t s == 2a s s e r t e == 4
s , e = trim (np . array ( [ 20 , 20 , 10 , 10 ] ) , 15 )a s s e r t s == 0a s s e r t e == 2
..20
12-1
0-22
Testing
Testing trim V
..
Fencepost Errors
If you build a straight fence 100 meters long with posts 10 metersapart, how many posts do you need?
Eleven, but we often think 10.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (11 / 16) .
..
Fencepost Errors
If you build a straight fence 100 meters long with posts 10 metersapart, how many posts do you need?
Eleven, but we often think 10.
..20
12-1
0-22
Testing
Fencepost Errors
..
Fencepost Errors
If you build a straight fence 100 meters long with posts 10 metersapart, how many posts do you need?
Eleven, but we often think 10.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (11 / 16) .
..
Fencepost Errors
If you build a straight fence 100 meters long with posts 10 metersapart, how many posts do you need?
Eleven, but we often think 10.
..20
12-1
0-22
Testing
Fencepost Errors
..
What is the use of testing?
Ok, I tested itIt seems to workNow, I am happy
But save those tests!
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (12 / 16) .
..
What is the use of testing?
Ok, I tested itIt seems to workNow, I am happy
But save those tests!
..20
12-1
0-22
Testing
What is the use of testing?
..
What is the use of testing?
Ok, I tested itIt seems to workNow, I am happyBut save those tests!
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (12 / 16) .
..
What is the use of testing?
Ok, I tested itIt seems to workNow, I am happyBut save those tests!
..20
12-1
0-22
Testing
What is the use of testing?
..
When your code changes
When your code changes…
…you rerun your tests.Over time, you will accumulate a collection of tests.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (13 / 16) .
..
When your code changes
When your code changes…
…you rerun your tests.Over time, you will accumulate a collection of tests.
..20
12-1
0-22
Testing
When your code changes
..
When your code changes
When your code changes……you rerun your tests.Over time, you will accumulate a collection of tests.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (13 / 16) .
..
When your code changes
When your code changes……you rerun your tests.Over time, you will accumulate a collection of tests.
..20
12-1
0-22
Testing
When your code changes
..
Software Testing Philosophies
...1 Test everything. Test it twice.
...2 Write tests first.
...3 Regression testing.
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (14 / 16) .
..
Software Testing Philosophies
...1 Test everything. Test it twice.
...2 Write tests first.
...3 Regression testing.
..20
12-1
0-22
Testing
Software Testing Philosophies
..
Regression Testing
Make sure bugs only appear once!
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (15 / 16) .
..
Regression Testing
Make sure bugs only appear once!
..20
12-1
0-22
Testing
Regression Testing
..
Nose testing
Many utilities already exist to help manage test suites(A test suite is a fancy name for “a bunch of tests).In Python, nose is the most popular one.
http://nose.readthedocs.org/en/latest/
Luis Pedro Coelho (Programming for Scientists) ⋆ Testing ⋆ October 22, 2012 (16 / 16) .
..
Nose testing
Many utilities already exist to help manage test suites(A test suite is a fancy name for “a bunch of tests).In Python, nose is the most popular one.
http://nose.readthedocs.org/en/latest/
..20
12-1
0-22
Testing
Nose testing
Demo it.Show Coverage HTML