• No results found

Unit testing using Python nose

N/A
N/A
Protected

Academic year: 2021

Share "Unit testing using Python nose"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)

Unit testing using Python nose

Luis Pedro Coelho §

On the web: http://luispedro.org On twitter: @luispedrocoelho

European Molecular Biology Laboratory

June 11, 2014

(2)

Motivation

Scientific code must not just produce nice looking output, but actually be correct.

(3)

Motivation (II)

Some recent code-related scientific catastrophes: Geoffrey Chang

Abortion reduces crime? maybe not so much once you fix the bug Rogoff’s “Growth in a Time of Debt” paper is a famous example (even if the bug itself is only a small part of the counter-argument) East Anglia “Climategate”

(4)

Why do things go wrong?

.

..

1 Your code is correct, but input files are wrong/missing/, the

network goes down …

.

..

(5)

Never fail silently!

The worst thing is to fail silently. Fail loudly and clearly

(This is partially why Unix tradition is to produce no output when things go well)

(6)

Defensive Programming

(7)

Assertions

d e f s t d d e v(v a l u e s) : ’ ’ ’ S = s t d d e v ( v a l u e s ) Compute s t a n d a r d d e v i a t i o n ’ ’ ’ a s s e r t l e n (v a l u e s) > 0, ’ s t d d e v : g o t empty l i s t . ’ . . .

(8)

Assertions

d e f s t d d e v(v a l u e s) : ’ ’ ’ S = s t d d e v ( v a l u e s ) Compute s t a n d a r d d e v i a t i o n ’ ’ ’ i f l e n (v a l u e s) <= 0: r a i s e A s s e r t i o n E r r o r( ’ s t d d e v : g o t empty l i s t . ’) . . .

(9)

Preconditions

In computer programming, a precondition is a condition or predicate that must always be true just prior to the execution of some section of code.

(Wikipedia)

(10)

Preconditions

Other Languages

C/C++#include <assert.h>

Java assert pre-condition

Matlab assert () (in newer versions)

(11)

Assertions Are Not Error Handling!

Error handling protects against outside events; assertions protect against programmer mistakes. Assertions should neverbe false.

(12)

Programming by Contract

. .. 1 pre-conditions. . .. 2 post-conditions. . .. 3 invariants.

(13)

Pre-condition

What must be true before calling a function.

Post-condition

What is true after calling a function.

(14)

Testing

(15)

Unit Testing

d e f t e s t _ s t d d e v _ c o n s t( ) :

a s s e r t s t d d e v( [1] *100) < 1e-3

d e f t e s t _ s t d d e v _ p o s i t i v e( ) :

a s s e r t s t d d e v( r a n g e (20) ) > 0.

(16)

Nosetest

Nose software testing framework: Tests are named test_something. Conditions are asserted.

(17)

Software Testing Philosophies

.

..

1 Test everything. Test it twice.

. .. 2 Write tests first. . .. 3 Regression testing.

(18)

Regression Testing

(19)

Practical Session: some preliminaries

statistics.py d e f s t d d e v(xs) : . . . test_statistics.py d e f t e s t _ s t d d e v _ c o n s t( ) : a s s e r t s t d d e v( [1] *100) < 1e-3 d e f t e s t _ s t d d e v _ p o s i t i v e( ) : a s s e r t s t d d e v( r a n g e (20) ) > 0.

(20)

Practical Session: some preliminaries

statistics.py d e f s t d d e v(xs) : . . . test_statistics.py import s t a t i s t i c s d e f t e s t _ s t d d e v _ c o n s t( ) : a s s e r t s t a t i s t i c s . s t d d e v( [1] *100) < 1e-3 d e f t e s t _ s t d d e v _ p o s i t i v e( ) : a s s e r t s t a t i s t i c s . s t d d e v( r a n g e (20) ) > 0.

(21)

Practical: Python III & Unit testing

.

..

1 You can either start from scratch or check the files I give you

(or any combination of both).

.

..

2 Goal is to write code to do a simple task & test it.

(22)

Types of tests

Smoke test: just check it runs

Corner/edge cases: check “complex” cases. Case testing: test a “known case”

Regression testing: create a test when you find a bug. Integration test: test that different parts work together.

(23)

Goals

.

..

1 Download files from

https://github.com/luispedro/2014-06-10-cyi-support/testing

.

..

2 There is a data file (data.txt)

.

..

3 See the code in main.py, which loads it.

.

..

4 Write a function average in a file called robust.py, which computes

the average of a sequence of numbers, whilst ignoring the maximum and minimum.

.

..

5 Write tests for robust.average.

.

..

6 (If you have the time, you can look at plots.py)

References

Related documents

OpenFOAM® directory organization $WM_PROJECT_DIR applications bin doc etc platforms src tutorials wmake Allwmake.. If you installed OpenFOAM ® in

66c ccSalter Harris

October 4, 7:30PM Knox College Kresge Recital Hall October 6, 4:00PM Bucknell University Natalie Davis Rooke

As shown in table 2at a particular frequency (2.45GHz), the gain was also simulated and measured, it can be seen that the gain was almost equal, though there

The most important face recognition problem via thermal IR images is the existence of diffusion obstacles like glasses, which blocks an accurate extraction of the

Başak Yönetim was established in 2008 for the purpose of building and operating of electricity production facility and producing, selling and marketing of

¾ In silico systems are extensively used during the early phases of drug development until selection of the clinical candidate (e.g. 3D-modeling, expert systems, QSAR tools).