CS 1133, LAB 2: FUNCTIONS AND TESTING http://www.cs.cornell.edu/courses/cs1133/2015fa/labs/lab02.pdf

Similar documents
We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Python Loops and String Manipulation

r-one Python Setup 1 Setup 2 Using IDLE 3 Using IDLE with your Robot ENGI 128: Introduction to Engineering Systems Fall, 2014

Outline Basic concepts of Python language

Exercise 1: Python Language Basics

Beginning to Program Python

Using Dedicated Servers from the game

enter the administrator user name and password for that domain.

SQL Server Instance-Level Benchmarks with DVDStore

Using Karel with Eclipse

Creating Your Own TinyWeb Database. Ball State University - CS116 - Ashley Swartz

CS 106 Introduction to Computer Science I

Microsoft Windows PowerShell v2 For Administrators

Week 2 Practical Objects and Turtles

Introduction to Python

Installing and Running the Google App Engine On Windows

1001ICT Introduction To Programming Lecture Notes

IBI Group FTP: Usage Instructions

PHP Debugging. Draft: March 19, Christopher Vickery

Getting Started with Command Prompts

Exercise 4 Learning Python language fundamentals

Setting Up a Windows Virtual Machine for SANS FOR526

Handle Tool. User Manual

Appendix K Introduction to Microsoft Visual C++ 6.0

Programming Languages CIS 443

Today s Topics... Intro to Computer Science (cont.) Python exercise. Reading Assignment. Ch 1: 1.5 (through 1.5.2) Lecture Notes CPSC 121 (Fall 2011)

TCP/IP Networking, Part 2: Web-Based Control

Cloud Backup Express

ProxiBlue Dynamic Category Products

Importing and Exporting With SPSS for Windows 17 TUT 117

Introduction to the course, Eclipse and Python

Server & Workstation Installation of Client Profiles for Windows

Introduction to CloudScript

CS 100 Introduction to Computer Science Lab 1 - Playing with Python Due September 21/23, 11:55 PM

SQL Injection Attack Lab

Computer Programming In QBasic

Visual Basic Programming. An Introduction

2 SQL in iseries Navigator

CONNECTING TO DEPARTMENT OF COMPUTER SCIENCE SERVERS BOTH FROM ON AND OFF CAMPUS USING TUNNELING, PuTTY, AND VNC Client Utilities

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

COMSC 100 Programming Exercises, For SP15

Testing Methodology Assignment 1 Unit testing using JUnit

Installing C++ compiler for CSc212 Data Structures

Using the Radmind Command Line Tools to. Maintain Multiple Mac OS X Machines

LAB 2 SPARK / D-STREAM PROGRAMMING SCIENTIFIC APPLICATIONS FOR IOT WORKSHOP

Bitrix Site Manager ASP.NET. Installation Guide

Installing TeamCall Server on Mac OS X

Integrating Secure FTP into Data Services

CLC Server Command Line Tools USER MANUAL

Google Calendar 3 Version 0.8 and 0.9 Installation and User Guide. Preliminary Setup

BACKSCATTER PROTECTION AGENT Version 1.1 documentation

Welcome to Introduction to programming in Python

Crystal Reports Integration Plugin for JIRA

SnapLogic Tutorials Document Release: October 2013 SnapLogic, Inc. 2 West 5th Ave, Fourth Floor San Mateo, California U.S.A.

Cloud Server powered by Mac OS X. Getting Started Guide. Cloud Server. powered by Mac OS X. AKJZNAzsqknsxxkjnsjx Getting Started Guide Page 1

Prescribed Specialised Services 2015/16 Shadow Monitoring Tool

eggon SDK for ios 7 Integration Instructions

by Jonathan Kohl and Paul Rogers 40 BETTER SOFTWARE APRIL

Website Development Komodo Editor and HTML Intro

Activation of your SeKA account

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Waspmote IDE. User Guide

The Hitchhiker s Guide to Github: SAS Programming Goes Social Jiangtang Hu d-wise Technologies, Inc., Morrisville, NC

Deploying WinLIMS Web v7.2 to a Windows 2008 x64 box. Table of Contents. Deploying WinLIMS Web v7.2 to a Windows 2008 x64 box... 1

Personal Portfolios on Blackboard

Introduction to Python

Server & Workstation Installation of Client Profiles for Windows (WAN Edition)

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

SchoolBooking SSO Integration Guide

Python Lists and Loops

Code::Blocks Student Manual

FTP Updates. Hiden Analytical Limited 420 Europa Boulevard Warrington WA5 7UN England. Tel: +44 (0) Fax: +44 (0)

M100 System File Manager Help

NewsletterAdmin 2.4 Setup Manual

ecopy ShareScan 5.0 SQL installs guide

Wireless Security Camera with the Arduino Yun

Informatics for Integrating Biology and the Bedside. i2b2 User Guide. Import Data View. Document Version: I2b2 Software Release: 1.

Introduction to: Computers & Programming: Input and Output (IO)

Using Flwrap to Send Error Checked and Compressed Files

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

Computer Science for San Francisco Youth

Uploads from client PC's to mercury are not enabled for security reasons.

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

