Fundamentals%20of%20Python:%20From%20First%20Programs%20Through%20Data%20Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Fundamentals%20of%20Python:%20From%20First%20Programs%20Through%20Data%20Structures

Description:

Fundamentals of Python: From First Programs Through Data Structures Chapter 12 Tools for Design, Documentation, and Testing Fundamentals of Python: From First ... – PowerPoint PPT presentation

Number of Views:325
Avg rating:3.0/5.0
Slides: 50
Provided by: unie1
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Fundamentals%20of%20Python:%20From%20First%20Programs%20Through%20Data%20Structures


1
Fundamentals of PythonFrom First Programs
Through Data Structures
  • Chapter 12
  • Tools for Design, Documentation,
  • and Testing

2
Objectives
  • After completing this chapter, you will be able
    to
  • Write scenarios and use cases for the analysis
    phase of a simple software system
  • Design a simple software system in which the
    classes have the relationships of aggregation,
    composition, and inheritance
  • Use UML diagrams to depict use cases,
    relationships among classes, and collaborations
    among objects in a simple software system

3
Objectives (continued)
  • Write preconditions and postconditions for
    methods
  • Raise exceptions in methods when preconditions
    are violated
  • Generate Web-based documentation of classes
  • Write simple unit tests for classes

4
Introduction
  • A program must be well designed, thoroughly
    documented, and carefully tested
  • A good design is essential to solving any complex
    problem
  • Well-designed and well-written code is, to a
    certain extent, self-documenting
  • No matter how carefully you design and document a
    program, you need to test it with equal care
  • Computer Assisted Software Engineering (CASE)
    tools include debuggers, version trackers,
    profilers, and test beds

5
Software Design with UML
  • Programmers use various graphical notations to
    represent analysis and design decisions
  • UML Unified Modeling Language
  • Dominant graphical scheme currently used in
    object-oriented software development
  • An excellent and free UML authoring tool is
    available at http//argouml.tigris.org/

6
UML and Modeling
  • UML diagrams come in handy during the initial
    phases of software development
  • Help programmer visualize, or model, the
    interactions of human users with proposed system,
    relationships among its classes, and interactions
    among its objects
  • UML includes diagrams for almost any occasion
  • For example
  • Class diagrams
  • Use case diagrams
  • Collaboration diagrams

7
Use Case Diagrams
  • A use case is a narrative, in English, of the
    steps a user takes to perform a single action

8
Use Case Diagrams (continued)
  • Use cases should include narratives of
    exceptional conditions and corresponding
    responses

9
Use Case Diagrams (continued)
  • Use case diagrams in UML translate narratives of
    use cases into a pictorial representation

10
Use Case Diagrams (continued)
  • Use cases and use case diagrams enable the
    programmer to accomplish several things
  • Result in a precise statement of the functional
    requirements of the software system, in other
    words, the what it does, from the users
    perspective
  • Serve as the basis for writing user
    documentation, in the form of manuals and online
    help systems for the software
  • Allow the programmer to determine which classes
    need to be chosen or developed to realize the
    requirements of the system

11
Class Diagrams
  • During the discovery of classes for a system,
    their relationships can be modeled with class
    diagrams
  • The simplest such diagrams show a relationship of
    association between two classes

12
Class Diagrams (continued)
  • Associations do not tell us how classes are
    related
  • Programmer can also specify a role that a class
    plays in relation to another

13
Class Diagrams (continued)
14
Class Diagrams (continued)
15
Class Diagrams (continued)
  • The arrows representing directed associations can
    be dropped in favor of using edges with markers
    that indicate aggregation or composition

16
Collaboration Diagrams
  • The attributes and operations of a given class
    are often designed to fit into a sequence of
    operations that perform a particular task
  • The actors in these scenarios are software
    objects or instances of the systems classes
  • A system function or task is broken down into a
    set of method calls on objects, and coordinating
    these becomes the focus of design
  • The interactions among objects can be documented
    in a collaboration diagram

17
Collaboration Diagrams (continued)
18
From Collaboration Diagram to Code
19
From Collaboration Diagram to Code (continued)
20
Inheritance
  • Subclassing helps eliminate redundant code in
    similar classes
  • Inheritance relationship between classes is shown
    with an outlined arrowhead in a class diagram

21
Documentation
  • In this section, we examine ways to make
    documentation more systematic and informative
  • You will also learn about a tool, pydoc, which
    you can use to generate documentation that can be
    viewed with a Web browser

