CSE : Python Programming. Packages (Tutorial, Section 6.4) Announcements. Today. Packages: Concretely. Packages: Overview

Similar documents
CSE : Python Programming

CSE : Python Programming

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Specification-Based Testing 1

Object-Oriented Design Lecture 21 CSU 370 Fall 2008 (Pucella) Tuesday, Dec 9, 2007

JUnit in EDA Introduction. 2 JUnit 4.3

Exception Handling. Genome 559

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Class extension and. Exception handling. Genome 559

Outline. Outline. 1 Chapter 2: Data Abstraction

Making Programs Fail. Andreas Zeller

Testing and Debugging

Objects and Classes. Chapter 8

6.001 Notes: Section 15.1


CS103 Spring 2018 Mathematical Vocabulary

Practical Objects: Test Driven Software Development using JUnit

Scripting With Jython

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Testing. UW CSE 160 Winter 2016

Advanced topics, part 2

Intro. Scheme Basics. scm> 5 5. scm>

CS354 gdb Tutorial Written by Chris Feilbach

LECTURE 2. Python Basics

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

JUnit Test Patterns in Rational XDE

Argparse Tutorial Release 2.7.9

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing

Starting to Program in C++ (Basics & I/O)

Agile development. Agile development work cycle. Reacting to bugs

Software testing A.A. 2018/2019

Slide 1 CS 170 Java Programming 1 Testing Karel

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Class extension and. Exception handling. Genome 559

Chrome if I want to. What that should do, is have my specifications run against four different instances of Chrome, in parallel.

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Homework 09. Collecting Beepers

Object Oriented Software Design - I

Tuesday, November 15. Testing

Regression testing. Whenever you find a bug. Why is this a good idea?

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Modules and Programs 1 / 14

6.001 Notes: Section 8.1

Marthon User Guide. Page 1 Copyright The Marathon developers. All rights reserved.

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

Unit Testing as Hypothesis Testing

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

Django Test Utils Documentation

Software Testing Lecture 1. Justin Pearson

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Testing and Debugging

Reviewing gcc, make, gdb, and Linux Editors 1

Assignment 5: Testing and Debugging

1 Getting used to Python

Final CSE 131B Spring 2004

Representing Documents; Unit Testing II

QUIZ. What is wrong with this code that uses default arguments?

JUnit Howto. Blaine Simpson

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

Programming Languages and Techniques (CIS120)

mismatch between what is maybe possible today and what is going on in many of today's IDEs.

An Introduction to Python

Programming Languages and Techniques (CIS120)

In today s video I'm going show you how you can set up your own online business using marketing and affiliate marketing.

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Tools for Unit Test - JUnit

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

Test-Driven Development (TDD)

CSE 113 A. Announcements - Lab

CS1210 Lecture 28 Mar. 27, 2019

Jtest Tutorial. Tutorial

Thursday, February 16, More C++ and root

Lecture 9: July 14, How to Think About Debugging

Testing Exceptions with Enforcer

Chapter 9. Software Testing

CS Programming Languages: Python

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Consistency. CS 475, Spring 2018 Concurrent & Distributed Systems

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Basic Programming Language Syntax

Chapter 5 Errors. Bjarne Stroustrup

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

Unit Testing In Python

6.001 Notes: Section 17.5

Defensive Programming

CSC148: Week 1

Strings and Testing string methods, formatting testing approaches CS GMU

COMP-202 Unit 0: Course Details

CS18000: Programming I

Engineering Robust Server Software

Unit Testing as Hypothesis Testing

MITOCW watch?v=w_-sx4vr53m

Write for your audience

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Transcription:

