Unit testing using Python nose



Similar documents
CS 1133, LAB 2: FUNCTIONS AND TESTING

How To Understand The Details Of A Function In Python (For Beginners)

A Laboratory Information. Management System for the Molecular Biology Lab

Research Data Management CODING

Software security specification and verification

Loop Invariants and Binary Search

National University of Ireland, Maynooth MAYNOOTH, CO. KILDARE, IRELAND. Testing Guidelines for Student Projects

Testing Introduction. IEEE Definitions

PHP Debugging. Draft: March 19, Christopher Vickery

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Lecture Notes on Linear Search

Software Development Phases

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

PL/SQL Practicum #2: Assertions, Exceptions and Module Stability

CME 193: Introduction to Scientific Python Lecture 8: Unit testing, more modules, wrap up

mate of your wit Case Study 1

Software Engineering Techniques

Committee on WIPO Standards (CWS)

How to Outsource Without Being a Ninnyhammer

Data Intensive Computing Handout 5 Hadoop

Software Construction

X05. An Overview of Source Code Scanning Tools. Loulwa Salem. Las Vegas, NV. IBM Corporation IBM System p, AIX 5L & Linux Technical University

4. Test Design Techniques

Using Files as Input/Output in Java 5.0 Applications

Evaluation of AgitarOne

Fast string matching

Data Intensive Computing Handout 6 Hadoop

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Lessons Learned in Software Testing

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

FCCS Tutorial INSTRUCTOR S GUIDE

Eclipse Help

Introduction to Programming System Design. CSCI 455x (4 Units)

Ruby in the context of scientific computing

02-201: Programming for Scientists

The first program: Little Crab

On the Brittleness of Software and the Infeasibility of Security Metrics

Tool-Assisted Unit-Test Generation and Selection Based on Operational Abstractions

A Short Course in Logic Zeno s Paradox

2 Introduction to Nintex Workflow

BLOCK OCCUPANCY DETECTOR WITH SEMAPHORE OPERATION BOD1/DAP4-BR

CIS 192: Lecture 13 Scientific Computing and Unit Testing

System Level Integration and Test Leveraging Software Unit Testing Techniques

2 The first program: Little Crab

Dhany Saputra. Summary. Experience. Doctoral Candidate in Bioinformatics

Design by Contract beyond class modelling

Fully Automated Static Analysis of Fedora Packages

DATEX II User Support

Python Programming: An Introduction to Computer Science

Numerical Analysis. Professor Donna Calhoun. Fall 2013 Math 465/565. Office : MG241A Office Hours : Wednesday 10:00-12:00 and 1:00-3:00

Automatic promotion and versioning with Oracle Data Integrator 12c

Applied Software Project Management

From Raw Data to. Actionable Insights with. MATLAB Analytics. Learn more. Develop predictive models. 1Access and explore data

Extreme Programming and Embedded Software Development

CHAPTER 01 THE SCOPE OF SOFTWARE ENGINEERING

RepoGuard Validation Framework for Version Control Systems

Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)

Lab 2.1 Tracking Down the Bugs

Should Business Move To The Cloud

Hoare-Style Monitors for Java

Report on the Examination

3 Extending the Refinement Calculus

Python as a Tool for Squeezing More Learning into Mathematical Physics: Powerful, Versatile, and Free. Geoffrey Poore Assistant Professor of Physics

Adapting C++ Exception Handling to an Extended COM Exception Model

Show me the tests! Writing Automated Tests for Drupal

BuildBot. S.Cozzini/A.Messina/G.Giuliani. And Continuous Integration. RegCM4 experiences. Warning: Some slides/ideas.

Secure Programming with Static Analysis. Jacob West

Modern Web Application Framework Python, SQL Alchemy, Jinja2 & Flask

Software Configuration Management

I don t intend to cover Python installation, please visit the Python web site for details.

CS1102: Adding Error Checking to Macros

Introduction to Python

1. A is a tax system which has higher tax rates on people with lower incomes.

Project Management Tools

Generate Android App