EBSCO MEDIA FILE TRANSFER SOFTWARE INSTALLATION INSTRUCTIONS

A Crash Course in OS X D. Riley and M. Allen

Online Web Learning University of Massachusetts at Amherst

Linux Overview. Local facilities. Linux commands. The vi (gvim) editor

Excel Reporting with 1010data

Code Estimation Tools Directions for a Services Engagement

National Fire Incident Reporting System (NFIRS 5.0) Configuration Tool User's Guide

How to Create a Spreadsheet With Updating Stock Prices Version 3, February 2015

Wakefield Council Secure and file transfer User guide for customers, partners and agencies

Access Control and Audit Trail Software

Copyright Notice. Mobile Testing Enterprise 7.3. September Copyright Keynote LLC. All rights reserved.

System update procedure for Kurio 7 (For build number above 110)

Transcription:

CS 1133, LAB 2: FUNCTIONS AND TESTING http://www.cs.cornell.edu/courses/cs1133/2015fa/labs/lab02.pdf First Name: Last Name: NetID: The purpose of this lab is to help you to better understand functions: both how to write them and how to test them. These concepts are the primary focus of Assignment 1, and therefore it is important that you complete this lab before starting on the assignment. If you have never programmed before, you will find this lab significantly longer than the previous lab. In that case, it is very likely that you will not finish the lab during class time. If you are having any difficulty at all with this lab, we strongly encourage you to go to consulting hours. Lab Materials. We have created several Python files for this lab. You can download all of the from the Labs section of the course web page. http://www.cs.cornell.edu/courses/cs1133/2015fa/labs For today s lab you will notice two files. lab02.py (a module with your first function) test_lab02.py (a testing script) You should create a new directory on your hard drive and download the files into that directory. You can also get all of the files bundled as a single ZIP file called lab02.zip. On both Windows and OS X, you can double click on the ZIP file to expand it to a folder. However, Windows has a weird way of dealing with ZIP files, so Windows users will need to drag the folder contents to another folder before using them. Getting Credit for the Lab. This lab is unlike the previous two in that it will involve a combination of both code and answering questions on this paper. In particular, you are expected to complete both the module lab02.py and the testing script test_lab02.py. When you are done, show all of these (the handout, the test script, and the module) to your instructor. You instructor will then swipe your ID card to record your success. You do not need to submit the paper with your answers, and you do not need to submit the computer files anywhere. As with the previous lab, if you do not finish during the lab, you have until the beginning of lab next week to finish it. You should always do your best to finish during lab hours. Remember that labs are graded on effort, not correctness. 1. Extracting Strings As we saw in lecture, even though single and double quotes are used to delimit string literals, they also are characters that can occur in strings, just like any other character. The simplest way to get a quote character into a string is to use the other kind of quotes to delimit the string: s = "Don't panic!" Course authors: D. Gries, L. Lee, S. Marschner, W. White 1

When both kinds of quotes need to appear, we need to use the escape sequences we saw in class. An escape sequence consists of a backslash followed by a quote: s = 'The phrase, "Don\'t panic!" is frequently uttered by consultants.' You could also write a string like this using double quotes as the delimiters. Rewrite the assignment statement for s above using a double-quoted string literal: For the first part of this lab, we want you to write Python code to extract the substring inside the double quotes (which is "Don't panic!"). But we want to do it in a way that is independent of s. That means, even if you change the contents of s, your answer should still work, provided that s still has a pair of double-quote characters somewhere. In the box below, write a sequence of one or more assignment statements, ending with an assignment to a variable called inner (the other variables can be named whatever you want). The assignment statements should use string slicing to remove the unwanted parts of s. When you are done, the contents of inner should be the substring inside the double-quote characters (but not including the double quotes themselves). To test that your statements are correct, do the following. First, type in s = 'The phrase, "Don\'t panic!" is frequently uttered by consultants.' Then type in your statements from the box above. Finally, print the value of inner. You should see Don't panic, without the quotes (printing always removes the quotes from a string value). Now, try the process again with s = 'The question "Can you help me?" is often asked in consulting hours.' Type in the assignment statement above, then type in your statements from the box, and finally print the value of inner. You should see Can you help me?, without the quotes. If you had to modify your answer in the box for the second q1, you have done it incorrectly. 2. Writing Your First Function In the string extraction problem, we had you verify that your assignment statements worked on different values of s as well. This got a bit annoying, as you had to type in the assignment statements each time, even though they did not change (only s changed). This is the motivation for writing a function. A function allows us to group all of those assignments together and replace them with a single statement (the function call). 2