Announcements CSE 399-004: Python Programming Lecture 07: Packages, Command-line arguments, and Unit testing February 26, 2007 http://www.seas.upenn.edu/~cse39904/ No homework this week There may be one or two more homeworks later If so, they will be short Projects Should have received feedback on your proposal Might be a good idea to start on them now Some of you were asked to send me email I'll ask later in the term if I should change the due date No office hours during Spring Break I'll be in town though Send email if you want to meet 2 Today Packages Command-line arguments Unit testing: The unittest module Packages (Tutorial, Section 6.4) 3 Packages: Overview A package collects together many modules and organizes them into some hierarchy Packages: Concretely A package in Python is simply any directory that contains a file named The organization of files (modules) on disk determines the structure of the hierarchy Useful since putting many modules in the same directory can be cumbersome Aside: Packages in Python are similar to packages in Java 5 A sample package hierarchy. 6

Packages: Concretely A package in Python is simply any directory that contains a file named Packages: Concretely A package in Python is simply any directory that contains a file named Unlike in Java, nothing special is needed within these files to make them part of a package. A subpackage of the Sound package. 6 6 Importing from packages import Sound.Effects.echo from Sound.Effects import echo Importing from packages import Sound.Effects.echo from Sound.Effects import echo Only the "dot-notation" is new here. 7 7 Imports within packages Imports across package boundaries need to use the full package names Imports within packages Imports across package boundaries need to use the full package names import surround import Sound.Formats.wavread 8 8

Imports within packages Imports across package boundaries need to use the full package names import surround from..formats import wavread Python also supports relative imports, but I think it's clearer to write out paths in full. 9 Command-line arguments Why command-line arguments? Even in this age of graphical user interfaces (GUIs), it is still common to invoke programs using a command-line It's not nice to pester the user with lots of questions as to what the program should do For example, the Unix cp program has something like 10 20 different options to control its behavior What are command-line arguments? % cse39904submit -a hw5 html/index.html Command-line arguments are an easy way for a user to signal to a program what they want it to do 11 12 What are command-line arguments? What are command-line arguments? program name % cse39904submit -a hw5 html/index.html % cse39904submit -a hw5 html/index.html 12 Three arguments: (1) -a (2) hw5 (3) html/index.html 12

What are command-line arguments? % cse39904submit -a hw5 html/index.html sys.argv The sys module has a variable argv which is a list of the command-line arguments given to the current program Each argument is always a string The first argument is always the name of the program (e.g., file) being run Arguments of the form "-foo" are sometimes referred to as "options" or "flags". 13 14 sys.argv: Example foo.py #!/usr/bin/env python import sys print sys.argv sys.argv: Example The "program name" is useful only your program depends on "how it was called" or if you need to generate something like a usage message. %./foo.py arg1 200 arg3 ['./foo.py', 'arg1', '200', 'arg3'] % python foo.py a b 30 ['foo.py', 'a', 'b', '30'] % python foo.py ['foo.py'] %./foo.py arg1 200 arg3 ['./foo.py', 'arg1', '200', 'arg3'] % python foo.py a b 30 ['foo.py', 'a', 'b', '30'] % python foo.py ['foo.py'] 15 15 The optparse module Parsing "interesting" command-line arguments by hand is painful and error prone except The optparse module provides an easy way for you to specify flags/options and then parse them 16 17

Automatically knows that -h means to print out a help message. Does not know about any other flags. Tells the parser to look for the -a flag. 17 17 Actually parse sys.argv. Information about flags passed in is stored in options, and args will be a list of all other command-line arguments. 18 % cse39904submit! options.assn == None! args == [ ] 19 % cse39904submit -a hw1! options.assn == 'hw1'! args == [ ] 20 % cse39904submit -a hw1 file1 file2! options.assn == 'hw1'! args == [ 'file1', 'file2'] 21

