CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.

Slides:



Advertisements
Similar presentations
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Advertisements

Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
CIT 590 Unit testing.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
How to Debug VB .NET Code.
Fixing Broken Programs. How do you figure out what’s wrong? Look carefully at the error message. Locate the error – Most messages have line numbers –
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Exceptions COMPSCI 105 S Principles of Computer Science.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Introduction to Python
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
CIT 590 Intro to Programming Last lecture on Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CS0004: Introduction to Programming Subprocedures and Modular Design.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Input, Output, and Processing
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Pointers OVERVIEW.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Debugging Strategies from Software Carpentry. Agan's Rules Many people make debugging harder than it needs to be by: Using inadequate tools Not going.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
CS320n –Visual Programming More LabVIEW Foundations.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Error Handling Tonga Institute of Higher Education.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSC 108H: Introduction to Computer Programming
Test-driven development
CIT 590 Unit testing.
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Cracking the Coding Interview
Some Basics for Problem Analysis and Solutions
Designing and Debugging Batch and Interactive COBOL Programs
Topics Introduction to File Input and Output
Introduction to C++ Programming
Test-driven development (TDD)
Coding Concepts (Basics)
Coding Concepts (Data- Types)
Test Driven Development
An Introduction to Debugging
Test Driven Development
Topics Introduction to File Input and Output
Presentation transcript:

CIT 590 Unit testing

Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python

What is unit testing Test individual units of a program. Simplest way to think of it – test every function This does not guarantee that the different functions work together harmoniously. That is the job of integration testing

Why unit test test driven development (TDD) can save you lots of time Tracking a bug down is often the most time consuming part of development When something breaks you want to know exactly which sub sub component has broken down. In an ideal world, if you implement incorrect code, one of your tests breaks

Common excuses for not doing TDD Resistance A lot of this is practice You will need to start doing this from the next assignment.

Test, Code, Refactor loop Also described as Make it fail (tests) Make it work (code) Make it better (refactor) Pure test driven development says that you write your tests first! This can be annoying unless you commit to your method signatures up front. Given method signatures write some tests

Refactor? def isperfect(number): if number==sum(i for i in range(1,number) if number%i==0): return True def isabundant(number): if number<sum(i for i in range(1,number) if number%i==0): return True If code is repeating itself, make a function out of it.

def decFloat2Bin(string): """ Converts a Float to a Binary Number""" ### EXTRACT THE INT AND FRACTION PART integer = extractIntegerPart(string) frac = extractFractionalPart(string) ### convert integer part ##### inputCount = int(integer) decimalCount = "" while (inputCount > 0): binary = (int(inputCount) % 2) decimalCount += str(binary) inputCount = int(inputCount) /2 intOutput =str(decimalCount) ## convert fractional part to binary ### decCount = "" dec = "0."+frac ## only 23 bits allowed while (len(str(decCount))<23): #do repeated multiplication by 2 #### ADD THE TWO PIECES TOGETHER output = intOutput[::-1] + "." + decCount return output Break up functions into smaller functions. Even if the specs say 1 function – you are always allowed to do helper functions

Code reviews Code review is the process of making pieces of source code available for other developers to review, with the intention of catching bugs and design errors before the code becomes part of the product. If you are part of a software company that is not doing code reviews …

Code reviews Crucible. One of the many tools used for online code reviews

How many tests? Test out a typical example – some call this the smoke test. You really expect this one to pass. Test out examples at the extreme ends of the input spectrum – empty list, large numbers etc But there will/might still be bugs If you hit a bug – begin the fix by adding a unit test case for it Code until unit test passes Now run the whole program and ensure things pass

Hands on example of TDD Quadratic equation solver mathOps.py and testMath.py (in case I mess up )

Rules for writing tests The name of a test method must start with the letters 'test', otherwise it will be ignored. This is so that you can write "helper" methods you can call from your tests, but are not directly called by the test framework. Every test method must have exactly one parameter, self. You must put 'self.' in front of every built-in assertion method you call. The tests must be independent of one another, because they may be run in any order. Do not assume they will be executed in the order they occur in the program

What do I test for within a test self.assertEqual(expectedResult, actualResult, [message]) Test that the two values are exactly equal. self.assertNotEqual(firstValue secondValue,[message]) Test that the two values are different, and fail if they are equal. self.assertAlmostEqual(expected, actual, [places,[message]]) Test that the two numeric values are equal, after rounding to places decimal places (default is 7). self.assertAlmostNotEqual(expected, actual,[places,[message]]) Test that the two numeric values are equal, after rounding to places decimal places (default is 7). self.assertTrue(booleanCondition,[message]) Test that the booleanCondition is true. self.assertFalse(booleanCondition,[message]) Test that the booleanCondition is false. self.assertRaises(exception, functionName, parameter,...,) Test that the function functionName, when called with the given (zero or more) parameters, raises the given exception. Note that, for this assertion method, there is no option for a message parameter..

TDD as an algorithm pick a method that doesn't depend on other, untested methods while the method isn't complete: write a test for the desired feature run all tests and make sure the new one fails while any test fails: add/fix just enough code to try to pass the tests refactor the code to make it cleaner

Caution Who tests the tests?? One of the more annoying things if you get sloppy about writing your tests is that a failed test could indicate an error in the test file more than an error in the actual code BE VERY CAREFUL WHEN YOU WRITE YOUR TESTS Sometimes a code refactor will result in the tests having to be refactored themselves DO NOT HESITATE TO DO SO