Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Floating Point Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Passing Arguments Suppose you’re seated in class, which doesn’t begin for another 20 minutes… …and your friend, who missed the last two classes due to.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Unit Testing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
1 Functions Samuel Marateck © A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Program Design CMSC 201. Motivation We’ve talked a lot about certain ‘good habits’ we’d like you guys to get in while writing code. There are two main.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Nanotech Example Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
● Click on Start Button ● Go to Programs and click on Photo Story 3.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Compsci 06/101, Fall How to build a program l Create a module  Can be used by other modules via import  Collection of functions, later collection.
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Inheritance Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
First-Class Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Finding Things Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Let’s get started!.
Operators Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
CS1315: Introduction to Media Computation Making sense of functions.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
CSC 108H: Introduction to Computer Programming
Python Aliasing Copyright © Software Carpentry 2010
Lesson 06: Functions Class Participation: Class Chat:
Program Design Invasion Percolation: Aliasing
Program Design Invasion Percolation: Testing
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
What to do when a test fails
A Lecture for the c++ Course
Program Design Invasion Percolation: Resolving Ties
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
Chapter 5 Conclusion CIS 61.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CHAPTER FOUR Functions.
Writing Functions( ) (Part 5)
Functions BIS1523 – Lecture 17.
Lesson 16: Functions with Return Values
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Libraries of Code Notes from Wilson, Software Design and Development Preliminary Course pp
Recursion Taken from notes by Dr. Neil Moore
Program Design Invasion Percolation: The Grid
Nate Brunelle Today: Functions again, Scope
The Python interpreter
Functions, Procedures, and Abstraction
Presentation transcript:

Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Testing

Fixtures Back to those fields in Saskatchewan...

TestingFixtures Finding areas in photographs where fields overlap

TestingFixtures Finding areas in photographs where fields overlap Each photograph contains one or more rectangles

TestingFixtures Finding areas in photographs where fields overlap Each photograph contains one or more rectangles So a photo is a collection (set? list?) of rectangles

TestingFixtures Finding areas in photographs where fields overlap Each photograph contains one or more rectangles So a photo is a collection (set? list?) of rectangles Want to find all overlaps

TestingFixtures Finding areas in photographs where fields overlap Each photograph contains one or more rectangles So a photo is a collection (set? list?) of rectangles Want to find all overlaps +=

TestingFixtures Have tested overlap_rect(rect_1, rect_2)

TestingFixtures Have tested overlap_rect(rect_1, rect_2) Now want to test overlap_photo(photo_1, photo_2)

TestingFixtures Have tested overlap_rect(rect_1, rect_2) Now want to test overlap_photo(photo_1, photo_2) Imagine its implementation is something like this def overlap_photo(photo_1, photo_2): result = set() for rect_1 in photo_1: for rect_2 in photo_2: temp = overlap_rect(rect_1, rect_2) if temp is not None: result.add(temp) return result

TestingFixtures Have tested overlap_rect(rect_1, rect_2) Now want to test overlap_photo(photo_1, photo_2) Imagine its implementation is something like this def overlap_photo(photo_1, photo_2): result = set() for rect_1 in photo_1: for rect_2 in photo_2: temp = overlap_rect(rect_1, rect_2) if temp is not None: result.add(temp) return result Compare all against all

TestingFixtures Have tested overlap_rect(rect_1, rect_2) Now want to test overlap_photo(photo_1, photo_2) Imagine its implementation is something like this def overlap_photo(photo_1, photo_2): result = set() for rect_1 in photo_1: for rect_2 in photo_2: temp = overlap_rect(rect_1, rect_2) if temp is not None: result.add(temp) return result Save every non-empty overlap

TestingFixtures First test +=

TestingFixtures First test def test_unit_with_unit(): unit = ((0, 0), (1, 1)) photo_1 = { unit } photo_2 = { unit } result = overlap_photo(photo_1, photo_2) assert result == { unit } +=

TestingFixtures First test def test_unit_with_unit(): unit = ((0, 0), (1, 1)) photo_1 = { unit } photo_2 = { unit } result = overlap_photo(photo_1, photo_2) assert result == { unit } That's not too bad +=

TestingFixtures Second test +=

TestingFixtures def test_unit_with_checkerboard(): photo_1 = { ((0, 0), (1, 1)) } photo_2 = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } result = overlap_photo(photo_1, photo_2) assert result == { ((0, 0), (1, 1)) } Second test +=

TestingFixtures def test_unit_with_checkerboard(): photo_1 = { ((0, 0), (1, 1)) } photo_2 = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } result = overlap_photo(photo_1, photo_2) assert result == { ((0, 0), (1, 1)) } That's hard to read Second test +=

