Unittest in five minutes Ray Toal 2011-10-22. How to unit test Not manually, that's for sure You write code that exercises your code Perform assertions.

Slides:



Advertisements
Similar presentations
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Advertisements

Variables and References in Python. "TAGAGAATTCTA” Objects s Names References >>> s = “TAGAGAATTCTA” >>>
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Test Driven Development George Mason University. Today’s topics Review of Chapter 1: Testing Go over examples and questions testing in Java with Junit.
Chapter 2 Writing Simple Programs
CSC 9010: Natural Language Processing
1 Spidering the Web in Python CSC 161: The Art of Programming Prof. Henry Kautz 11/23/2009.
Exceptions COMPSCI 105 S Principles of Computer Science.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
Test Driven Development George Mason University. Today’s topics Review of Chapter 1: Testing Go over examples and questions testing in Python.
Software Development Software Testing. Testing Definitions There are many tests going under various names. The following is a general list to get a feel.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python iterators and generators. Iterators and generators  Python makes good use of iterators  And has a special kind of generator function that is.
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
Python Conditionals chapter 5
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Python Let’s get started!.
Variables, Types, Expressions Intro2CS – week 1b 1.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Lecture 4 Python Basics Part 3.
Debugging Ruth Anderson UW CSE 160 Winter
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming
Object Oriented Testing (Unit Testing)
Chapter 2 Writing Simple Programs
Software Development.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Example: Vehicles What attributes do objects of Sedan have?
Python Let’s get started!.
Ruth Anderson UW CSE 160 Winter 2017
Topics Introduction to File Input and Output
Ruth Anderson CSE 140 University of Washington
Exception Handling.
Python’s Errors and Exceptions
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Ruth Anderson UW CSE 160 Spring 2018
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Passing Parameters by value
Iteration: Beyond the Basic PERFORM
ERRORS AND EXCEPTIONS Errors:
Michael Ernst CSE 140 University of Washington
Python programming exercise
Testing 24-Feb-19.
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
By Ryan Christen Errors and Exceptions.
Chopin’s Nocturne.
Topics Introduction to File Input and Output
Debugging UW CSE 160.
Python iterators and generators
Python Reserved Words Poster
Presentation transcript:

unittest in five minutes Ray Toal

How to unit test Not manually, that's for sure You write code that exercises your code Perform assertions that record o that you get the results you expect for various inputs o that exceptions are raised when they should be Pay attention to edge cases

Test-driven development Write some tests (yes, first) Run them o They will fail because the code isn't written yet. That is supposed to happen. Good news is you will know the tests run. Write some code Now run the tests again Work until the tests pass Iterate!

Unit testing in Python The module unittest is already there Docs Python 2.7 Python 3 Also see The unit testing chapter from Dive Into Python The unit testing chapter from Dive Into Python Are there alternatives to unittest? See what they say on StackOverflowSee what they say on StackOverflow

A first example Let's write a function to interleave two lists It will be okay if one list is longer than the other Before we start writing the code, we should know what the function should produce for all types of inputs: interleave([], []) ☞ [] interleave([1,5,3], ["hello"]) ☞ [1,"hello",5,3] interleave([True], [[], 8]) ☞ [True, [], 8]

Write the test first (interleavetest.py) from interleave import interleave import unittest class TestGettingStartedFunctions(unittest.TestCase): def test_interleave(self): cases = [ ([], [], []), ([1, 4, 6], [], [1, 4, 6]), ([], [2, 3], [2, 3]), ([1], [9], [1, 9]), ([8, 8, 3, 9], [1], [8, 1, 8, 3, 9]), ([2], [7, 8, 9], [2, 7, 8, 9]), ] for a, b, expected in cases: self.assertEqual(interleave(a, b), expected)

Write a stub (interleave.py) def interleave(a, b): return None

Run the test $ python interleavetest.py F ====================================================================== FAIL: test_interleave (__main__.TestGettingStartedFunctions) Traceback (most recent call last): File "interleavetest.py", line 15, in test_interleave self.assertEqual(interleave(a, b), expected) AssertionError: None != [] Ran 1 test in 0.000s FAILED (failures=1)

Now write the code def interleave(a, b): """Return the interleaving of two sequences as a list.""" return [y for x in izip_longest(a, b) for y in x if y is not None]

Test again $ python interleavetest.py E ====================================================================== ERROR: test_interleave (__main__.TestGettingStartedFunctions) Traceback (most recent call last): File "interleavetest.py", line 15, in test_interleave self.assertEqual(interleave(a, b), expected) File "/Users/raytoal/scratch/interleave.py", line 3, in interleave return [y for x in izip_longest(a, b) for y in x if y is not None] NameError: global name 'izip_longest' is not defined Ran 1 test in 0.000s FAILED (errors=1)

Fix the code from itertools import izip_longest def interleave(a, b): """Return the interleaving of two sequences as a list.""" return [y for x in izip_longest(a, b) for y in x if y is not None]

Rerun the test $ python interleavetest.py Ran 1 test in 0.000s OK

kthx