testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · testing luis pedro coelho...

21
. . Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists) Testing October 22, 2012 (1 / 16)

Upload: others

Post on 29-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 2: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 3: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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?

Page 4: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 5: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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.

Page 6: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 7: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 8: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 9: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 10: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 11: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 12: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 13: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 14: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 15: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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?

Page 16: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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?

Page 17: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 18: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 19: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 20: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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

Page 21: Testing - luispedro.orgluispedro.org/files/pfs/09-12/11-tests.pdf · Testing Luis Pedro Coelho Programming for Scientists October 22, 2012 Luis Pedro Coelho (Programming for Scientists)

..

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