Unit testing Unit testing with Python and Zope Søren Roug February 2008.

Slides:



Advertisements
Similar presentations
The Little man computer
Advertisements

Main task -write me a program
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.
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
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,
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
The Little man computer
Development Environment
Whatcha doin'? Aims: To start using Python. To understand loops.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Lecture 2 Python Basics.
Introduction to Python
Introduction to Python
Containers and Lists CIS 40 – Introduction to Programming in Python
Programming For Big Data
Different Types of Testing
(optional - but then again, all of these are optional)‏
Testing UW CSE 160 Winter 2017.
Fundamentals of Programming I Design with Functions
Software engineering – 1
UML Activity Diagram Documents the Flow the of Program
Functions CIS 40 – Introduction to Programming in Python
Functions.
Little work is accurate
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Testing UW CSE 160 Spring 2018.
TRANSLATORS AND IDEs Key Revision Points.
Exception Handling Chapter 9.
Python I/O.
Topics Introduction to File Input and Output
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Testing UW CSE 160 Winter 2016.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
UML Activity Diagram Documents the Flow the of Program
Test-driven development (TDD)
Passing Parameters by value
CS 1111 Introduction to Programming Fall 2018
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
CSE 303 Concepts and Tools for Software Development
Text Analyzer BIS1523 – Lecture 14.
Loop Strategies Repetition Playbook.
CISC101 Reminders All assignments are now posted.
For loops Taken from notes by Dr. Neil Moore
A First Program.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Loops and Simple Functions
Bryan Burlingame 17 April 2019
Introduction to Computer Science
Running a Java Program using Blue Jay.
Chapter 11: Integration- and System Testing
Python Inputs Mr. Husch.
CSE 231 Lab 5.
Topics Introduction to File Input and Output
Tools to make the process safer : secured filing
CSC 221: Introduction to Programming Fall 2018
Functions, Procedures, and Abstraction
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Lecture 20 – Practice Exercises 4
Unit Testing.
Presentation transcript:

Unit testing Unit testing with Python and Zope Søren Roug February 2008

Purpose of Unit testing To validate that individual units of source code are working properly It is executable documentation of the source By showing what output is expected from given input Continual quality assurance of the code Works like a burglar alarm Doesn't guarantee bugfree code But catches most unsophisticated bugs

Python unit testing exercise Create function called divide that divides arg1 with arg2 division.py def divide(arg1, arg2): return arg1 / arg2

Unit test to verify the code import unittest from division import divide class TestDivision(unittest.TestCase): def test_int(self): self.assertEqual( , divide(17, 7) ) def test_float(self): self.assertEqual( , divide(17.0, 7) ) if __name__ == '__main__': unittest.main()

Safer way import unittest from division import divide class TestDivision(unittest.TestCase): def test_int(self): assert abs( divide(17, 7)) < def test_float(self): assert abs( divide(17.0, 7)) < if __name__ == '__main__': unittest.main()

Exercise Fix division.py to work as the unit test expects it Next, add a unit test that provides the arguments as text strings divide(“17”,”7”) Then: Fix division.py to work as the unit test expects it This cycle is called Test-Driven Development (TDD)

But TDD is very difficult to get used to because it is so counter- intuitive

Why Unit tests? It makes you a better programmer The design is cleaner It makes it safer to modify code Unit tests gives you peace of mind.

Unit testing in Zope Since version 2.8, Zope has had ZopeTestCase built in That's what we'll be using

Setting it up Check out the CountCoup product to a Zope site Create a 'tests' directory in the product Copy framework.py, runalltests.py, runtest and testTemplate.py to the tests directory Verify:./runtest testTemplate.py./runtest runalltests.py

First real Unit test A CountCoup object has already been set up in afterSetup() Now we call the index_html method RESPONSE = self.app.REQUEST.RESPONSE self.app.mycounter.index_html('testfile.pp t', self.app.REQUEST, RESPONSE) self.assertEquals(RESPONSE.getHeader("loca tion"), 'testfile.ppt')

Unit test interlude Print the output from listcounts() to see what it contains print self.app.mycounter.listcounts()

Unit test clearcounts() Call the clearcounts() and then the output from listcounts() is expected to be the empty list Potentially ensure something is counted by calling index_html a couple of times before you clear the counter

Unit test listcounts() Call clearcounts() to ensure a known state Call index_html with two different paths Several times for each path Check that listcounts() shows the expected values

Test with addresssum=0 Copy the entire class to another class name in the same file Add the class to the test_suite in the bottom In afterSetUp() simulate what would happen if the user had created the object with the addresssum unchecked …manage_addCountCoup('mycounter','Title') Run the tests again Fix the code – fix the tests

Revisiting the tests for addresssum=1 Did we test listcounts() correctly when we created the object with addresssum=1? index_html looks at REQUEST['REMOTE_ADDR'], so we need to call index_html with that set to various things self.app.REQUEST.set('REMOTE_ADDR', ' ') Then check that listcounts() works

End of exercise