22
Writing APIs
  • API Application Programming Interface
  • Interfaces for a programming languages built-in
    resources
  • Most interfaces also include documentation
  • Browse the API for the current version of Python
    at Pythons Web site (http//docs.python.org/)
  • Access information about an API from within a
    Python shell by using the help function

23
Writing APIs (continued)
  • What information should an API include?
  • Module its filename, a brief statement of its
    purpose, a summary of the resources that it
    includes
  • Class name of class and superclass, a brief
    statement of its purpose
  • Might also include a short list of the operations
    available to the client (the classs interface)
  • Method its header, a brief statement of what it
    does (not how it does it), the types of arguments
    it expects, and the type of value it returns
  • Function same information included for a method

24
Revisiting the Student Class
25
Revisiting the Student Class (continued)
26
Revisiting the Student Class (continued)
  • Most of the methods in the Student class are
    accessors
  • Accessors are generally the easiest to document,
    especially those that expect no arguments
  • A simple statement about the type and meaning of
    the value returned by the method usually suffices
  • setScore is the only mutator method
  • getScore and setScore call for further attention
  • What happens if the reader misses the fact that
    the positions of the scores are counted from 1,
    not 0?

27
Preconditions and Postconditions
  • Precondition Statement of what must be true
    before a method is invoked if it is to run
    correctly
  • Usually describe the state of any parameters and
    instance variables that a method is about to
    access
  • Example

28
Preconditions and Postconditions (continued)
  • Postcondition Statement of what will be true
    after the method has finished execution
  • Describe the state of parameters and instance
    variables that the method has changed
  • Example

29
Enforcing Preconditions with Exceptions
  • Preconditions in documentation do not prevent
    users from violating them
  • Avoid problems by guaranteeing that an exception
    will be raised if a precondition is violated
  • Syntax for raising an exception
  • Must document that exceptions may be raised

30
Enforcing Preconditions with Exceptions
(continued)
  • A programmer should try to raise a type of
    exception that is specifically related to the
    kind of condition being enforced

31
Web-Based Documentation with pydoc
  • Programmer-authored documentation is always
    available in the Python shell

32
Web-Based Documentation with pydoc (continued)
  • Documentation can be viewed in a Web browser
  • pydoc tool allows programmer to generate
    documentation from a module in form of a Web page
  • To create this page
  • Open a terminal window and navigate to the
    directory that contains your Python source file
  • Example student.py
  • At the terminal command prompt, run
  • pydoc w student gt student.html

33
Testing
  • A great deal of time, effort, and money is spent
    trying to prevent software errors from ever
    happening
  • In this section, we examine testing, the
    discipline that searches for program errors and
    builds confidence that software works correctly

34
What to Test
  • There are several features of a programs
    behavior that we might want to consider
  • Is the program user friendly?
  • Is the program robust?
  • Is the program reliable?
  • Does the program provide acceptable performance?
  • In this section we confine our attention to a
    more minimal sense of program correctness
  • A correct program produces the expected outputs
    when operating on inputs that fall within
    specified limits

35
Three Approaches to Choosing Test Data
  • Haphazard Bang on the program with a few inputs
    until it breaks, fix bugs, and call it correct
  • Not very effective
  • Black-box Partition inputs in clusters of
    equivalent data (if program works correctly on a
    set of values from a cluster, it works well for
    all values in cluster)
  • Also test for values on boundaries between
    clusters
  • Easy to overlook some clusters
  • Number of clusters can be so large that we cannot
    possibly consider them all

36
Three Approaches to Choosing Test Data (continued)
  • White-box Attempt to concoct test data that
    exercise all parts of our program
  • You examine the code closely and then formulate
    the test data, but the task can be difficult
  • Code coverage
  • Tip Combine black-box testing with white-box
    testing

37
When to Test
  • Four points during coding at which test data can
    be used
  • Unit testing Write code that instantiates an
    object of the desired type and then run a series
    of methods that subject it to a thorough workout
  • Integration testing After classes have been
    tested in isolation, confirm that they work
    together properly
  • Acceptance testing Test under conditions
    identical to those in which it will eventually be
    used
  • Regression testing Keep test data for later use
    (e.g., to test modifications to program)

38
Proofs of Program Correctness
  • Testing cannot actually demonstrate the programs
    correctness
  • We can prove that a program is correct in a
    strictly mathematical sense
  • Quite tedious
  • May lead to invalid proofs if not enough
    mathematical background
  • Can we automate the process of determining
    program correctness?
  • It is impossible, not just difficult, to write a
    program that can solve the halting problem

39
Unit Testing in Python
  • Kent Beck developed one of the first tools for
    the unit testing of classes (for Smalltalk)
  • Beck and Erich Gamma wrote junit and pyunit
  • A unit test consists of a set of test cases for a
    given class
  • Each test case is a method that runs tests on an
    individual method in the class
  • The tests take the form of assertions
  • A test suite can be run whenever a change is made
    to the class under development

40
Unit Testing in Python (continued)
41
Unit Testing in Python (continued)
42
Unit Testing in Python (continued)
43
Unit Testing in Python (continued)
44
Unit Testing in Python (continued)
45
Unit Testing in Python (continued)
  • The TestCase class includes a significant number
    of methods that you can use to make assertions
    about your code

46
Unit Testing in Python (continued)
  • Although unit tests are ideal for testing
    individual classes, to a certain extent they can
    also be used during integration testing
  • By developing and running a good unit test for
    each class, programmer can focus on writing code
    and leave the testing to an automated assistant
  • Test-driven development encourages detection of
    errors early in the coding process and increases
    the likelihood of thorough white-box testing

47
Summary
  • UML is the leading graphical notation for
    depicting the structure and behavior of OO
    software systems
  • A use case describes the steps performed during
    an single interaction of a user with a SW system
  • A class diagram shows the relationships among the
    classes of a software system
  • A collaboration diagram shows interactions among
    objects required to complete a task in a SW
    system
  • Preconditions and postconditions form the subject
    of an agreement between the author of a method
    and its client

48
Summary (continued)
  • You can enforce a precondition by raising an
    exception if that precondition is not satisfied
  • Complete documentation of a method includes
    information about any preconditions,
    postconditions, and exceptions that it might
    raise
  • Generate Web-based documentation for a Python
    resource by running the pydoc tool
  • Black-box testing Programmer selects test data
    that produce expected results for normal inputs
    and abnormal inputs

49
Summary (continued)
  • White-box testing Select test data that exercise
    all of the possible execution paths in a given
    resource
  • Unit test Subjects a class to a thorough workout
  • Pyunit provides a tool for automating unit tests
  • Integration testing examines the behavior of
    cooperating classes that have been unit tested
  • Acceptance testing subjects a software system to
    examination under realistic conditions
  • Regression testing is performed when any part of
    a software system is repaired or extended
Write a Comment
User Comments (0)
About PowerShow.com