References There are many things you can do with OptParsers http://docs.python.org/lib/module-optparse.html % cse39904submit file1 -a hw1 file2! options.assn == 'hw1'! args == [ 'file1', 'file2'] 22 23 What is unit testing? Unit testing: Overview Well-written programs can be broken up into "units" Individual functions and methods Classes Modules Packages Unit testing aims to test the functionality of all the "units" in your program 25 Why unit testing? Testing whole programs is difficult Too many code paths to verify Too many behaviors to check for Isolating bugs is tricky It makes more sense to test things by checking the smaller units first, then the larger ones This naturally isolates bugs and problematic behavior Test cases themselves can serve as documentation, e.g., examples of (in)correct behavior Regression testing Programs evolve over time New features are added Bugs are uncovered, then fixed New code can be incorrect or break existing code Makes similar mistakes as previously written code Breaks long held assumptions Regression testing involves the periodic running of test cases to monitor code for the reemergence of bugs Easy to do with a suite of unit tests 26 27

Problems with unit testing Many pieces of code cannot run in complete isolation They may rely on external resources They may rely on other code behaving correctly Two techniques to get around this: Test harnesses: Create a suitable environment before running certain test cases Mock objects: When testing, say, one class, create dummy instances of other necessary classes Goal here: To test one piece of code without relying on other pieces to behave correctly Caveats about testing in general You need to know what your code is supposed to do before you can write test cases for it But, you can still write the test cases for a given piece of code before you write the code itself Beware of bugs in the test cases themselves The test cases are, after all, simply more code Be very careful when using your program's output to write test cases you don't want to use buggy output! 28 29 References http://en.wikipedia.org/wiki/unit_testing http://en.wikipedia.org/wiki/regression_testing Unit testing: The unittest module 30 The unittest module: Overview The unittest module makes it relatively easy to write a suite of unit tests for your programs It is modeled after JUnit, a Java unit testing framework Aside: There are many ways to use unittest I'll describe one way of organizing test cases The module provides other ways Goal for today: Basic introduction to unittest import random import unittest def setup(self): self.seq = range(10) <methods for test cases elided here> if name == ' main ': unittest.main() 32 33

import random import unittest import random import unittest No need to define a constructor. def setup(self): self.seq = range(10) <methods for test cases elided here> if name == ' main ': unittest.main() def setup(self): self.seq = range(10) <methods for test cases elided here> if name == ' main ': unittest.main() The class representing a test case. Can be used to defined multiple cases at once. 33 34 import random import unittest def setup(self): self.seq = range(10) <methods for test cases elided here> if name == ' main ': unittest.main() import random import unittest def setup(self): self.seq = range(10) <methods for test cases elided here> if name == ' main ': unittest.main() The setup() method is called before each test case in this class is run. A similar method teardown() is called Runs the test cases defined in this module. after each test case is run, no matter the result. 34 35 36 Any method whose name starts with "test" defines a test case. These methods should take no arguments. 36

setup() is called before each method is called, The TestCase class provides several methods which test cases should use to test whether certain conditions hold. so these self.seq's will all be equal to range(10). 37 38 TestCase.assertEqual() : Used to test if two values are equal. 39 TestCase.assert_() : Used to test if something is true. 40 Failures versus errors unittest distinguishes "failures" from "errors" Failure: The test case was expecting one thing to happen, and instead something else happen These happen when some assert*() method fails to detect the expected condition or event TestCase.assertRaises() : Test if some exception is thrown. 41 These correspond to incorrect answers Error: An unexpected exception is thrown Corresponds to code doing something incorrect 42

More unittest terminology Test fixture: The framework needed to run a single test case; managed by the TestCase class Test case: Smallest unit of testing; managed by TestCase Test suite: A collection of test cases and test suites Test suites can be built up from other suites Test runner: Component which actually runs test cases and test suites References http://docs.python.org/lib/module-unittest.html In particular, see the TestCase class for all the various assert* and fail* methods that test cases can use 43 44 Final remarks Next class is Monday, March 12, 2007 Do try to enjoy Spring Break List of (potential) future topics: Graphical user interfaces (GUIs) Regular expressions Networking Functional programming [next time?] [near end?] Some of you may have to figure out the above topics before I get a chance to say anything about them 45