Before you write a function, you need to a module to store the function. We have already created a module file for you the file lab02.py that you have downloaded for this lab. At the end of this file, you will see the body of a function called first_inside_quotes(). It looks like this: def first_inside_quotes(s): # Your assignment statements from lab 2 here return inner There is also another function in this file. Ignore the other function for now. You should only work on the function first_inside_quotes() The function first_inside_quotes() takes a string and returns the substring inside the first pair of double-quote characters. To implement this function, replace the comment with your assignments from the previous lab exercise. It is now time to try out your function. Navigate the command line to the folder containing the file lab02.py (ask a consultant/instructor for help if you cannot figure out how to do this). Start the Python interactive shell and import the module lab02. Remember to omit the.py suffix when you use the import command. Call the function lab02.first_inside_quotes('the instructions say "Dry-clean only".') (Remember the module prefix) What happens? To check off this portion of the lab you should demo your function to the course staff with a few different arguments. Whenever you call the function, you should make sure that each argument always has a pair of double-quote characters in it, as this is required by the precondition. 3. Test the Function has_a_vowel(s) There are now two functions in the module lab02. We need to test them out and make sure that they are working properly. This is the purpose of a test script. As we showed in class, a script is a special module that we do not import. We instruct Python to run the module and then quit. The test script is named test_lab02.py. Quit python and type the following: python test_lab02.py This will not give you the Python interactive shell with the symbol >>>. Instead, it will run the python statements in test_lab02.py and then immediately quit Python. What do you see? 3

3.1. Working with a Test Procedure. A test procedure is a function that contains all of the test cases for testing out another function. The convention in this class is that the test procedure should have the same name as the function it is testing except that it has test_ at the beginning. For example, you should test has_a_vowel(s) with the test procedure called test_has_a_vowel(). The test procedure test_has_a_vowel() is already placed in the file test_lab02.py for you. In fact, you will see it twice. Once is the procedure definition. That is where you will put the test cases. The other is the procedure call. The tests will not activate unless you call it. To see what we mean, comment out the function call on line 17 if test_lab02.py. Run the script again. What do you see this time? This illustrates the reason for the print statement in test_has_a_vowel(). It is so that you have a way to determine whether the test is running properly. Without it, a properly written script will not display anything at all, and we have seen that students find this confusing. 3.2. Implement the First Test Case. In the body of function test_has_a_vowel(), you are now going to add several two statements below the print statement that do the following: Call the function has_a_vowel('aeiou'), and put the answer in a variable called result. Call the procedure cornelltest.assert_equals(true,result). These two lines correspond to the input and output portions of a test case. In the first line you provide the input to the function has_a_vowel, which is the function being tested. In the second line you compare the result to the expected answer. Run the unit test script now. If you have done everything correctly, the script should reach the message 'Module lab02 is working correctly.' If not, then you have actually made an error in the testing program. This can be frustrating, but it happens sometimes. One of the important challenges with debugging is understanding whether the error is in the code or the test. 3.3. Add More Test Cases for a Complete Test. Just because one test case worked does not mean that the function is correct. The function has_a_vowel can be true in more than one way. For example, it is true when s has just one vowel, like 'a'. Alternatively, s could be 'o' or 'e'. We also need to test strings with no vowels. It is possible that the bug in has_a_vowel causes it returns True all the time. If it does not return False when there are no vowels, it is not correct. There are many strings that we could test infinitely many. The goal is to pick test cases that are representative. Every possible input should be similar to, but not exactly the same as, one of the representative tests. For example, if we test one string with no vowels, we are fairly confident that it works for all strings with no vowels. But testing 'aeiou' is not enough to test all of the possible vowel combinations. 4

How many representative test cases do you think that you need in order to make sure that the function is correct? Perhaps 6 or 7 or 8? Write down a list of test cases that you think will suffice to assure that the function is correct: 3.4. Test. Run the test script. If an error message appears, study the message and where the error occurred to determine what is wrong. While you will be given a line number, that is where the error was detected, not where it occured. The error is in has_a_vowel. 3.5. Fix and Repeat. You now have permission to fix the code in lab02.py. Rerun the unit test. Repeat this process (fix, then run) until there are no more error messages. 4. Test the Function first_inside_quotes(s) The lab has now come full circle. You started the lab creating your first function. You have also learned how to test a function. It is now time to create a unit test for your function first_inside_quotes(s). First, you should think of several test cases for first_inside_quotes(s). Come up with at least 4 different test cases, and explain why they are different: Remember that a test case is both an input and and output. We need both Now that you have your test cases, the process is very much the same as what you did to test has_a_vowel() in the previous part of the lab 4.1. Add a Test Procedure. In module test_lab02.py, you should make up another test procedure, test_first_inside_quotes(). Add the definition of this test procedure to a file. For right now, this procedure should only have one line in it, the print statement print 'Testing function first_inside_quotes' 5

4.2. Call the Test Procedure. Remember that a test procedure does not do anything unless you call it. Add a call to test_first_inside_quotes just below the call to test_has_a_vowel. Once you have done this, run the script test_lab02.py. What do you see? 4.3. Implement the First Test Case. Take your first test case from the box above. Call first_inside_quotes on the input and assign the value to result. Use assert_equals to verify that result is the output you expected. 4.4. Test and Fix Errors. Run the script before you add any more of your test cases. If you get an error, look at your code for first_inside_quotes(s) and try to figure out what it is. Keep fixing and testing until there are no errors. 4.5. Repeat with a New Test Case. Once you are statisfied that a particular test case is working correctly, start over with the next test case that your wrote in the box on the previous page. Continue until there are no test cases left. 6