TestingFixtures def test_unit_with_checkerboard(): unit = ((0, 0), (1, 1)) photo_1 = { unit } photo_2 = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } result = overlap_photo(photo_1, photo_2) assert result == { unit } Using unit instead of ((0, 0), (1, 1)) doesn't really help much Second test +=

TestingFixtures Third test +=

TestingFixtures Third test def test_unit_checkerboard_with_short_and_wide(): photo_1 = { ((0, 0), (3, 1)) } photo_2 = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } result = overlap_photo(photo_1, photo_2) assert result == { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } +=

TestingFixtures Third test def test_unit_checkerboard_with_short_and_wide(): photo_1 = { ((0, 0), (3, 1)) } photo_2 = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } result = overlap_photo(photo_1, photo_2) assert result == { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } Also hard to read +=

TestingFixtures Third test def test_unit_checkerboard_with_short_and_wide(): photo_1 = { ((0, 0), (3, 1)) } photo_2 = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } result = overlap_photo(photo_1, photo_2) assert result == { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } Also hard to read And a new problem: too much duplicated code +=

TestingFixtures Solution: create fixtures outside specific tests Nose

TestingFixtures Solution: create fixtures outside specific tests (Reminder: the fixture is the thing the test is run on) Nose

TestingFixtures Solution: create fixtures outside specific tests (Reminder: the fixture is the thing the test is run on) If a module contains a function called setup, Nose runs that before it runs any of the tests Nose

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2'

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2' Would actually create fixtures

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2' Would actually run tests

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2' setup test 1.test Ran 2 tests in 0.001s OK 

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2'  This is Nose's usual output setup test 1.test Ran 2 tests in 0.001s OK

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2'  Would look like this without our print statements Ran 2 tests in 0.001s OK

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2' setup test 1.test Ran 2 tests in 0.001s OK  Nose runs setup once at the start

TestingFixtures Here's how it works import sys def setup(): print >> sys.stderr, 'setup' def test_1(): print >> sys.stderr, 'test 1' def test_2(): print >> sys.stderr, 'test 2'  Then runs tests (in any order) setup test 1.test Ran 2 tests in 0.001s OK

TestingFixtures Create fixtures for testing photo overlap

TestingFixtures Create fixtures for testing photo overlap Photos = {} def setup(): Photos['unit'] = { ((0, 0), (1, 1)) } Photos['checkerboard'] = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } Photos['short_and_wide'] = { ((0, 0), (3, 1)) }

TestingFixtures Create fixtures for testing photo overlap Photos = {} def setup(): Photos['unit'] = { ((0, 0), (1, 1)) } Photos['checkerboard'] = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } Photos['short_and_wide'] = { ((0, 0), (3, 1)) } Store fixtures in a global variable so they're visible in every test

TestingFixtures Create fixtures for testing photo overlap Photos = {} def setup(): Photos['unit'] = { ((0, 0), (1, 1)) } Photos['checkerboard'] = { ((0, 0), (1, 1)), ((1, 0), (2, 1)), ((0, 1), (1, 2)), ((1, 1), (2, 2)) } Photos['short_and_wide'] = { ((0, 0), (3, 1)) } Create fixtures once before tests are run

TestingFixtures Then use fixtures in tests

TestingFixtures Then use fixtures in tests def test_unit_with_unit(): temp = overlap_rect(Photos['unit'], Photos['unit']) assert temp == Photos['unit']

TestingFixtures Then use fixtures in tests def test_unit_with_unit(): temp = overlap_rect(Photos['unit'], Photos['unit']) assert temp == Photos['unit'] def test_checkerboard_with_short_and_wide(): temp = overlap_rect(Photos['checkerboard'], Photos['short_and_wide']) assert temp == { ((0, 0), (1, 1)), ((1, 0), (2, 1)) }

TestingFixtures Could create one global variable per fixture

TestingFixtures Could create one global variable per fixture Unit = None Short_And_Wide = None def setup(): Unit = { ((0, 0), (1, 1)) } Short_And_Wide = { ((0, 0), (3, 1)) }

TestingFixtures A matter of taste and style Could create one global variable per fixture Unit = None Short_And_Wide = None def setup(): Unit = { ((0, 0), (1, 1)) } Short_And_Wide = { ((0, 0), (3, 1)) }

TestingFixtures Don't actually need setup in this case Unit = { ((0, 0), (1, 1)) } Short_And_Wide = { ((0, 0), (3, 1)) }