Robustness Testing of the Microsoft Win32 API

Experiences with Online Programming Examinations

The way to good code

How To Write A Test Engine For A Microsoft Microsoft Web Browser (Php) For A Web Browser For A Non-Procedural Reason)

Chapter 8 Software Testing

Introduction to Scientific Computing

CS 106 Introduction to Computer Science I

Kernel Types System Calls. Operating Systems. Autumn 2013 CS4023

How to Design and Create Your Own Custom Ext Rep

Writing in the Computer Science Major

Rigorous Software Development CSCI-GA

Using Excel for Statistical Analysis

Test Plan Airline Reservation System

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Part II. Managing Issues

Specification and Analysis of Contracts Lecture 1 Introduction

Managing XML Documents Versions and Upgrades with XSLT

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Chapter 14. Programming and Languages. McGraw-Hill/Irwin. Copyright 2008 by The McGraw-Hill Companies, Inc. All rights reserved.

Wrestling with Python Unit testing. Warren Viant

<Insert Picture Here> What's New in NetBeans IDE 7.2

Transcription:

Unit testing using Python nose Luis Pedro Coelho On the web: http://luispedro.org On twitter: @luispedrocoelho European Molecular Biology Laboratory June 11, 2014 Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (1 / 23)

Motivation Scientific code must not just produce nice looking output, but actually be correct. http://bit.ly/testing-science Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (2 / 23)

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 Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (3 / 23)

Why do things go wrong?.1 Your code is correct, but input files are wrong/missing/, the network goes down.2 Your code is buggy. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (4 / 23)

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) Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (5 / 23)

Defensive Programming Defensive programming means writing code that will catch bugs early. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (6 / 23)

Assertions def stddev ( v a l u e s ) : S = stddev ( v a l u e s ) Compute standard 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, stddev : got empty l i s t.... Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (7 / 23)

Assertions def stddev ( v a l u e s ) : S = stddev ( v a l u e s ) Compute standard 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 ( stddev : got empty l i s t. )... Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (8 / 23)

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) Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (9 / 23)

Preconditions Other Languages C/C++ #include <assert.h> Java assert pre-condition Matlab assert () (in newer versions) Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (10 / 23)

Assertions Are Not Error Handling! Error handling protects against outside events; assertions protect against programmer mistakes. Assertions should never be false. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (11 / 23)

Programming by Contract.1 pre-conditions..2 post-conditions..3 invariants. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (12 / 23)

Pre-condition What must be true before calling a function. Post-condition What is true after calling a function. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (13 / 23)

Testing Do you test your code? Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (14 / 23)

Unit Testing def test_stddev_const ( ) : a s s e r t stddev ( [ 1 ] * 100 ) < 1e - 3 def t e s t _ s t d d e v _ p o s i t i v e ( ) : a s s e r t stddev ( range ( 20 ) ) > 0. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (15 / 23)

Nosetest Nose software testing framework: Tests are named test_something. Conditions are asserted. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (16 / 23)

Software Testing Philosophies.1 Test everything. Test it twice..2 Write tests first..3 Regression testing. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (17 / 23)

Regression Testing Make sure bugs only appear once! Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (18 / 23)

Practical Session: some preliminaries statistics.py def stddev ( xs ) :... test_statistics.py def test_stddev_const ( ) : a s s e r t stddev ( [ 1 ] * 100 ) < 1e - 3 def t e s t _ s t d d e v _ p o s i t i v e ( ) : a s s e r t stddev ( range ( 20 ) ) > 0. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (19 / 23)

Practical Session: some preliminaries statistics.py def stddev ( xs ) :... test_statistics.py import s t a t i s t i c s def test_stddev_const ( ) : a s s e r t s t a t i s t i c s. stddev ( [ 1 ] *100 ) < 1e - 3 def 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. stddev ( range ( 20 ) ) > 0. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (20 / 23)

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. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (21 / 23)

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. Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (22 / 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) Luis Pedro Coelho (luis@luispedro.org) (EMBL) Unit testing June 11, 2014 (23 / 23)