TestingFixtures But this doesn't generalize Don't actually need setup in this case Unit = { ((0, 0), (1, 1)) } Short_And_Wide = { ((0, 0), (3, 1)) }

TestingFixtures What if tests modify fixtures?

TestingFixtures What if tests modify fixtures? Example: photo_crop(photo, rect) removes all rectangles in photo that are completely outside the given cropping window

TestingFixtures What if tests modify fixtures? Example: photo_crop(photo, rect) removes all rectangles in photo that are completely outside the given cropping window This means it isn't safe to re-use fixtures

TestingFixtures What if tests modify fixtures? Example: photo_crop(photo, rect) removes all rectangles in photo that are completely outside the given cropping window This means it isn't safe to re-use fixtures So re-create fixtures for each test

TestingFixtures Use a decorator for per-test setup

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2'

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' Import the decorator from the Nose library

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' Import the decorator from the Nose library (It's actually just a function that behaves a specific way)

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' to apply it to a function

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' to apply it to a function Tells Nose to run setup_each before running the test

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' setup each test 1.setup each test Ran 2 tests in 0.001s OK 

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' setup each test 1.setup each test Ran 2 tests in 0.001s OK  Standard Nose output

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' setup each test 1.setup each test Ran 2 tests in 0.001s OK  Nose ran setup_each before test_1

TestingFixtures Use a decorator for per-test setup import sys from nose import with_setup def setup_each(): print >> sys.stderr, 'setup def test_1(): print >> sys.stderr, 'test def test_2(): print >> sys.stderr, 'test 2' setup each test 1.setup each test Ran 2 tests in 0.001s OK  And then again before test_2

TestingFixtures from nose import with_setup checkerboard = None unit = None whole_map = def test_crop_unit(): photo_crop(checkerboard, unit) assert checkerboard == def test_crop_keep_everything(): original = photo_copy(checkerboard) photo_crop(checkerboard, whole_map) assert checkerboard == original

TestingFixtures create_fixtures – Create first copy of checkerboard from nose import with_setup checkerboard = None unit = None whole_map = def test_crop_unit(): photo_crop(checkerboard, unit) assert checkerboard == def test_crop_keep_everything(): original = photo_copy(checkerboard) photo_crop(checkerboard, whole_map) assert checkerboard == original

TestingFixtures create_fixtures – Create first copy of checkerboard test_crop_unit – Modify checkerboard from nose import with_setup checkerboard = None unit = None whole_map = def test_crop_unit(): photo_crop(checkerboard, unit) assert checkerboard == def test_crop_keep_everything(): original = photo_copy(checkerboard) photo_crop(checkerboard, whole_map) assert checkerboard == original

TestingFixtures create_fixtures – Create first copy of checkerboard test_crop_unit – Modify checkerboard create_fixtures – Creates fresh copy of checkerboard from nose import with_setup checkerboard = None unit = None whole_map = def test_crop_unit(): photo_crop(checkerboard, unit) assert checkerboard == def test_crop_keep_everything(): original = photo_copy(checkerboard) photo_crop(checkerboard, whole_map) assert checkerboard == original

TestingFixtures create_fixtures – Create first copy of checkerboard test_crop_unit – Modify checkerboard create_fixtures – Creates fresh copy of checkerboard test_copy_keep_everything – Modify checkerboard again from nose import with_setup checkerboard = None unit = None whole_map = def test_crop_unit(): photo_crop(checkerboard, unit) assert checkerboard == def test_crop_keep_everything(): original = photo_copy(checkerboard) photo_crop(checkerboard, whole_map) assert checkerboard == original

TestingFixtures Re-running setup wastes a few microseconds of the computer's time

TestingFixtures Re-running setup wastes a few microseconds of the computer's time That is much less valuable than any of yours

TestingFixtures Decorators aren't magic

TestingFixtures Decorators aren't magic But they are tricky

TestingFixtures Decorators aren't magic But they are tricky You don't have to understand how they work

TestingFixtures Decorators aren't magic But they are tricky You don't have to understand how they work Just as you don't have to understand how Nose finds test in files or files that contain tests

TestingFixtures Decorators aren't magic But they are tricky You don't have to understand how they work Just as you don't have to understand how Nose finds test in files or files that contain tests As long as you know:

TestingFixtures Decorators aren't magic But they are tricky You don't have to understand how they work Just as you don't have to understand how Nose finds test in files or files that contain tests As long as you know: – does

TestingFixtures Decorators aren't magic But they are tricky You don't have to understand how they work Just as you don't have to understand how Nose finds test in files or files that contain tests As long as you know: – does – When and why to use it

August 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.